annotate pixiv-fanbox-unlazy.user.js @ 123:87442313317d

Apparently tampermonkey requires run-at ?_?
author nanaya <me@nanaya.net>
date Sat, 21 Jan 2023 02:00:59 +0900
parents 8de2d53a4cb1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
78
9dc1be1a6c7a Bye image lazy loading on pixiv fanbox
nanaya <me@nanaya.pro>
parents:
diff changeset
1 // ==UserScript==
118
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
2 // @name pixiv fanbox no lazy loading image
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 2.1.2
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
5 // @description Lazy loading is bad for environment. Disable it.
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://*.fanbox.cc/*
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 // @run-at document-start
8de2d53a4cb1 Fix metadata
nanaya <me@nanaya.net>
parents: 117
diff changeset
10 // @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/pixiv-fanbox-unlazy.user.js
78
9dc1be1a6c7a Bye image lazy loading on pixiv fanbox
nanaya <me@nanaya.pro>
parents:
diff changeset
11 // ==/UserScript==
9dc1be1a6c7a Bye image lazy loading on pixiv fanbox
nanaya <me@nanaya.pro>
parents:
diff changeset
12
108
2c4470b73ad9 Switch to semistandard
nanaya <me@nanaya.net>
parents: 104
diff changeset
13 'use strict';
78
9dc1be1a6c7a Bye image lazy loading on pixiv fanbox
nanaya <me@nanaya.pro>
parents:
diff changeset
14
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
15 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/';
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
16
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
17 function disableEventLink (event) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
18 event.stopPropagation();
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
19 }
78
9dc1be1a6c7a Bye image lazy loading on pixiv fanbox
nanaya <me@nanaya.pro>
parents:
diff changeset
20
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
21 function fix (link) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
22 const href = link.href;
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
23
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
24 // basic sanity check
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
25 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
26 return;
78
9dc1be1a6c7a Bye image lazy loading on pixiv fanbox
nanaya <me@nanaya.pro>
parents:
diff changeset
27 }
9dc1be1a6c7a Bye image lazy loading on pixiv fanbox
nanaya <me@nanaya.pro>
parents:
diff changeset
28
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
29 // don't run again if already run on passed link
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
30 if (link._ecUserscript) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
31 return;
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
32 }
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
33 link._ecUserscript = true;
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
34
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
35 link.addEventListener('click', disableEventLink);
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
36 const image = document.createElement('img');
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
37 image.style.width = '100%';
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
38 image.src = href;
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
39 link.replaceChildren(image);
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
40 }
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
41
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
42 function run (node) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
43 if (!(node instanceof window.HTMLElement)) return;
78
9dc1be1a6c7a Bye image lazy loading on pixiv fanbox
nanaya <me@nanaya.pro>
parents:
diff changeset
44
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
45 fix(node);
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
46 for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
47 fix(link);
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
48 }
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
49 }
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
50
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
51 function onMutate (mutations) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
52 for (const mutation of mutations) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
53 for (const node of mutation.addedNodes) {
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
54 run(node);
109
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
55 }
ef21ef445fc6 Cleanups
nanaya <me@nanaya.net>
parents: 108
diff changeset
56 }
100
74c33632c353 Simplify script
nanaya <me@nanaya.net>
parents: 85
diff changeset
57 }
85
9c8cde985caf Update standard and fix
nanaya <me@nanaya.pro>
parents: 82
diff changeset
58
117
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
59 const observer = new window.MutationObserver(onMutate);
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
60 observer.observe(document, { childList: true, subtree: true });
d9dc190bccaf Undo combining fixes into single script per site
nanaya <me@nanaya.net>
parents: 109
diff changeset
61 run(document.body);