Mercurial > ec-userscripts
comparison soranews-image.user.js @ 85:9c8cde985caf
Update standard and fix
author | nanaya <me@nanaya.pro> |
---|---|
date | Thu, 15 Apr 2021 15:01:22 +0900 |
parents | af4f8b25495e |
children | 2c4470b73ad9 |
comparison
equal
deleted
inserted
replaced
84:af4f8b25495e | 85:9c8cde985caf |
---|---|
10 // ==/UserScript== | 10 // ==/UserScript== |
11 | 11 |
12 ;(function () { | 12 ;(function () { |
13 'use strict' | 13 'use strict' |
14 | 14 |
15 // loop through passed nodes (or body if called without arguments) | 15 const fix = function (image) { |
16 var run = function (nodes) { | |
17 if (nodes == null) { | |
18 nodes = [document.body] | |
19 } | |
20 | |
21 for (var i = 0; i < nodes.length; i++) { | |
22 // find all the images inside (and self) | |
23 var images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')] | |
24 | |
25 for (var j = 0; j < images.length; j++) { | |
26 fix(images[j]) | |
27 } | |
28 } | |
29 } | |
30 | |
31 var fix = function (image) { | |
32 // basic sanity check | 16 // basic sanity check |
33 if (!image.classList.contains('lazy')) { | 17 if (!image.classList.contains('lazy')) { |
34 return | 18 return |
35 } | 19 } |
36 | 20 |
37 image.classList.remove('lazy') | 21 image.classList.remove('lazy') |
38 image.removeAttribute('src') | 22 image.removeAttribute('src') |
39 image.setAttribute('srcset', image.dataset.scoSrcset) | 23 image.setAttribute('srcset', image.dataset.scoSrcset) |
40 } | 24 } |
41 | 25 |
42 var onMutate = function (mutations) { | 26 // loop through passed nodes (or body if called without arguments) |
43 for (var mutation in mutations) { | 27 const run = function (nodes) { |
28 if (nodes == null) { | |
29 nodes = [document.body] | |
30 } | |
31 | |
32 for (let i = 0; i < nodes.length; i++) { | |
33 // find all the images inside (and self) | |
34 const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')] | |
35 | |
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) | 44 run(mutation.addedNodes) |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 // the observer | 48 // the observer |
49 var observer = new window.MutationObserver(onMutate) | 49 const observer = new window.MutationObserver(onMutate) |
50 | 50 |
51 // start the observer | 51 // start the observer |
52 observer.observe(document, { childList: true, subtree: true }) | 52 observer.observe(document, { childList: true, subtree: true }) |
53 // initial run on existing document | 53 // initial run on existing document |
54 run() | 54 run() |