Mercurial > ec-userscripts
comparison soranews-image.user.js @ 75:0aa28e02e257
Add soranews unlazy image
author | nanaya <me@nanaya.pro> |
---|---|
date | Tue, 07 Jul 2020 01:58:09 +0900 |
parents | |
children | 5a3a269c5e6f |
comparison
equal
deleted
inserted
replaced
74:78598a92e6fc | 75:0aa28e02e257 |
---|---|
1 // ==UserScript== | |
2 // @name Soranews image fix | |
3 // @namespace https://myconan.net | |
4 // @version 1.0.0 | |
5 // @description Soranews lazy load image fix | |
6 // @author nanaya | |
7 // @match https://soranews24.com/* | |
8 // @grant none | |
9 // @downloadURL https://hg.sr.ht/~nanaya/ec-userscripts/raw/default/soranews-image.user.js | |
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++) { | |
22 // first try fixing itself | |
23 fix(nodes[i]) | |
24 | |
25 // and then find all the images inside | |
26 var images = nodes[i].querySelectorAll('.lazy') | |
27 | |
28 for (var j = 0; j < images.length; j++) { | |
29 fix(images[j]) | |
30 } | |
31 } | |
32 } | |
33 | |
34 var fix = function (image) { | |
35 // basic sanity check | |
36 if (!image.classList.contains('lazy')) { | |
37 return | |
38 } | |
39 | |
40 image.classList.remove('lazy') | |
41 image.removeAttribute('src') | |
42 image.setAttribute('srcset', image.dataset.scoSrc) | |
43 } | |
44 | |
45 var onMutate = function (mutations) { | |
46 for (var mutation in mutations) { | |
47 run(mutation.addedNodes) | |
48 } | |
49 } | |
50 | |
51 // the observer | |
52 var observer = new window.MutationObserver(onMutate) | |
53 | |
54 // start the observer | |
55 observer.observe(document, { childList: true, subtree: true }) | |
56 // initial run on existing document | |
57 run() | |
58 }).call() |