简单的网页无图模式
当前为
// ==UserScript==
// @name 无图模式
// @namespace
// @include *
// @version 0.2.1
// @description 简单的网页无图模式
// @author ymzhao
// @namespace
// ==/UserScript==
;
(function() {
'use strict';
const clearedTag = 'IMGNOTFOUND';
// 清理图片src
let clearImgs = function(source) {
let notNull = 0;
let elements = document.getElementsByTagName("img");
Array.prototype.forEach.call(elements, function (element) {
if(element.src != null) {
element.removeAttribute('src')
notNull++;
}
});
console.log('Clear Log: Source = ' + source + ', Count = ' + elements.length + ', Dealed = ' + notNull);
}
// 定时器
let interval = window.setInterval(function(){
if(!isImgsAllCleared) {
clearImgs('Interval');
}
},200);
// 清楚定时器
window.setTimeout(function(){
clearInterval(interval);
},1500);
// 初次清除图片
clearImgs('First');
// 新增DOM时,清除图片
document.addEventListener('DOMNodeInserted', throttle(function() {
if(!isImgsAllCleared()) clearImgs('DOM Inserted');
}, 200));
/**
* 新图判断
*/
function isImgsAllCleared() {
let elements = document.getElementsByTagName("img");
if(elements.length == 0) return true;
return Array.prototype.every.call(elements, function (element) {
element.src == null;
});
}
/**
* 节流
*
* @param fn {Function} 实际要执行的函数
* @param delay {Number} 执行间隔,单位是毫秒(ms)
*
* @return {Function} 返回一个“节流”函数
*/
function throttle(fn, threshhold) {
var last
var timer
threshhold || (threshhold = 250)
return function () {
var context = this
var args = arguments
var now = +new Date()
if (last && now < last + threshhold) {
clearTimeout(timer)
timer = setTimeout(function () {
last = now
fn.apply(context, args)
}, threshhold)
} else {
last = now
fn.apply(context, args)
}
}
}
})();