Behance - fetch lazy-load images immediately

Fetch lazy-load images immediately at document load

目前為 2015-04-27 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Behance - fetch lazy-load images immediately
// @description Fetch lazy-load images immediately at document load
// @include     https://www.behance.net/*
// @version     1.0.4
// @namespace   wOxxOm.scripts
// @author      wOxxOm
// @run-at      document-start
// ==/UserScript==

window.addEventListener('DOMContentLoaded', function(e) {
  processNodes(null, document.querySelectorAll('.js-picture-lazy'));
  setMutationHandler(document.body, '.js-picture-lazy', processNodes);
});

function processNodes(observer, nodes) {
  for (var i=0, len=nodes.length, n, img; i<len && (n=nodes[i]); i++) {
    if (img = n.querySelector('img')) {
      img.src = img.dataset.src;
      img.removeAttribute('width');
      img.removeAttribute('height');
      img.removeAttribute('style');
    }
    var picture = document.createElement('picture');
    while (n.firstElementChild)
      picture.appendChild(n.removeChild(n.firstElementChild));
    n.parentNode.replaceChild(picture, n);
    n.remove();
  }
}

function setMutationHandler(baseNode, selector, cb) {
  var ob = new MutationObserver(function(mutations){
    for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
      for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
        if (n.nodeType == 1) 
          if ((n = n.matches(selector) ? [n] : n.querySelectorAll(selector)) && n.length)
            if (!cb(ob, n))
              return;
  });
  ob.observe(baseNode, {subtree:true, childList:true}); 
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址