Mercurial > ec-userscripts
annotate melonbooks-unlazy.user.js @ 127:a701294f38ff
More reliable state handling
author | nanaya <me@nanaya.net> |
---|---|
date | Sat, 21 Jan 2023 03:15:26 +0900 |
parents | d9dc190bccaf |
children |
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 | |
108 | 13 'use strict'; |
95 | 14 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
15 function fix (image) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
16 if (!image.classList.contains('lazyload')) return; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
17 |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
18 const src = image.dataset.src; |
109 | 19 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
20 if (src == null || src === '') return; |
109 | 21 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
22 image.classList.remove('lazyload'); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
23 image.src = image.dataset.src; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
24 delete image.dataset.src; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
25 } |
102 | 26 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
27 function run (node) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
28 if (!(node instanceof window.HTMLElement)) return; |
109 | 29 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
30 fix(node); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
31 for (const image of node.querySelectorAll('.lazyload')) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
32 fix(image); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
33 } |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
34 } |
98
3e5f1fa9ed52
Skip redundant class check and add dataset check instead
nanaya <me@nanaya.net>
parents:
97
diff
changeset
|
35 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
36 function onMutate (mutations) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
37 for (const mutation of mutations) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
38 for (const node of mutation.addedNodes) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
39 run(node); |
109 | 40 } |
41 } | |
97 | 42 } |
95 | 43 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
44 const observer = new window.MutationObserver(onMutate); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
45 observer.observe(document, { childList: true, subtree: true }); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
46 run(document.body); |