tophf / mpiv

A fully reworked fork of Mouseover Popup Image Viewer
MIT License
167 stars 21 forks source link

Fix for substitution rule failure on yande.re #115

Closed necaran closed 6 months ago

necaran commented 6 months ago

When I was trying to compose a better rule for pixiv, I found that mpiv always fails to load external links on ynade.re. Here is my pixiv rule. It works on pixiv.net as well as other sites such as https://danbooru.donmai.us/posts/7519462.

{"r":"(www\\.pixiv\\.net)/(?:\\w\\w/)?artworks/(\\d+).*",
 "s":"https://$1/ajax/illust/$2/pages",
 "g":"let _=JSON.parse(text).body.map(_=>({url:_.urls.original})), i=node.querySelector('img[alt]'); _.title=i?i.alt:node.href; return _",
 "xhr":true}

But It just cannot load pixiv links on yande.re (e.g. https://yande.re/post/show/1167946). If rule.s is provided, url is always resolved as https://yande.re/post/show/undefined. Afer digging into the script of both mpiv and yande.re, I found the culprit at https://assets.yande.re/assets/application_classic-235ec31087b8915268055065472ee9fe5fd4be45ae482e55bb3af61d678b9752.js.

Array.from=$A

It turns out yande.re has its own implementation of Array.from, which is incompatible with mpiv's standard use. (/facepalm)

    return u === '' /* "stop all rules" */ ? urls
      : u && Array.from(new Set(urls), Util.decodeUrl);

With their non-standard implementation, the Array.from statement in mpiv always returns an empty array. It is obviously their fault, but I can only assume it to be their effort to support old browsers. I would not expect them to fix it, so I propose to modify mpiv's code like this.

    return u === '' /* "stop all rules" */ ? urls
      : u && urls.map(Util.decodeUrl);

BTW, the pixiv rules on the wiki page need update too. The regular rule loads only one page and is domain-limited. The album rule no longer works. My rule loads multiple pages via the official JSON API and works across different sites.

Steps to reproduce the bug

  1. Go to https://yande.re/post/show/1167946
  2. Add the rule for pixiv.
    {"r":"(www\\.pixiv\\.net)/(?:\\w\\w/)?artworks/(\\d+).*",
    "s":"https://$1/ajax/illust/$2/pages",
    "g":"let _=JSON.parse(text).body.map(_=>({url:_.urls.original})), i=node.querySelector('img[alt]'); _.title=i?i.alt:node.href; return _",
    "xhr":true}
  3. Keep the config popup open and move mouse to the link to pixiv on the left.

Expected behavior

Images from pixiv should be loaded.

Screenshots

Paste

Environment

tophf commented 6 months ago

I've opened write access for wiki. Do you want to edit it yourself? If not can I copy your rule there?

necaran commented 6 months ago

Wow, that is fast! Thank you for the permission! I might have some other rules too add since I am trying to migrate from Imagus.