无图模式

简单的网页无图模式

目前為 2021-01-15 提交的版本,檢視 最新版本

// ==UserScript==
// @name         无图模式
// @namespace
// @include      *
// @version      0.2.0
// @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.indexOf(clearedTag) == -1) {
				element.src = clearedTag;
				// 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 = clearedTag;
	});
}

/**
* 节流
* 
* @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)
		}
	}
}

})();

QingJ © 2025

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