comparison pixiv-fanbox-unlazy.user.js @ 109:ef21ef445fc6

Cleanups - wrap everything in function and call them last - fix namespace and update url
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:33:41 +0900
parents 2c4470b73ad9
children d9dc190bccaf
comparison
equal deleted inserted replaced
108:2c4470b73ad9 109:ef21ef445fc6
1 // ==UserScript== 1 // ==UserScript==
2 // @name pixiv fanbox no lazy loading image 2 // @name pixiv fanbox no lazy loading image
3 // @namespace https://myconan.net 3 // @namespace https://nanaya.net
4 // @version 2.1.2 4 // @version 2.1.2
5 // @description Lazy loading is bad for environment. Disable it. 5 // @description Lazy loading is bad for environment. Disable it.
6 // @author nanaya 6 // @author nanaya
7 // @match https://*.fanbox.cc/* 7 // @match https://*.fanbox.cc/*
8 // @grant none 8 // @grant none
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 function unlazy () {
16 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/';
16 17
17 function disableEventLink (event) { 18 function disableEventLink (event) {
18 event.stopPropagation(); 19 event.stopPropagation();
20 }
21
22 function fix (link) {
23 const href = link.href;
24
25 // basic sanity check
26 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) {
27 return;
28 }
29
30 // don't run again if already run on passed link
31 if (link._ecUserscript) {
32 return;
33 }
34 link._ecUserscript = true;
35
36 link.addEventListener('click', disableEventLink);
37 const image = document.createElement('img');
38 image.style.width = '100%';
39 image.src = href;
40 link.replaceChildren(image);
41 }
42
43 function run (node) {
44 if (!(node instanceof window.HTMLElement)) return;
45
46 fix(node);
47 for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) {
48 fix(link);
49 }
50 }
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);
19 } 63 }
20 64
21 function fix (link) { 65 unlazy();
22 const href = link.href;
23
24 // basic sanity check
25 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) {
26 return;
27 }
28
29 // don't run again if already run on passed link
30 if (link._ecUserscript) {
31 return;
32 }
33 link._ecUserscript = true;
34
35 link.addEventListener('click', disableEventLink);
36 const image = document.createElement('img');
37 image.style.width = '100%';
38 image.src = href;
39 link.replaceChildren(image);
40 }
41
42 function run (node) {
43 if (!(node instanceof window.HTMLElement)) return;
44
45 fix(node);
46 for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) {
47 fix(link);
48 }
49 }
50
51 function onMutate (mutations) {
52 for (const mutation of mutations) {
53 for (const node of mutation.addedNodes) {
54 run(node);
55 }
56 }
57 }
58
59 const observer = new window.MutationObserver(onMutate);
60 observer.observe(document, { childList: true, subtree: true });
61 run(document.body);