annotate soranews-image.user.js @ 103:b2d0b37f945f

Update mandarake script - update download url - remove iife - run at document start - use observer - remove search link fixer (seems to be fixed already)
author nanaya <me@nanaya.net>
date Sat, 24 Dec 2022 22:10:30 +0900
parents 9c8cde985caf
children 2c4470b73ad9
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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
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
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
82
86da34e62d29 Update url
nanaya <me@nanaya.pro>
parents: 77
diff changeset
9 // @downloadURL https://hg.myconan.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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
12 ;(function () {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
13 'use strict'
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
14
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')) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
18 return
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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
21 image.classList.remove('lazy')
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
24 }
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) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
29 nodes = [document.body]
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)
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
34 const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')]
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++) {
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
37 fix(images[j])
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 }
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
diff changeset
40 }
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) {
75
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
44 run(mutation.addedNodes)
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
45 }
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
46 }
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
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 84
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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
52 observer.observe(document, { childList: true, subtree: true })
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
53 // initial run on existing document
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
54 run()
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
55 }).call()