无图模式

简单的网页无图模式

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         无图模式
// @namespace
// @include      *
// @version      0.2.2
// @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) {
				if(notNull < 2) console.log('test: src = ' + element.src);
				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)
		}
	}
}

})();