# HG changeset patch # User nanaya # Date 1671884916 -32400 # Node ID 74c33632c353e68b7206dc45153305c029ecd7c3 # Parent b44d5cb661c5e7a72559b5f2dc13052402e45624 Simplify script diff -r b44d5cb661c5 -r 74c33632c353 pixiv-fanbox-unlazy.user.js --- a/pixiv-fanbox-unlazy.user.js Sat Dec 24 06:55:55 2022 +0900 +++ b/pixiv-fanbox-unlazy.user.js Sat Dec 24 21:28:36 2022 +0900 @@ -1,69 +1,55 @@ // ==UserScript== // @name pixiv fanbox no lazy loading image // @namespace https://myconan.net -// @version 2.0.2 +// @version 2.1.0 // @description Lazy loading is bad for environment. Disable it. // @author nanaya // @match https://*.fanbox.cc/* // @grant none -// @downloadURL https://hg.myconan.net/ec-userscripts/raw-file/tip/pixiv-fanbox-unlazy.user.js +// @run-at document-start +// @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/pixiv-fanbox-unlazy.user.js // ==/UserScript== -;(function () { - 'use strict' +'use strict' - const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/' - - const fix = function (link) { - const href = link.href +const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/' - // 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 - } - link._ecUserscript = true +function fix (link) { + const href = link.href - link.addEventListener('click', (event) => { - event.stopPropagation() - }) - link.innerHTML = `` + // basic sanity check + if (typeof href !== 'string' || !href.startsWith(imageUrlPrefix)) { + return } - const onMutate = function (mutations) { - for (const mutation in mutations) { - run(mutation.addedNodes) - } + // don't run again if already run on passed link + if (link._ecUserscript) { + return } + link._ecUserscript = true - // loop through passed nodes (or body if called without arguments) - const run = function (nodes) { - if (nodes == null) { - nodes = [document.body] - } + link.addEventListener('click', disableEventLink) + const image = document.createElement('img') + image.style.width = '100%' + image.src = href + link.replaceChildren(image) +} - for (let i = 0; i < nodes.length; i++) { - // first try fixing itself - fix(nodes[i]) - - // and then find all the links inside - const links = nodes[i].querySelectorAll(`[href^="${imageUrlPrefix}"]`) - - for (let j = 0; j < links.length; j++) { - fix(links[j]) +function onMutate (mutations) { + for (const mutation of mutations) { + for (const node of mutation.addedNodes) { + if (node instanceof window.HTMLElement) { + for (const link of node.querySelectorAll(`[href^="${imageUrlPrefix}"]`)) { + fix(link) + } } } } - - // the observer - const observer = new window.MutationObserver(onMutate) +} - // start the observer - observer.observe(document, { childList: true, subtree: true }) - // initial run on existing document - run() -}).call() +const observer = new window.MutationObserver(onMutate) +observer.observe(document, { childList: true, subtree: true })