view soranews-image.user.js @ 114:817b62848b27

Backed out changeset 055f5d084706
author nanaya <me@nanaya.net>
date Sun, 15 Jan 2023 23:43:40 +0900
parents ef21ef445fc6
children d9dc190bccaf
line wrap: on
line source

// ==UserScript==
// @name         Soranews image fix
// @namespace    https://nanaya.net
// @version      1.0.3
// @description  Soranews lazy load image fix
// @author       nanaya
// @match        https://soranews24.com/*
// @grant        none
// @downloadURL  https://hg.nanaya.net/ec-userscripts/raw-file/tip/soranews-image.user.js
// ==/UserScript==

'use strict';

function unlazy () {
  const fix = function (image) {
    // basic sanity check
    if (!image.classList.contains('lazy')) {
      return;
    }

    image.classList.remove('lazy');
    image.removeAttribute('src');
    image.setAttribute('srcset', image.dataset.scoSrcset);
  };

  // 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++) {
      // find all the images inside (and self)
      const images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')];

      for (let j = 0; j < images.length; j++) {
        fix(images[j]);
      }
    }
  };

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

  // the observer
  const observer = new window.MutationObserver(onMutate);

  // start the observer
  observer.observe(document, { childList: true, subtree: true });
  // initial run on existing document
  run();
}

unlazy();