Block reddit click tracking

Stops reddit from tracking your inbound and outbound clicks

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

  1. // ==UserScript==
  2. // @name Block reddit click tracking
  3. // @namespace mjhcfwlmjfzg778evppa995xavvt2nmb
  4. // @description Stops reddit from tracking your inbound and outbound clicks
  5. // @match *://*.reddit.com/*
  6. // @version 1.1
  7. // @grant none
  8. // @run-at document-start
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. "use strict";
  13.  
  14. var expiredDate = Date.now().toString();
  15.  
  16.  
  17. // Capturing event listener on the outermost element.
  18. // Runs FIRST, before any others.
  19. function beforeClick(e) {
  20. var target = e.target;
  21.  
  22. if (target.tagName && target.tagName.toUpperCase() === "A" && target.dataset.hrefUrl) {
  23. // Remove link tracking attributes
  24. delete target.dataset.inboundUrl;
  25. delete target.dataset.outboundUrl;
  26.  
  27. // Mark outbound link as expired so reddit code
  28. // does not try to use it
  29. if (target.dataset.outboundExpiration) {
  30. target.dataset.outboundExpiration = expiredDate;
  31. }
  32. }
  33. }
  34.  
  35.  
  36. // Bubbling event listener on the outermost element.
  37. // Runs LAST, just before the click goes through
  38. // (unless reddit code adds any others afterwards)
  39. function justBeforeClick(e) {
  40. var target = e.target;
  41.  
  42. // If reddit event handlers modified the link, change it back
  43. if (target.tagName && target.tagName.toUpperCase() === "A" && target.dataset.hrefUrl) {
  44. target.href = target.dataset.hrefUrl;
  45. }
  46. }
  47.  
  48.  
  49. // Mark both event listeners as passive so they
  50. // won't impact scroll performance
  51. var doCapture = { capture: true, passive: true };
  52. var doBubble = { capture: false, passive: true };
  53.  
  54. window.addEventListener("mousedown", beforeClick, doCapture);
  55. window.addEventListener("keydown", beforeClick, doCapture);
  56. window.addEventListener("touchstart", beforeClick, doCapture);
  57.  
  58. window.addEventListener("mousedown", justBeforeClick, doBubble);
  59. window.addEventListener("keydown", justBeforeClick, doBubble);
  60. window.addEventListener("touchstart", justBeforeClick, doBubble);
  61.  
  62.  
  63. // Patch navigator.sendBeacon() if it is available
  64. if (typeof Navigator.prototype.sendBeacon === "function") {
  65. // Fake sendBeacon() function that does nothing
  66. Navigator.prototype.sendBeacon = function sendBeacon(url) {
  67. return true;
  68. };
  69. }
  70. })();