Stop Nefarious Redirects

Block unauthorized redirects

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

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

(function() {
    'use strict';

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

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

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

    // Function to handle navigation events
    function handleNavigation(url) {
        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;
        }
    }

    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); // Set to zero to process immediately
                    return original.apply(this, arguments);
                }
            };
        } else {
            Object.defineProperty(window.location, property, {
                set: function(url) {
                    if (!navigationInProgress && handleNavigation(url)) {
                        navigationInProgress = true;
                        setTimeout(() => {
                            navigationInProgress = false;
                        }, 0); // Set to zero to process immediately
                        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); // Set to zero to process immediately
            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.includes(url);
    }

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