comparison pixiv-fanbox-unlazy.user.js @ 100:74c33632c353

Simplify script
author nanaya <me@nanaya.net>
date Sat, 24 Dec 2022 21:28:36 +0900
parents 9c8cde985caf
children e21710f5dd7b
comparison
equal deleted inserted replaced
99:b44d5cb661c5 100:74c33632c353
1 // ==UserScript== 1 // ==UserScript==
2 // @name pixiv fanbox no lazy loading image 2 // @name pixiv fanbox no lazy loading image
3 // @namespace https://myconan.net 3 // @namespace https://myconan.net
4 // @version 2.0.2 4 // @version 2.1.0
5 // @description Lazy loading is bad for environment. Disable it. 5 // @description Lazy loading is bad for environment. Disable it.
6 // @author nanaya 6 // @author nanaya
7 // @match https://*.fanbox.cc/* 7 // @match https://*.fanbox.cc/*
8 // @grant none 8 // @grant none
9 // @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/pixiv-fanbox-unlazy.user.js 9 // @run-at document-start
10 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/pixiv-fanbox-unlazy.user.js
10 // ==/UserScript== 11 // ==/UserScript==
11 12
12 ;(function () { 13 'use strict'
13 'use strict'
14 14
15 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/' 15 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/'
16 16
17 const fix = function (link) { 17 function disableEventLink (event) {
18 const href = link.href 18 event.stopPropagation()
19 }
19 20
20 // basic sanity check 21 function fix (link) {
21 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { 22 const href = link.href
22 return
23 }
24 23
25 // don't run again if already run on passed link 24 // basic sanity check
26 if (link._ecUserscript) { 25 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) {
27 return 26 return
28 }
29 link._ecUserscript = true
30
31 link.addEventListener('click', (event) => {
32 event.stopPropagation()
33 })
34 link.innerHTML = `<img style="width: 100%;" src="${href}" />`
35 } 27 }
36 28
37 const onMutate = function (mutations) { 29 // don't run again if already run on passed link
38 for (const mutation in mutations) { 30 if (link._ecUserscript) {
39 run(mutation.addedNodes) 31 return
40 }
41 } 32 }
33 link._ecUserscript = true
42 34
43 // loop through passed nodes (or body if called without arguments) 35 link.addEventListener('click', disableEventLink)
44 const run = function (nodes) { 36 const image = document.createElement('img')
45 if (nodes == null) { 37 image.style.width = '100%'
46 nodes = [document.body] 38 image.src = href
47 } 39 link.replaceChildren(image)
40 }
48 41
49 for (let i = 0; i < nodes.length; i++) { 42 function onMutate (mutations) {
50 // first try fixing itself 43 for (const mutation of mutations) {
51 fix(nodes[i]) 44 for (const node of mutation.addedNodes) {
52 45 if (node instanceof window.HTMLElement) {
53 // and then find all the links inside 46 for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) {
54 const links = nodes[i].querySelectorAll(`[href^="${imageUrlPrefix}"]`) 47 fix(link)
55 48 }
56 for (let j = 0; j < links.length; j++) {
57 fix(links[j])
58 } 49 }
59 } 50 }
60 } 51 }
52 }
61 53
62 // the observer 54 const observer = new window.MutationObserver(onMutate)
63 const observer = new window.MutationObserver(onMutate) 55 observer.observe(document, { childList: true, subtree: true })
64
65 // start the observer
66 observer.observe(document, { childList: true, subtree: true })
67 // initial run on existing document
68 run()
69 }).call()