Stop Nefarious Redirects

Block unauthorized redirects

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name Stop Nefarious Redirects
// @namespace http://tampermonkey.net/
// @version 3.81 // @description Block unauthorized redirects
// @match http://*/*
// @match https://*/*
// @grant GM_setValue
// @grant GM_getValue
// @license MIT
// @run-at document-start
// @description Block unauthorized redirects
// ==/UserScript==

(function() {
  'use strict';

  // Function to get the current blacklist
  function getBlacklist() {
    return new Set(GM_getValue('blacklist', []));
  }

  // Function to add a URL to the blacklist
  function addToBlacklist(url) {
    let blacklist = getBlacklist();
    if (!blacklist.has(url)) {
      blacklist.add(url);
      GM_setValue('blacklist', Array.from(blacklist));
    }
  }

  // Function to display the blacklist
  function displayBlacklist() {
    let blacklist = getBlacklist();
    console.log('Current Blacklist:\n' + Array.from(blacklist).join('\n'));
  }

  // Debounce function
  function debounce(func, delay) {
    let timeoutId;
    return function(...args) {
      clearTimeout(timeoutId);
      timeoutId = setTimeout(() => func.apply(this, args), delay);
    };
  }

  // Function to handle navigation events
  const handleNavigation = debounce(function(url) {
    try {
      if (!isUrlAllowed(url)) {
        console.error('Blocked navigation to:', url);
        addToBlacklist(url); // Add the unauthorized URL to the blacklist
        if (lastKnownGoodUrl) {
          window.location.replace(lastKnownGoodUrl);
        }
        return false;
      } else {
        console.log('Navigation allowed to:', url);
        lastKnownGoodUrl = url;
        return true;
      }
    } catch (error) {
      console.error('Error in handleNavigation:', error);
    }
  }, 100);

  let lastKnownGoodUrl = window.location.href;
  let navigationInProgress = false;

  // Monitor changes to window.location
  ['assign', 'replace', 'href'].forEach(property => {
    const original = window.location[property];
    if (typeof original === 'function') {
      window.location[property] = function(url) {
        if (!navigationInProgress && handleNavigation(url)) {
          navigationInProgress = true;
          setTimeout(() => {
            navigationInProgress = false;
          }, 0);
          return original.apply(this, arguments);
        }
      };
    } else {
      Object.defineProperty(window.location, property, {
        set: function(url) {
          if (!navigationInProgress && handleNavigation(url)) {
            navigationInProgress = true;
            setTimeout(() => {
              navigationInProgress = false;
            }, 0);
            return Reflect.set(window.location, property, url);
          }
        },
        get: function() {
          return original;
        },
        configurable: true
      });
    }
  });

  // Enhanced navigation control for back/forward buttons
  window.addEventListener('popstate', function(event) {
    if (!navigationInProgress && !isUrlAllowed(window.location.href)) {
      navigationInProgress = true;
      setTimeout(() => {
        navigationInProgress = false;
      }, 0);
      event.preventDefault();
    }
  });

  // Keyboard shortcut listener to display the blacklist
  document.addEventListener('keydown', function(e) {
    if (e.ctrlKey && e.shiftKey && e.key.toLowerCase() === 'l') {
      e.preventDefault();
      displayBlacklist();
    }
  });

  // Function to check if a URL is allowed based on the blacklist
  function isUrlAllowed(url) {
    let blacklist = getBlacklist();
    return !blacklist.has(url);
  }

  console.log('Redirect control script with blacklist initialized.');
})();