GitHub Remove Diff Signs

A userscript that removes the "+" and "-" from code diffs

当前为 2016-12-29 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          GitHub Remove Diff Signs
// @version       1.0.2
// @description   A userscript that removes the "+" and "-" from code diffs
// @license       https://creativecommons.org/licenses/by-sa/4.0/
// @namespace     http://github.com/Mottie
// @include       https://github.com/*
// @grant         none
// @run-at        document-idle
// @author        Rob Garrison
// ==/UserScript==
/* jshint unused:true, esnext:true */
(function() {
	"use strict";

	let targets,
		busy = false;

	function processDiff() {
		busy = true;
		if (document.querySelector(".highlight")) {
			let indx = 0,

				els = document.querySelectorAll(".blob-code-deletion .blob-code-inner, .blob-code-addition .blob-code-inner"),
				len = els.length,

				// target "+" and "-" at start
				regex = /^[+-]/,

				// loop with delay to allow user interaction
				loop = function() {
					let el, txt,
						// max number of DOM insertions per loop
						max = 0;
					while ( max < 20 && indx < len ) {
						if (indx >= len) {
							return;
						}
						el = els[indx];
						txt = el.childNodes[0].textContent;
						el.childNodes[0].textContent = txt.replace(regex, " ");
						max++;
						indx++;
					}
					if (indx < len) {
						setTimeout(function(){
							loop();
						}, 200);
					}
				};
			loop();
		}
		busy = false;
	}

	function init() {
		if (document.querySelector("#files.diff-view")) {
			processDiff();
		}
	}

	// DOM targets - to detect GitHub dynamic ajax page loading
	targets = document.querySelectorAll([
		"#js-repo-pjax-container",
		"#js-pjax-container"
	].join(","));

	// update TOC when content changes
	Array.prototype.forEach.call(targets, function(target) {
		new MutationObserver(function(mutations) {
			mutations.forEach(function(mutation) {
				// preform checks before adding code wrap to minimize function calls
				if (!busy && mutation.target === target) {
					init();
				}
			});
		}).observe(target, {
			childList: true,
			subtree: true
		});
	});

	init();

})();