Mercurial > ec-userscripts
annotate soranews-image.user.js @ 98:3e5f1fa9ed52
Skip redundant class check and add dataset check instead
| author | nanaya <me@nanaya.net> |
|---|---|
| date | Fri, 23 Dec 2022 19:58:14 +0900 |
| parents | 9c8cde985caf |
| children | 2c4470b73ad9 |
| 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 | |
| 85 | 15 const fix = function (image) { |
| 75 | 16 // basic sanity check |
| 17 if (!image.classList.contains('lazy')) { | |
| 18 return | |
| 19 } | |
| 20 | |
| 21 image.classList.remove('lazy') | |
| 22 image.removeAttribute('src') | |
|
84
af4f8b25495e
Merge elements first and use actual srcset
nanaya <me@nanaya.pro>
parents:
82
diff
changeset
|
23 image.setAttribute('srcset', image.dataset.scoSrcset) |
| 75 | 24 } |
| 25 | |
| 85 | 26 // loop through passed nodes (or body if called without arguments) |
| 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) { | |
| 75 | 44 run(mutation.addedNodes) |
| 45 } | |
| 46 } | |
| 47 | |
| 48 // the observer | |
| 85 | 49 const observer = new window.MutationObserver(onMutate) |
| 75 | 50 |
| 51 // start the observer | |
| 52 observer.observe(document, { childList: true, subtree: true }) | |
| 53 // initial run on existing document | |
| 54 run() | |
| 55 }).call() |
