annotate soranews-image.user.js @ 127:a701294f38ff

More reliable state handling
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 03:15:26 +0900
parents 8de2d53a4cb1
children
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==
118
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
2 // @name Soranews image fix
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
3 // @namespace https://nanaya.net
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
4 // @version 1.0.3
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
5 // @description Soranews lazy load image fix
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
6 // @author nanaya
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
7 // @match https://soranews24.com/*
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
8 // @grant none
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
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
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
14 const fix = function (image) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
15 // basic sanity check
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
16 if (!image.classList.contains('lazy')) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
17 return;
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
18 }
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
19
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
20 image.classList.remove('lazy');
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
21 image.removeAttribute('src');
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
22 image.setAttribute('srcset', image.dataset.scoSrcset);
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
23 };
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
24
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
25 // loop through passed nodes (or body if called without arguments)
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
26 const run = function (nodes) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
27 if (nodes == null) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
28 nodes = [document.body];
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
29 }
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
30
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
31 for (let i = 0; i < nodes.length; i++) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
32 // find all the images inside (and self)
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
33 const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')];
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
34
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
35 for (let j = 0; j < images.length; j++) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
36 fix(images[j]);
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
37 }
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
38 }
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
39 };
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
40
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
41 const onMutate = function (mutations) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
42 for (const mutation in mutations) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
43 run(mutation.addedNodes);
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
44 }
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
45 };
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
46
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
47 // the observer
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
48 const observer = new window.MutationObserver(onMutate);
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
49
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
50 // start the observer
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
51 observer.observe(document, { childList: true, subtree: true });
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
52 // initial run on existing document
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
53 run();