comparison soranews-image.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
9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/soranews-image.user.js 9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/soranews-image.user.js
10 // ==/UserScript== 10 // ==/UserScript==
11 11
12 'use strict'; 12 'use strict';
13 13
14 function unlazy () { 14 const fix = function (image) {
15 const fix = function (image) { 15 // basic sanity check
16 // basic sanity check 16 if (!image.classList.contains('lazy')) {
17 if (!image.classList.contains('lazy')) { 17 return;
18 return; 18 }
19
20 image.classList.remove('lazy');
21 image.removeAttribute('src');
22 image.setAttribute('srcset', image.dataset.scoSrcset);
23 };
24
25 // loop through passed nodes (or body if called without arguments)
26 const run = function (nodes) {
27 if (nodes == null) {
28 nodes = [document.body];
29 }
30
31 for (let i = 0; i < nodes.length; i++) {
32 // find all the images inside (and self)
33 const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')];
34
35 for (let j = 0; j < images.length; j++) {
36 fix(images[j]);
19 } 37 }
38 }
39 };
20 40
21 image.classList.remove('lazy'); 41 const onMutate = function (mutations) {
22 image.removeAttribute('src'); 42 for (const mutation in mutations) {
23 image.setAttribute('srcset', image.dataset.scoSrcset); 43 run(mutation.addedNodes);
24 }; 44 }
45 };
25 46
26 // loop through passed nodes (or body if called without arguments) 47 // the observer
27 const run = function (nodes) { 48 const observer = new window.MutationObserver(onMutate);
28 if (nodes == null) {
29 nodes = [document.body];
30 }
31 49
32 for (let i = 0; i < nodes.length; i++) { 50 // start the observer
33 // find all the images inside (and self) 51 observer.observe(document, { childList: true, subtree: true });
34 const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')]; 52 // initial run on existing document
35 53 run();
36 for (let j = 0; j < images.length; j++) {
37 fix(images[j]);
38 }
39 }
40 };
41
42 const onMutate = function (mutations) {
43 for (const mutation in mutations) {
44 run(mutation.addedNodes);
45 }
46 };
47
48 // the observer
49 const observer = new window.MutationObserver(onMutate);
50
51 // start the observer
52 observer.observe(document, { childList: true, subtree: true });
53 // initial run on existing document
54 run();
55 }
56
57 unlazy();