Mercurial > ec-userscripts
annotate soranews-image.user.js @ 84:af4f8b25495e
Merge elements first and use actual srcset
author | nanaya <me@nanaya.pro> |
---|---|
date | Thu, 15 Apr 2021 14:56:23 +0900 |
parents | 86da34e62d29 |
children | 9c8cde985caf |
rev | line source |
---|---|
75 | 1 // ==UserScript== |
2 // @name Soranews image fix | |
3 // @namespace https://myconan.net | |
84
af4f8b25495e
Merge elements first and use actual srcset
nanaya <me@nanaya.pro>
parents:
82
diff
changeset
|
4 // @version 1.0.3 |
75 | 5 // @description Soranews lazy load image fix |
6 // @author nanaya | |
7 // @match https://soranews24.com/* | |
8 // @grant none | |
82 | 9 // @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/soranews-image.user.js |
75 | 10 // ==/UserScript== |
11 | |
12 ;(function () { | |
13 'use strict' | |
14 | |
15 // loop through passed nodes (or body if called without arguments) | |
16 var run = function (nodes) { | |
17 if (nodes == null) { | |
18 nodes = [document.body] | |
19 } | |
20 | |
21 for (var i = 0; i < nodes.length; i++) { | |
84
af4f8b25495e
Merge elements first and use actual srcset
nanaya <me@nanaya.pro>
parents:
82
diff
changeset
|
22 // find all the images inside (and self) |
af4f8b25495e
Merge elements first and use actual srcset
nanaya <me@nanaya.pro>
parents:
82
diff
changeset
|
23 var images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')] |
75 | 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 | |
33 if (!image.classList.contains('lazy')) { | |
34 return | |
35 } | |
36 | |
37 image.classList.remove('lazy') | |
38 image.removeAttribute('src') | |
84
af4f8b25495e
Merge elements first and use actual srcset
nanaya <me@nanaya.pro>
parents:
82
diff
changeset
|
39 image.setAttribute('srcset', image.dataset.scoSrcset) |
75 | 40 } |
41 | |
42 var onMutate = function (mutations) { | |
43 for (var mutation in mutations) { | |
44 run(mutation.addedNodes) | |
45 } | |
46 } | |
47 | |
48 // the observer | |
49 var 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 }).call() |