comparison melonbooks-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
comparison
equal deleted inserted replaced
116:0e108a9dc6d7 117:d9dc190bccaf
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 unlazy () { 15 function fix (image) {
16 function fix (image) { 16 if (!image.classList.contains('lazyload')) return;
17 if (!image.classList.contains('lazyload')) return;
18 17
19 const src = image.dataset.src; 18 const src = image.dataset.src;
20 19
21 if (src == null || src === '') return; 20 if (src == null || src === '') return;
22 21
23 image.classList.remove('lazyload'); 22 image.classList.remove('lazyload');
24 image.src = image.dataset.src; 23 image.src = image.dataset.src;
25 delete image.dataset.src; 24 delete image.dataset.src;
25 }
26
27 function run (node) {
28 if (!(node instanceof window.HTMLElement)) return;
29
30 fix(node);
31 for (const image of node.querySelectorAll('.lazyload')) {
32 fix(image);
26 } 33 }
34 }
27 35
28 function run (node) { 36 function onMutate (mutations) {
29 if (!(node instanceof window.HTMLElement)) return; 37 for (const mutation of mutations) {
30 38 for (const node of mutation.addedNodes) {
31 fix(node); 39 run(node);
32 for (const image of node.querySelectorAll('.lazyload')) {
33 fix(image);
34 } 40 }
35 } 41 }
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);
48 } 42 }
49 43
50 unlazy(); 44 const observer = new window.MutationObserver(onMutate);
45 observer.observe(document, { childList: true, subtree: true });
46 run(document.body);