Mercurial > ec-userscripts
annotate soranews-image.user.js @ 120:f3f646436567
Forgot to add update url
author | nanaya <me@nanaya.net> |
---|---|
date | Tue, 17 Jan 2023 15:49:24 +0900 |
parents | 8de2d53a4cb1 |
children |
rev | line source |
---|---|
75 | 1 // ==UserScript== |
118 | 2 // @name Soranews image fix |
3 // @namespace https://nanaya.net | |
4 // @version 1.0.3 | |
5 // @description Soranews lazy load image fix | |
6 // @author nanaya | |
7 // @match https://soranews24.com/* | |
8 // @grant none | |
9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/soranews-image.user.js | |
75 | 10 // ==/UserScript== |
11 | |
109 | 12 'use strict'; |
75 | 13 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
14 const fix = function (image) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
15 // basic sanity check |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
16 if (!image.classList.contains('lazy')) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
17 return; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
18 } |
75 | 19 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
20 image.classList.remove('lazy'); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
21 image.removeAttribute('src'); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
22 image.setAttribute('srcset', image.dataset.scoSrcset); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
23 }; |
75 | 24 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
25 // loop through passed nodes (or body if called without arguments) |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
26 const run = function (nodes) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
27 if (nodes == null) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
28 nodes = [document.body]; |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
29 } |
85 | 30 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
31 for (let i = 0; i < nodes.length; i++) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
32 // find all the images inside (and self) |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
33 const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')]; |
85 | 34 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
35 for (let j = 0; j < images.length; j++) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
36 fix(images[j]); |
75 | 37 } |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
38 } |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
39 }; |
75 | 40 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
41 const onMutate = function (mutations) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
42 for (const mutation in mutations) { |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
43 run(mutation.addedNodes); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
44 } |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
45 }; |
109 | 46 |
117
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
47 // the observer |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
48 const observer = new window.MutationObserver(onMutate); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
49 |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
50 // start the observer |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
51 observer.observe(document, { childList: true, subtree: true }); |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
52 // initial run on existing document |
d9dc190bccaf
Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents:
109
diff
changeset
|
53 run(); |