Mercurial > ec-userscripts
annotate soranews-image.user.js @ 114:817b62848b27
Backed out changeset 055f5d084706
author | nanaya <me@nanaya.net> |
---|---|
date | Sun, 15 Jan 2023 23:43:40 +0900 |
parents | ef21ef445fc6 |
children | d9dc190bccaf |
rev | line source |
---|---|
75 | 1 // ==UserScript== |
2 // @name Soranews image fix | |
109 | 3 // @namespace https://nanaya.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 | |
109 | 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 |
109 | 14 function unlazy () { |
85 | 15 const fix = function (image) { |
75 | 16 // basic sanity check |
17 if (!image.classList.contains('lazy')) { | |
108 | 18 return; |
75 | 19 } |
20 | |
108 | 21 image.classList.remove('lazy'); |
22 image.removeAttribute('src'); | |
23 image.setAttribute('srcset', image.dataset.scoSrcset); | |
24 }; | |
75 | 25 |
85 | 26 // loop through passed nodes (or body if called without arguments) |
27 const run = function (nodes) { | |
28 if (nodes == null) { | |
108 | 29 nodes = [document.body]; |
85 | 30 } |
31 | |
32 for (let i = 0; i < nodes.length; i++) { | |
33 // find all the images inside (and self) | |
108 | 34 const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')]; |
85 | 35 |
36 for (let j = 0; j < images.length; j++) { | |
108 | 37 fix(images[j]); |
85 | 38 } |
39 } | |
108 | 40 }; |
85 | 41 |
42 const onMutate = function (mutations) { | |
43 for (const mutation in mutations) { | |
108 | 44 run(mutation.addedNodes); |
75 | 45 } |
108 | 46 }; |
75 | 47 |
48 // the observer | |
108 | 49 const observer = new window.MutationObserver(onMutate); |
75 | 50 |
51 // start the observer | |
108 | 52 observer.observe(document, { childList: true, subtree: true }); |
75 | 53 // initial run on existing document |
108 | 54 run(); |
109 | 55 } |
56 | |
57 unlazy(); |