Stop Nefarious Redirects

Block unauthorized redirects

Per 03-05-2024. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==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.');
})();