Mercurial > ec-userscripts
diff pixiv-fanbox-unlazy.user.js @ 109:ef21ef445fc6
Cleanups
- wrap everything in function and call them last
- fix namespace and update url
author | nanaya <me@nanaya.net> |
---|---|
date | Sun, 15 Jan 2023 23:33:41 +0900 |
parents | 2c4470b73ad9 |
children | d9dc190bccaf |
line wrap: on
line diff
--- a/pixiv-fanbox-unlazy.user.js Sun Jan 15 23:24:03 2023 +0900 +++ b/pixiv-fanbox-unlazy.user.js Sun Jan 15 23:33:41 2023 +0900 @@ -1,6 +1,6 @@ // ==UserScript== // @name pixiv fanbox no lazy loading image -// @namespace https://myconan.net +// @namespace https://nanaya.net // @version 2.1.2 // @description Lazy loading is bad for environment. Disable it. // @author nanaya @@ -12,50 +12,54 @@ 'use strict'; -const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/'; - -function disableEventLink (event) { - event.stopPropagation(); -} +function unlazy () { + const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/'; -function fix (link) { - const href = link.href; - - // basic sanity check - if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { - return; + function disableEventLink (event) { + event.stopPropagation(); } - // don't run again if already run on passed link - if (link._ecUserscript) { - return; + function fix (link) { + const href = link.href; + + // basic sanity check + if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { + return; + } + + // don't run again if already run on passed link + if (link._ecUserscript) { + return; + } + link._ecUserscript = true; + + link.addEventListener('click', disableEventLink); + const image = document.createElement('img'); + image.style.width = '100%'; + image.src = href; + link.replaceChildren(image); } - link._ecUserscript = true; - link.addEventListener('click', disableEventLink); - const image = document.createElement('img'); - image.style.width = '100%'; - image.src = href; - link.replaceChildren(image); + function run (node) { + if (!(node instanceof window.HTMLElement)) return; + + fix(node); + for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) { + fix(link); + } + } + + function onMutate (mutations) { + for (const mutation of mutations) { + for (const node of mutation.addedNodes) { + run(node); + } + } + } + + const observer = new window.MutationObserver(onMutate); + observer.observe(document, { childList: true, subtree: true }); + run(document.body); } -function run (node) { - if (!(node instanceof window.HTMLElement)) return; - - fix(node); - for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) { - fix(link); - } -} - -function onMutate (mutations) { - for (const mutation of mutations) { - for (const node of mutation.addedNodes) { - run(node); - } - } -} - -const observer = new window.MutationObserver(onMutate); -observer.observe(document, { childList: true, subtree: true }); -run(document.body); +unlazy();