Stop Nefarious Redirects

Block unauthorized redirects and prevent history manipulation

// ==UserScript==
// @name         Stop Nefarious Redirects
// @namespace    http://tampermonkey.net/
// @version      4.1
// @description  Block unauthorized redirects and prevent history manipulation
// @match        http://*/*
// @match        https://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// @license      MIT
// @run-at       document-start
// @icon         data:image/webp;base64,UklGRtYgAABXRUJQVlA4IMogAAAwcgCdASr6APoAPlEkj0WjoiGTGp3AOAUEovY/J0n/X41r8jffec1yn4A+lNOK/Jyb5sXQvnw/5/rC8w39Wf1868HmH/cH1lPyq95v999QD+p/9nrVPQA8uv2XP7N/0vTS9QD//7Kb+s3hA/b/7P+SfoT5IfHvrx/Zv/F60HiU6780f5V9rfwP9v/cT8ovwF94HhT6wPUI/EP5T/dPyp/MzkX9t8wj2b+f/4r+9ft//ePix+r/6noz9nfYA/mn9U/0/5ufHH/L8PT7l/zPYG/mH9l/4/+C/dj/VfIl/i/37/PfsL7hvz3/Ef8f/Gfkj9g38h/on+v/vP+f/+H+J////t+672MfuB7G/6wf7/8/zU+FhwutbNoVMwoIgad1rZtCo9+/akXKTZk4xQW75YTb68Gnd18CvCIdUgP3Ue2rXhfolVf6IzXCA4FO53C4dkpy+f/QTlEsDgqJhNCJQfYVfFaE4o/S/G8ID4w536WXxRjpUc21BiEY9TKC6Dr9iBO1uwY9+AhLrG9ATHhQ/tYQ6nHSvyrCwNNKtVszfDhtWBAjtcG581jGmi0WAjxv1ub5CMCPedmd3kWcCX9T1SJ5zB//kYQRPB+yf98rHsvK8rJJib9ZTb1ctOde2pEv9gCQsQzuKvGEf4kjds9N75IvxwTElXSiinJwR9CxVy6r9zhYcAC2RlaYG/1xY5TRmpt/07xDc/qrV6JbaLEswYKK+4Dh/2pQlVfDmLIL8VkAz+y6XNYD9OmzH/v36BAMwsSAAB4Ziu0kErELHpsXlUJ+7TEFFV7DRtrQUv3hBA25reJIncHUia4k/wbvGfR4jJ6GACLc2u6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/nM8bv2t6Dz6kezO8MRsWTsN18m+aPNEVlxm+pbKp4tVuszE5uxqhnDI04qLD+xZ38mn0lP8Eg820fFCFzA2iVG5ra+zYoRmJcR2Vf4se1PRuiAUfkqCX9TjOSpw93RTMIN6C0mIwI9IMpJ3vkaBWm1u2UiwC/q5WsP5Kud5WFGifBnHL8jNgQOExHU2qlqYMt9YrxB4weRcsY4Q7/HL9+HokZ343SDwgb5jDHlTvMVCk6nOXNsPqfCuy9gDAIYJT/yBGeEcdilavOiBTVSAb6/SV6svoZ29fHZ64y3Cd9gqpteduTOn4UT44MIHVQFMbZV0yDXcK+Y+eAtsV2lxLL0BwlpHGGaENcqYXH8XbB8pWv6ntK9yOGydjyxPwXRE4qlXVzEkox89M2CQ1oslBgMVRPH96yt0y7eHYT5B5cAAHdMeVeGninMQ7ju/DNWMyfOQfr4sM1eLvCBt9ojl6M62zW6B06QpxaTpBX+rwFt0+d0rR/usP7x6QO8Xd0+FrxEFUi/DQaUevMcGoBSfUHxu0TWjnyVy3k/0l54oHymSzXs102CZK+mOwMnnjIsDpF4m2ykxDv6ObpgBfQADEl+cgAABQpv8PalEjUPQ3AUg4LwoPVw9i/zCdMJf1NQPeE5Jyy8wAAA=
// ==/UserScript==

const manualBlacklist = new Set([
    'getrunkhomuto.info'
]);

// List of allowed popups domains (user should add specific domains here as needed)
const allowedSites = new Set([
    '500px.com', 'accuweather.com', 'adobe.com', 'alibaba.com', 'amazon.com',
    'apple.com', 'bbc.com', 'bing.com', 'cnn.com', 'craigslist.org',
    'dailymail.co.uk', 'ebay.com', 'facebook.com', 'github.com', 'google.com',
    'instagram.com', 'linkedin.com', 'microsoft.com', 'netflix.com', 'reddit.com',
    'twitter.com', 'wikipedia.org', 'youtube.com'
]);

const logPrefix = '[Nefarious Redirect Blocker]';

(function() {
    'use strict';

    console.log(`${logPrefix} Script initialization started.`);

    function getAutomatedBlacklist() {
        return new Set(GM_getValue('blacklist', []));
    }

    function addToAutomatedBlacklist(url) {
        const encodedUrl = encodeURIComponent(url);
        const blacklist = getAutomatedBlacklist();
        if (!blacklist.has(encodedUrl)) {
            blacklist.add(encodedUrl);
            GM_setValue('blacklist', Array.from(blacklist));
            console.log(`${logPrefix} Added to automated blacklist:`, url);
        }
    }

    function isNavigationAllowed(url) {
        if (!isUrlBlocked(url)) {
            console.log(`${logPrefix} Navigation allowed to:`, url);
            lastKnownGoodUrl = url;
            return true;
        } else {
            console.error(`${logPrefix} Blocked navigation to:`, url);
            addToAutomatedBlacklist(url);
            if (lastKnownGoodUrl) {
                window.location.replace(lastKnownGoodUrl);
            }
            return false;
        }
    }

    const originalOpen = window.open;

    console.log(`${logPrefix} Original window.open saved.`);

    window.open = function(url, name, features) {
        console.log(`${logPrefix} Popup attempt detected:`, url);
        if (Array.from(allowedSites).some(domain => url.includes(domain)) || isNavigationAllowed(url)) {
            console.log(`${logPrefix} Popup allowed for:`, url);
            return originalOpen(url, name, features);
        }
        console.log(`${logPrefix} Blocked a popup from:`, url);
        return null;
    };

    console.log(`${logPrefix} window.open overridden with custom logic.`);

    let lastKnownGoodUrl = window.location.href;

    function interceptNavigation(event) {
        const url = event.detail.url;
        if (!isNavigationAllowed(url)) {
            event.preventDefault();
            return false;
        }
        return true;
    }

    window.addEventListener('beforeunload', function(event) {
        if (!isNavigationAllowed(window.location.href)) {
            event.preventDefault();
            event.returnValue = '';
            return false;
        }
    });

    window.addEventListener('popstate', function(event) {
        if (!isNavigationAllowed(window.location.href)) {
            console.error(`${logPrefix} Blocked navigation to:`, window.location.href);
            history.pushState(null, "", lastKnownGoodUrl);
            window.location.replace(lastKnownGoodUrl);
            event.preventDefault();
        }
    });

    function handleHistoryManipulation(originalMethod, data, title, url) {
        if (!isUrlBlocked(url)) {
            return originalMethod.call(history, data, title, url);
        }
        console.error(`${logPrefix} Blocked history manipulation to:`, url);
    }

    const originalPushState = history.pushState;
    const originalReplaceState = history.replaceState;

    history.pushState = function(data, title, url) {
        return handleHistoryManipulation(originalPushState, data, title, url);
    };

    history.replaceState = function(data, title, url) {
        return handleHistoryManipulation(originalReplaceState, data, title, url);
    };

    function isUrlBlocked(url) {
        const encodedUrl = encodeURIComponent(url);
        const automatedBlacklist = getAutomatedBlacklist();
        const isBlocked = [...manualBlacklist, ...automatedBlacklist].some(blockedUrl => encodedUrl.includes(blockedUrl));
        if (isBlocked) {
            console.log(`${logPrefix} Blocked URL:`, url);
        }
        return isBlocked;
    }

    console.log(`${logPrefix} Redirect control script with blacklist initialized.`);
})();