annotate 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
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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
4 // @version 1.0.0
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
0aa28e02e257 Add soranews unlazy image
nanaya <me@nanaya.pro>
parents:
diff changeset
9 // @downloadURL https://hg.sr.ht/~nanaya/ec-userscripts/raw/default/soranews-image.user.js
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()