无图模式

简单的网页无图模式

当前为 2021-01-15 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         无图模式
// @namespace
// @include      *://movie.douban.com
// @include      *://movie.douban.com/*
// @version      0.1.10
// @description  简单的网页无图模式
// @author       ymzhao
// @namespace 
// ==/UserScript==
;
(function() {
    'use strict';
 
	const clearedTag = '#';

	// 清理图片src
    let clearImgs = function(source) {
		let notNull = 0;
        let elements = document.getElementsByTagName("img");
        Array.prototype.forEach.call(elements, function (element) {
            if(element.src != clearedTag) {
				if(notNull < 2) console.log(element.src, clearedTag);
				element.src = clearedTag;
				notNull++;
			}
        });
		console.log('clear: 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() {
	//console.log('check start');
	let elements = document.getElementsByTagName("img");
	//console.log('check is all cleared: count = ' + count);
	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)
    }
  }
}
})();