Mercurial > ec-userscripts
annotate melonbooks-unlazy.user.js @ 104:93e21738b588
Run fix on whole body on start
There's no guarantee the script loads before document.
author | nanaya <me@nanaya.net> |
---|---|
date | Sun, 25 Dec 2022 11:52:30 +0900 |
parents | 3fded109e23a |
children | 2c4470b73ad9 |
rev | line source |
---|---|
95 | 1 // ==UserScript== |
2 // @name melonbooks unlazy | |
3 // @namespace https://nanaya.net | |
4 // @match https://www.melonbooks.co.jp/* | |
5 // @grant none | |
97 | 6 // @run-at document-start |
104 | 7 // @version 1.0.5 |
95 | 8 // @author nanaya |
9 // @description replace lazy loaded images with just images | |
10 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/melonbooks-unlazy.user.js | |
11 // ==/UserScript== | |
12 | |
13 'use strict' | |
14 | |
97 | 15 function fix (image) { |
102 | 16 if (!image.classList.contains('lazyload')) return |
17 | |
98
3e5f1fa9ed52
Skip redundant class check and add dataset check instead
nanaya <me@nanaya.net>
parents:
97
diff
changeset
|
18 const src = image.dataset.src |
3e5f1fa9ed52
Skip redundant class check and add dataset check instead
nanaya <me@nanaya.net>
parents:
97
diff
changeset
|
19 |
99 | 20 if (src == null || src === '') return |
97 | 21 |
22 image.classList.remove('lazyload') | |
23 image.src = image.dataset.src | |
24 delete image.dataset.src | |
25 } | |
95 | 26 |
104 | 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) | |
33 } | |
34 } | |
35 | |
97 | 36 function onMutate (mutations) { |
37 for (const mutation of mutations) { | |
38 for (const node of mutation.addedNodes) { | |
104 | 39 run(node) |
97 | 40 } |
95 | 41 } |
42 } | |
97 | 43 |
44 const observer = new window.MutationObserver(onMutate) | |
45 observer.observe(document, { childList: true, subtree: true }) | |
104 | 46 run(document.body) |