Nefarious URL Redirect Blocker

Detects and stops URL redirections, loads the original URL, and logs the actions

目前为 2024-04-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         Nefarious URL Redirect Blocker
// @namespace    http://tampermonkey.net/
// @version      1.8
// @description  Detects and stops URL redirections, loads the original URL, and logs the actions
// @match        http://*/*
// @match        https://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Store the original URL
    const originalUrl = window.location.href;

    // Flag to track if the script has been activated
    let scriptActivated = false;

    // Function to log actions
    function logAction(message) {
        console.log(message);
    }

    // Function to handle redirection
    function handleRedirect(event) {
        // Check if the URL has changed
        if (window.location.href !== originalUrl && !scriptActivated) {
            // Set the script activation flag
            scriptActivated = true;

            // Stop the redirection
            event.preventDefault();
            event.stopPropagation();

            // Log the action
            logAction('Redirection stopped.');
        }
    }

    // Function to continuously check for URL changes
    function checkUrlChange() {
        if (window.location.href !== originalUrl && !scriptActivated) {
            // Set the script activation flag
            scriptActivated = true;

            // Push the original URL into the browser history
            window.history.pushState(null, null, originalUrl);

            // Replace the current URL with the original URL
            window.history.replaceState(null, null, originalUrl);

            // Log the action
            logAction('Redirection stopped. Original URL loaded.');
        }

        // Reset the script activation flag
        scriptActivated = false;

        // Schedule the next check
        setTimeout(checkUrlChange, 100);
    }

    // Listen for the beforeunload event (forward direction)
    window.addEventListener('beforeunload', handleRedirect);

    // Listen for the popstate event (backward direction)
    window.addEventListener('popstate', handleRedirect);

    // Start checking for URL changes
    checkUrlChange();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址