Fetch lazy-load images immediately at document load
当前为
// ==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
// @license MIT License
// @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});
}