forked from ywzhaiqi/userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
noLazyImageLoad.user.js | ||
======================= | ||
|
||
取消图片的延迟加载 。参考 [nolazyload](https://greasyfork.org/scripts/791-nolazyload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// ==UserScript== | ||
// @name No Lazy Image load | ||
// @namespace https://github.com/ywzhaiqi/ | ||
// @version 0.1 | ||
// @description 取消图片的延迟加载 | ||
// @include http* | ||
// @grant none | ||
// ==/UserScript== | ||
|
||
var lazyAttributes = [ | ||
"zoomfile", "file", "original", "load-src", "_src", "imgsrc", "real_src", "src2", "origin-src", | ||
"data-lazyload", "data-lazyload-src", "data-lazy-load-src", | ||
"data-ks-lazyload", "data-ks-lazyload-custom", | ||
"data-src", "data-defer-src", "data-actualsrc", | ||
"data-cover", "data-original", "data-thumb", "data-imageurl", "data-placeholder", | ||
]; | ||
|
||
function noLazyNode(node) { | ||
if (node.localName != 'img') return; | ||
|
||
lazyAttributes.some(function(attr) { | ||
if (!node.hasAttribute(attr)) return; | ||
|
||
var newSrc = node.getAttribute(attr); | ||
if (node.src != newSrc) { | ||
node.src = newSrc; | ||
} | ||
return true; | ||
}); | ||
} | ||
|
||
function addMutationObserver(selector, callback) { | ||
var watch = document.querySelector(selector); | ||
if (!watch) return; | ||
|
||
var observer = new MutationObserver(function(mutations){ | ||
mutations.forEach(function(x) { | ||
if (x.addedNodes.length) { | ||
callback(x.addedNodes); | ||
} | ||
}); | ||
}); | ||
observer.observe(watch, {childList: true, subtree: true}); | ||
} | ||
|
||
function run() { | ||
[].map.call(document.images, noLazyNode); | ||
|
||
addMutationObserver('body', function(addedNodes) { | ||
[].map.call(addedNodes, noLazyNode) | ||
}); | ||
} | ||
|
||
run(); |