annotate soranews-image.user.js @ 77:5a3a269c5e6f

Update downloadurl
author nanaya <me@nanaya.pro>
date Wed, 08 Jul 2020 20:45:05 +0900
parents 0aa28e02e257
children 86da34e62d29
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
77
5a3a269c5e6f Update downloadurl
nanaya <me@nanaya.pro>
parents: 75
diff changeset
4 // @version 1.0.1
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
77
5a3a269c5e6f Update downloadurl
nanaya <me@nanaya.pro>
parents: 75
diff changeset
9 // @downloadURL https://hg.sr.ht/~nanaya/ec-userscripts/raw/soranews-image.user.js?rev=tip
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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
15 // loop through passed nodes (or body if called without arguments)
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
16 var run = function (nodes) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
17 if (nodes == null) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
18 nodes = [document.body]
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 for (var i = 0; i < nodes.length; i++) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
22 // first try fixing itself
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
23 fix(nodes[i])
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 // and then find all the images inside
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
26 var images = nodes[i].querySelectorAll('.lazy')
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
27
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
28 for (var j = 0; j < images.length; j++) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
29 fix(images[j])
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
30 }
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
31 }
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
32 }
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
33
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
34 var fix = function (image) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
35 // basic sanity check
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
36 if (!image.classList.contains('lazy')) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
37 return
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
38 }
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
39
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
40 image.classList.remove('lazy')
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
41 image.removeAttribute('src')
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
42 image.setAttribute('srcset', image.dataset.scoSrc)
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
43 }
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
44
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
45 var onMutate = function (mutations) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
46 for (var mutation in mutations) {
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
47 run(mutation.addedNodes)
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
48 }
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
49 }
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 // the observer
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
52 var observer = new window.MutationObserver(onMutate)
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
53
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
54 // start the observer
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
55 observer.observe(document, { childList: true, subtree: true })
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
56 // initial run on existing document
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
57 run()
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
58 }).call()