view pixiv-fanbox-unlazy.user.js @ 89:47ae8c7673cb

Add japan times account wall remover
author nanaya <me@nanaya.pro>
date Sat, 04 Dec 2021 04:08:25 +0900
parents 9c8cde985caf
children 74c33632c353
line wrap: on
line source

// ==UserScript==
// @name         pixiv fanbox no lazy loading image
// @namespace    https://myconan.net
// @version      2.0.2
// @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
// ==/UserScript==

;(function () {
  'use strict'

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

  const fix = function (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', (event) => {
      event.stopPropagation()
    })
    link.innerHTML = `<img style="width: 100%;" src="${href}" />`
  }

  const onMutate = function (mutations) {
    for (const mutation in mutations) {
      run(mutation.addedNodes)
    }
  }

  // loop through passed nodes (or body if called without arguments)
  const run = function (nodes) {
    if (nodes == null) {
      nodes = [document.body]
    }

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

  // 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()