view melonbooks-unlazy.user.js @ 111:055f5d084706

Fix metadata and reset version
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:37:14 +0900
parents ef21ef445fc6
children d9dc190bccaf
line wrap: on
line source

// ==UserScript==
// @name        melonbooks unlazy
// @namespace   https://nanaya.net
// @match       https://www.melonbooks.co.jp/*
// @grant       none
// @run-at      document-start
// @version     1.0.5
// @author      nanaya
// @description replace lazy loaded images with just images
// @downloadURL https://hg.nanaya.net/ec-userscripts/raw-file/tip/melonbooks-unlazy.user.js
// ==/UserScript==

'use strict';

function unlazy () {
  function fix (image) {
    if (!image.classList.contains('lazyload')) return;

    const src = image.dataset.src;

    if (src == null || src === '') return;

    image.classList.remove('lazyload');
    image.src = image.dataset.src;
    delete image.dataset.src;
  }

  function run (node) {
    if (!(node instanceof window.HTMLElement)) return;

    fix(node);
    for (const image of node.querySelectorAll('.lazyload')) {
      fix(image);
    }
  }

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