reddit: /r/watchpeopledie censor bypass

Bypasses the Germany block on /r/watchpeopledie

当前为 2019-03-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name reddit: /r/watchpeopledie censor bypass
  3. // @namespace xk265z9sx26le7jte9rtz5h8su6adofn
  4. // @description Bypasses the Germany block on /r/watchpeopledie
  5. // @license MIT License
  6. // @match *://*.reddit.com/r/watchpeopledie/*
  7. // @match *://*.reddit.com//r/watchpeopledie/*
  8. // @version 1.0
  9. // @grant none
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. /* jshint bitwise: true, curly: true, eqeqeq: true, esversion: 6,
  14. funcscope: false, futurehostile: true, latedef: true, noarg: true,
  15. nocomma: true, nonbsp: true, nonew: true, notypeof: false,
  16. shadow: outer, singleGroups: true, strict: true, undef: true,
  17. unused: true, varstmt: true, browser: true */
  18.  
  19. (function () {
  20. "use strict";
  21.  
  22. const prefixFrom = /^\/r\/watchpeopledie\//;
  23. const prefixTo = "//r/watchpeopledie/";
  24.  
  25. // Returns the rewritten URL, or the original URL
  26. // if nothing was changed.
  27. const rewriteURL = function (url) {
  28. let rewrite;
  29.  
  30. try {
  31. rewrite = new URL(url);
  32. rewrite.pathname = rewrite.pathname.replace(prefixFrom, prefixTo);
  33. } catch (e) {
  34. return url;
  35. }
  36.  
  37. return rewrite.href;
  38. };
  39.  
  40.  
  41. if (prefixFrom.test(location.pathname)) {
  42. document.documentElement.hidden = true;
  43. location.replace(rewriteURL(location.href));
  44. } else {
  45. // Generate random string
  46. let random = "";
  47.  
  48. {
  49. const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  50. const charCount = chars.length;
  51.  
  52. for (let i = 0; i < 16; i++) {
  53. random += chars.charAt(Math.floor(Math.random() * charCount));
  54. }
  55. }
  56.  
  57. const attribName = "data-wpdprocessed-" + random;
  58. const selector = "a:not([" + attribName + "]),form:not([" + attribName + "])";
  59.  
  60.  
  61. const rewriteElement = function (element) {
  62. let tagName = element.tagName.toUpperCase();
  63. let attribute;
  64.  
  65. if (tagName === "A") {
  66. attribute = "href";
  67. } else if (tagName === "FORM") {
  68. attribute = "action";
  69. } else {
  70. return;
  71. }
  72.  
  73. const oldURL = element[attribute];
  74. const newURL = rewriteURL(oldURL);
  75.  
  76. if (newURL !== oldURL) {
  77. element[attribute] = newURL;
  78. }
  79.  
  80. // Mark this element as done so our selector won't find it again
  81. if (!element.hasAttribute(attribName)) {
  82. element.setAttribute(attribName, "");
  83. }
  84. };
  85.  
  86.  
  87. const rewriteElementsInside = function (root) {
  88. for (const element of root.querySelectorAll(selector)) {
  89. rewriteElement(element);
  90. }
  91. };
  92.  
  93.  
  94. window.addEventListener("DOMContentLoaded", () => {
  95. rewriteElementsInside(document);
  96.  
  97. new MutationObserver((mutations) => {
  98. for (const mutation of mutations) {
  99. if (mutation.type === "attributes") {
  100. // We intentionally don't check for
  101. // the special attribute here.
  102. // This may be called on elements that
  103. // were already rewritten, but then
  104. // had their href/action changed.
  105. rewriteElement(mutation.target);
  106. } else if (mutation.addedNodes.length) {
  107. rewriteElementsInside(mutation.target);
  108. }
  109. }
  110. }).observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ["href", "action"] });
  111. }, { capture: true });
  112. }
  113. })();