Stop Nefarious Redirects

Block unauthorized redirects

当前为 2024-05-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Stop Nefarious Redirects
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.80
  5. // @description Block unauthorized redirects
  6. // @match http://*/*
  7. // @match https://*/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @license MIT
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // Function to get the current blacklist
  18. function getBlacklist() {
  19. return GM_getValue('blacklist', []);
  20. }
  21.  
  22. // Function to add a URL to the blacklist
  23. function addToBlacklist(url) {
  24. let blacklist = getBlacklist();
  25. if (!blacklist.includes(url)) {
  26. blacklist.push(url);
  27. GM_setValue('blacklist', blacklist);
  28. }
  29. }
  30.  
  31. // Function to display the blacklist
  32. function displayBlacklist() {
  33. let blacklist = getBlacklist();
  34. alert('Current Blacklist:\n' + blacklist.join('\n'));
  35. }
  36.  
  37. // Function to handle navigation events
  38. function handleNavigation(url) {
  39. if (!isUrlAllowed(url)) {
  40. console.error('Blocked navigation to:', url);
  41. addToBlacklist(url); // Add the unauthorized URL to the blacklist
  42. if (lastKnownGoodUrl) {
  43. window.location.replace(lastKnownGoodUrl);
  44. }
  45. return false;
  46. } else {
  47. console.log('Navigation allowed to:', url);
  48. lastKnownGoodUrl = url;
  49. return true;
  50. }
  51. }
  52.  
  53. let lastKnownGoodUrl = window.location.href;
  54. let navigationInProgress = false;
  55.  
  56. // Monitor changes to window.location
  57. ['assign', 'replace', 'href'].forEach(property => {
  58. const original = window.location[property];
  59. if (typeof original === 'function') {
  60. window.location[property] = function(url) {
  61. if (!navigationInProgress && handleNavigation(url)) {
  62. navigationInProgress = true;
  63. setTimeout(() => {
  64. navigationInProgress = false;
  65. }, 0); // Set to zero to process immediately
  66. return original.apply(this, arguments);
  67. }
  68. };
  69. } else {
  70. Object.defineProperty(window.location, property, {
  71. set: function(url) {
  72. if (!navigationInProgress && handleNavigation(url)) {
  73. navigationInProgress = true;
  74. setTimeout(() => {
  75. navigationInProgress = false;
  76. }, 0); // Set to zero to process immediately
  77. return Reflect.set(window.location, property, url);
  78. }
  79. },
  80. get: function() {
  81. return original;
  82. },
  83. configurable: true
  84. });
  85. }
  86. });
  87.  
  88. // Enhanced navigation control for back/forward buttons
  89. window.addEventListener('popstate', function(event) {
  90. if (!navigationInProgress && !isUrlAllowed(window.location.href)) {
  91. navigationInProgress = true;
  92. setTimeout(() => {
  93. navigationInProgress = false;
  94. }, 0); // Set to zero to process immediately
  95. event.preventDefault();
  96. }
  97. });
  98.  
  99. // Keyboard shortcut listener to display the blacklist
  100. document.addEventListener('keydown', function(e) {
  101. if (e.ctrlKey && e.shiftKey && e.key.toLowerCase() === 'l') {
  102. e.preventDefault();
  103. displayBlacklist();
  104. }
  105. });
  106.  
  107. // Function to check if a URL is allowed based on the blacklist
  108. function isUrlAllowed(url) {
  109. let blacklist = getBlacklist();
  110. return !blacklist.includes(url);
  111. }
  112.  
  113. console.log('Redirect control script with blacklist initialized.');
  114. })();