view 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
line wrap: on
line source

// ==UserScript==
// @name        pixiv fanbox no lazy loading image
// @namespace   https://nanaya.net
// @version     2.1.2
// @description Lazy loading is bad for environment. Disable it.
// @author      nanaya
// @match       https://*.fanbox.cc/*
// @grant       none
// @run-at      document-start
// @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/pixiv-fanbox-unlazy.user.js
// ==/UserScript==

'use strict';

const imageUrlPrefix = 'https://downloads.fanbox.cc/images/post/';

function disableEventLink (event) {
  event.stopPropagation();
}

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);
}

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);