comparison melonbooks-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
10 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/melonbooks-unlazy.user.js 10 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/melonbooks-unlazy.user.js
11 // ==/UserScript== 11 // ==/UserScript==
12 12
13 'use strict'; 13 'use strict';
14 14
15 function fix (image) { 15 function unlazy () {
16 if (!image.classList.contains('lazyload')) return; 16 function fix (image) {
17 if (!image.classList.contains('lazyload')) return;
17 18
18 const src = image.dataset.src; 19 const src = image.dataset.src;
19 20
20 if (src == null || src === '') return; 21 if (src == null || src === '') return;
21 22
22 image.classList.remove('lazyload'); 23 image.classList.remove('lazyload');
23 image.src = image.dataset.src; 24 image.src = image.dataset.src;
24 delete image.dataset.src; 25 delete image.dataset.src;
26 }
27
28 function run (node) {
29 if (!(node instanceof window.HTMLElement)) return;
30
31 fix(node);
32 for (const image of node.querySelectorAll('.lazyload')) {
33 fix(image);
34 }
35 }
36
37 function onMutate (mutations) {
38 for (const mutation of mutations) {
39 for (const node of mutation.addedNodes) {
40 run(node);
41 }
42 }
43 }
44
45 const observer = new window.MutationObserver(onMutate);
46 observer.observe(document, { childList: true, subtree: true });
47 run(document.body);
25 } 48 }
26 49
27 function run (node) { 50 unlazy();
28 if (!(node instanceof window.HTMLElement)) return;
29
30 fix(node);
31 for (const image of node.querySelectorAll('.lazyload')) {
32 fix(image);
33 }
34 }
35
36 function onMutate (mutations) {
37 for (const mutation of mutations) {
38 for (const node of mutation.addedNodes) {
39 run(node);
40 }
41 }
42 }
43
44 const observer = new window.MutationObserver(onMutate);
45 observer.observe(document, { childList: true, subtree: true });
46 run(document.body);