view soranews-image.user.js @ 77:5a3a269c5e6f

Update downloadurl
author nanaya <me@nanaya.pro>
date Wed, 08 Jul 2020 20:45:05 +0900
parents 0aa28e02e257
children 86da34e62d29
line wrap: on
line source

// ==UserScript==
// @name         Soranews image fix
// @namespace    https://myconan.net
// @version      1.0.1
// @description  Soranews lazy load image fix
// @author       nanaya
// @match        https://soranews24.com/*
// @grant        none
// @downloadURL  https://hg.sr.ht/~nanaya/ec-userscripts/raw/soranews-image.user.js?rev=tip
// ==/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++) {
      // first try fixing itself
      fix(nodes[i])

      // and then find all the images inside
      var images = 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.scoSrc)
  }

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