comparison pixiv-fanbox-unlazy.user.js @ 117:d9dc190bccaf

Undo combining fixes into single script per site
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:49:35 +0900
parents ef21ef445fc6
children 8de2d53a4cb1
comparison
equal deleted inserted replaced
116:0e108a9dc6d7 117:d9dc190bccaf
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 function unlazy () { 15 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/';
16 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/';
17 16
18 function disableEventLink (event) { 17 function disableEventLink (event) {
19 event.stopPropagation(); 18 event.stopPropagation();
19 }
20
21 function fix (link) {
22 const href = link.href;
23
24 // basic sanity check
25 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) {
26 return;
20 } 27 }
21 28
22 function fix (link) { 29 // don't run again if already run on passed link
23 const href = link.href; 30 if (link._ecUserscript) {
31 return;
32 }
33 link._ecUserscript = true;
24 34
25 // basic sanity check 35 link.addEventListener('click', disableEventLink);
26 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { 36 const image = document.createElement('img');
27 return; 37 image.style.width = '100%';
28 } 38 image.src = href;
39 link.replaceChildren(image);
40 }
29 41
30 // don't run again if already run on passed link 42 function run (node) {
31 if (link._ecUserscript) { 43 if (!(node instanceof window.HTMLElement)) return;
32 return;
33 }
34 link._ecUserscript = true;
35 44
36 link.addEventListener('click', disableEventLink); 45 fix(node);
37 const image = document.createElement('img'); 46 for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) {
38 image.style.width = '100%'; 47 fix(link);
39 image.src = href;
40 link.replaceChildren(image);
41 } 48 }
49 }
42 50
43 function run (node) { 51 function onMutate (mutations) {
44 if (!(node instanceof window.HTMLElement)) return; 52 for (const mutation of mutations) {
45 53 for (const node of mutation.addedNodes) {
46 fix(node); 54 run(node);
47 for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) {
48 fix(link);
49 } 55 }
50 } 56 }
51
52 function onMutate (mutations) {
53 for (const mutation of mutations) {
54 for (const node of mutation.addedNodes) {
55 run(node);
56 }
57 }
58 }
59
60 const observer = new window.MutationObserver(onMutate);
61 observer.observe(document, { childList: true, subtree: true });
62 run(document.body);
63 } 57 }
64 58
65 unlazy(); 59 const observer = new window.MutationObserver(onMutate);
60 observer.observe(document, { childList: true, subtree: true });
61 run(document.body);