Mercurial > ec-userscripts
comparison pixiv-fanbox-unlazy.user.js @ 108:2c4470b73ad9
Switch to semistandard
No more `;` at the start of line.
author | nanaya <me@nanaya.net> |
---|---|
date | Sun, 15 Jan 2023 23:24:03 +0900 |
parents | 93e21738b588 |
children | ef21ef445fc6 |
comparison
equal
deleted
inserted
replaced
107:4bc5a633437c | 108:2c4470b73ad9 |
---|---|
8 // @grant none | 8 // @grant none |
9 // @run-at document-start | 9 // @run-at document-start |
10 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/pixiv-fanbox-unlazy.user.js | 10 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/pixiv-fanbox-unlazy.user.js |
11 // ==/UserScript== | 11 // ==/UserScript== |
12 | 12 |
13 'use strict' | 13 'use strict'; |
14 | 14 |
15 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/' | 15 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/'; |
16 | 16 |
17 function disableEventLink (event) { | 17 function disableEventLink (event) { |
18 event.stopPropagation() | 18 event.stopPropagation(); |
19 } | 19 } |
20 | 20 |
21 function fix (link) { | 21 function fix (link) { |
22 const href = link.href | 22 const href = link.href; |
23 | 23 |
24 // basic sanity check | 24 // basic sanity check |
25 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { | 25 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { |
26 return | 26 return; |
27 } | 27 } |
28 | 28 |
29 // don't run again if already run on passed link | 29 // don't run again if already run on passed link |
30 if (link._ecUserscript) { | 30 if (link._ecUserscript) { |
31 return | 31 return; |
32 } | 32 } |
33 link._ecUserscript = true | 33 link._ecUserscript = true; |
34 | 34 |
35 link.addEventListener('click', disableEventLink) | 35 link.addEventListener('click', disableEventLink); |
36 const image = document.createElement('img') | 36 const image = document.createElement('img'); |
37 image.style.width = '100%' | 37 image.style.width = '100%'; |
38 image.src = href | 38 image.src = href; |
39 link.replaceChildren(image) | 39 link.replaceChildren(image); |
40 } | 40 } |
41 | 41 |
42 function run (node) { | 42 function run (node) { |
43 if (!(node instanceof window.HTMLElement)) return | 43 if (!(node instanceof window.HTMLElement)) return; |
44 | 44 |
45 fix(node) | 45 fix(node); |
46 for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) { | 46 for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) { |
47 fix(link) | 47 fix(link); |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 function onMutate (mutations) { | 51 function onMutate (mutations) { |
52 for (const mutation of mutations) { | 52 for (const mutation of mutations) { |
53 for (const node of mutation.addedNodes) { | 53 for (const node of mutation.addedNodes) { |
54 run(node) | 54 run(node); |
55 } | 55 } |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 const observer = new window.MutationObserver(onMutate) | 59 const observer = new window.MutationObserver(onMutate); |
60 observer.observe(document, { childList: true, subtree: true }) | 60 observer.observe(document, { childList: true, subtree: true }); |
61 run(document.body) | 61 run(document.body); |