view soranews-image.user.js @ 84:af4f8b25495e

Merge elements first and use actual srcset
author nanaya <me@nanaya.pro>
date Thu, 15 Apr 2021 14:56:23 +0900
parents 86da34e62d29
children 9c8cde985caf
line wrap: on
line source

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

;(function () {
  'use strict'

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

    for (var i = 0; i < nodes.length; i++) {
      // find all the images inside (and self)
      var images = [nodes[i], ...nodes[i].querySelectorAll('.lazy')]

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

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

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

  // the observer
  var observer = new window.MutationObserver(onMutate)

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