Block reddit click tracking

Stops reddit from tracking your inbound and outbound clicks

当前为 2018-03-12 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Block reddit click tracking
// @namespace   mjhcfwlmjfzg778evppa995xavvt2nmb
// @description Stops reddit from tracking your inbound and outbound clicks
// @match       *://*.reddit.com/*
// @version     1.1
// @grant       none
// @run-at      document-start
// ==/UserScript==

(function () {
	"use strict";

	var expiredDate = Date.now().toString();


	// Capturing event listener on the outermost element.
	// Runs FIRST, before any others.
	function beforeClick(e) {
		var target = e.target;

		if (target.tagName && target.tagName.toUpperCase() === "A" && target.dataset.hrefUrl) {
			// Remove link tracking attributes
			delete target.dataset.inboundUrl;
			delete target.dataset.outboundUrl;

			// Mark outbound link as expired so reddit code
			// does not try to use it
			if (target.dataset.outboundExpiration) {
				target.dataset.outboundExpiration = expiredDate;
			}
		}
	}


	// Bubbling event listener on the outermost element.
	// Runs LAST, just before the click goes through
	// (unless reddit code adds any others afterwards)
	function justBeforeClick(e) {
		var target = e.target;

		// If reddit event handlers modified the link, change it back
		if (target.tagName && target.tagName.toUpperCase() === "A" && target.dataset.hrefUrl) {
			target.href = target.dataset.hrefUrl;
		}
	}


	// Mark both event listeners as passive so they
	// won't impact scroll performance
	var doCapture = { capture: true,  passive: true };
	var doBubble  = { capture: false, passive: true };

	window.addEventListener("mousedown",  beforeClick, doCapture);
	window.addEventListener("keydown",    beforeClick, doCapture);
	window.addEventListener("touchstart", beforeClick, doCapture);

	window.addEventListener("mousedown",  justBeforeClick, doBubble);
	window.addEventListener("keydown",    justBeforeClick, doBubble);
	window.addEventListener("touchstart", justBeforeClick, doBubble);


	// Patch navigator.sendBeacon() if it is available
	if (typeof Navigator.prototype.sendBeacon === "function") {
		// Fake sendBeacon() function that does nothing
		Navigator.prototype.sendBeacon = function sendBeacon(url) {
			return true;
		};
	}
})();