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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
1 // ==UserScript==
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
2 // @name Soranews image fix
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
5 // @description Soranews lazy load image fix
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
6 // @author nanaya
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
7 // @match https://soranews24.com/*
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
8 // @grant none
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
9 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/soranews-image.user.js
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
10 // ==/UserScript==
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
11
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
12 'use strict';
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
13
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
14 function unlazy () {
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
15 const fix = function (image) {
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
16 // basic sanity check
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
17 if (!image.classList.contains('lazy')) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
18 return;
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
19 }
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
20
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
21 image.classList.remove('lazy');
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
22 image.removeAttribute('src');
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
23 image.setAttribute('srcset', image.dataset.scoSrcset);
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
24 };
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
25
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
26 // loop through passed nodes (or body if called without arguments)
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
27 const run = function (nodes) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
28 if (nodes == null) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
29 nodes = [document.body];
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
30 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
31
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
32 for (let i = 0; i < nodes.length; i++) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
33 // find all the images inside (and self)
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
34 const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')];
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
35
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
36 for (let j = 0; j < images.length; j++) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
37 fix(images[j]);
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
38 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
39 }
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
40 };
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
41
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
42 const onMutate = function (mutations) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
43 for (const mutation in mutations) {
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
44 run(mutation.addedNodes);
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
45 }
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
46 };
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
47
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
48 // the observer
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
49 const observer = new window.MutationObserver(onMutate);
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
50
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
51 // start the observer
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
52 observer.observe(document, { childList: true, subtree: true });
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
53 // initial run on existing document
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 85
diff changeset
54 run();
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
55 }
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
56
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
57 unlazy();