Mercurial > ec-userscripts
comparison pixiv-fanbox-unlazy.user.js @ 85:9c8cde985caf
Update standard and fix
author | nanaya <me@nanaya.pro> |
---|---|
date | Thu, 15 Apr 2021 15:01:22 +0900 |
parents | 86da34e62d29 |
children | 74c33632c353 |
comparison
equal
deleted
inserted
replaced
84:af4f8b25495e | 85:9c8cde985caf |
---|---|
10 // ==/UserScript== | 10 // ==/UserScript== |
11 | 11 |
12 ;(function () { | 12 ;(function () { |
13 'use strict' | 13 'use strict' |
14 | 14 |
15 var imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/' | 15 const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/' |
16 | 16 |
17 // loop through passed nodes (or body if called without arguments) | 17 const fix = function (link) { |
18 var run = function (nodes) { | 18 const href = link.href |
19 if (nodes == null) { | |
20 nodes = [document.body] | |
21 } | |
22 | |
23 for (var i = 0; i < nodes.length; i++) { | |
24 // first try fixing itself | |
25 fix(nodes[i]) | |
26 | |
27 // and then find all the links inside | |
28 var links = nodes[i].querySelectorAll(`[href^="${imageUrlPrefix}"]`) | |
29 | |
30 for (var j = 0; j < links.length; j++) { | |
31 fix(links[j]) | |
32 } | |
33 } | |
34 } | |
35 | |
36 var fix = function (link) { | |
37 var href = link.href | |
38 | 19 |
39 // basic sanity check | 20 // basic sanity check |
40 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { | 21 if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { |
41 return | 22 return |
42 } | 23 } |
51 event.stopPropagation() | 32 event.stopPropagation() |
52 }) | 33 }) |
53 link.innerHTML = `<img style="width: 100%;" src="${href}" />` | 34 link.innerHTML = `<img style="width: 100%;" src="${href}" />` |
54 } | 35 } |
55 | 36 |
56 var onMutate = function (mutations) { | 37 const onMutate = function (mutations) { |
57 for (var mutation in mutations) { | 38 for (const mutation in mutations) { |
58 run(mutation.addedNodes) | 39 run(mutation.addedNodes) |
59 } | 40 } |
60 } | 41 } |
61 | 42 |
43 // loop through passed nodes (or body if called without arguments) | |
44 const run = function (nodes) { | |
45 if (nodes == null) { | |
46 nodes = [document.body] | |
47 } | |
48 | |
49 for (let i = 0; i < nodes.length; i++) { | |
50 // first try fixing itself | |
51 fix(nodes[i]) | |
52 | |
53 // and then find all the links inside | |
54 const links = nodes[i].querySelectorAll(`[href^="${imageUrlPrefix}"]`) | |
55 | |
56 for (let j = 0; j < links.length; j++) { | |
57 fix(links[j]) | |
58 } | |
59 } | |
60 } | |
61 | |
62 // the observer | 62 // the observer |
63 var observer = new window.MutationObserver(onMutate) | 63 const observer = new window.MutationObserver(onMutate) |
64 | 64 |
65 // start the observer | 65 // start the observer |
66 observer.observe(document, { childList: true, subtree: true }) | 66 observer.observe(document, { childList: true, subtree: true }) |
67 // initial run on existing document | 67 // initial run on existing document |
68 run() | 68 run() |