Refined Popup Blocker

Differentiates between good and bad pop-ups by monitoring user interactions and dynamically added content. Aggressively blocks unwanted pop-ups while allowing user-initiated ones. Prompts user to blacklist sites when pop-ups are blocked. Press Alt+0 to remove the site and its subdomains from both the whitelist and blacklist.

These are versions of this script where the code was updated. Show all versions.

  • v2.7.5 2024-05-27
  • v2.7.4 2024-05-27

    Changes Between the Old Script and the Updated Script

    Overview

    The updated script includes several changes and improvements over the old version. These updates enhance the functionality, especially concerning how popups are handled, and introduce a mechanism for managing a whitelist and blacklist of websites. Below are the detailed changes:

    1. Introduction of Blacklist and Whitelist

    Old Script

    • Only a whitelist was used to manage allowed websites.
    • Whitelisted websites were pre-defined and stored locally.
    • Users were prompted to add a website to the whitelist if it was not already on the list.

    Updated Script

    • Introduces both a blacklist and a whitelist.
    • Blacklist: Websites on this list will have their popups blocked.
    • Whitelist: Websites on this list are allowed to show popups, and it takes precedence over the blacklist.
    • Prompts the user to add websites to the blacklist when a popup is blocked.
    • If the user selects "Cancel," the website is added to the whitelist.

    2. Persistent Storage with GM_getValue and GM_setValue

    Old Script

    • Whitelist was managed using localStorage.

    Updated Script

    • Uses Tampermonkey's GM_getValue and GM_setValue for persistent storage of both the blacklist and whitelist.

    3. Handling Subdomains

    Old Script

    • Whitelisted websites were managed as-is without specific handling for subdomains.

    Updated Script

    • When a website is added to the blacklist or whitelist, subdomains are also considered.
    • On removing a website from the lists, all subdomains are also removed.

    4. User Prompts

    Old Script

    • When a popup was blocked, users were prompted to add the site to the whitelist.

    Updated Script

    • When a popup is blocked, users are prompted to add the site to the blacklist.
    • If the user selects "Cancel," the site is added to the whitelist instead.

    5. Improved Event Handling

    Old Script

    • Basic event listeners for click events were in place to manage popups.

    Updated Script

    • Listens for user-initiated actions (click, submit, keydown) to determine if a popup should be allowed.
    • Handles Alt+0 keypress to remove the current website and its subdomains from both the blacklist and whitelist.

    6. Verbose Logging

    Old Script

    • Logging was implemented for various actions.

    Updated Script

    • Maintains and extends verbose logging for better debugging and tracking of actions.

    Detailed Code Changes

    Persistent Storage Changes

    function getBlacklistedWebsites() {
        try {
            return new Set(JSON.parse(GM_getValue('blacklistedWebsites', '[]')));
        } catch (e) {
            log('Error parsing blacklist from storage: ' + e);
            return new Set();
        }
    }
    
    function getWhitelistedWebsites() {
        try {
            return new Set(JSON.parse(GM_getValue('whitelistedWebsites', '[]')));
        } catch (e) {
            log('Error parsing whitelist from storage: ' + e);
            return new Set();
        }
    }
    
    function saveBlacklistedWebsites(blacklistedWebsites) {
        GM_setValue('blacklistedWebsites', JSON.stringify([...blacklistedWebsites]));
        log('Blacklist saved');
    }
    
    function saveWhitelistedWebsites(whitelistedWebsites) {
        GM_setValue('whitelistedWebsites', JSON.stringify([...whitelistedWebsites]));
        log('Whitelist saved');
    }
    

    Prompt Changes

    function blockPopup(url) {
        try {
            const parsedUrl = new URL(url, location.origin);
            const hostname = parsedUrl.hostname;
            log('Blocked popup: ' + url);
            if (!blacklistedWebsites.has(hostname) && !whitelistedWebsites.has(hostname) && !promptedWebsites.has(hostname)) {
                promptedWebsites.add(hostname);
                setTimeout(() => {
                    if (confirm(`A popup from ${hostname} was blocked. Do you want to block pop-ups from this site in the future?`)) {
                        addToBlacklist(hostname);
                        location.reload();
                    } else {
                        addToWhitelist(hostname);
                        location.reload();
                    }
                }, 0);
            }
        } catch (e) {
            log('Error blocking popup: ' + e);
        }
    }
    

    Event Handling for Removing Sites from Lists

    document.addEventListener('keydown', (event) => {
        if (event.altKey && event.key === '0') {
            const hostname = location.hostname;
            let { foundInBlacklist, foundInWhitelist } = removeFromLists(hostname);
    
            if (foundInBlacklist || foundInWhitelist) {
                alert(`${hostname} and its subdomains have been removed from the ${foundInBlacklist ? 'blacklist' : ''} ${foundInWhitelist ? 'whitelist' : ''}.`);
            } else {
                let subdomainFound = false;
                blacklistedWebsites.forEach(site => {
                    if (hostname.endsWith(site) || site.endsWith(hostname)) {
                        blacklistedWebsites.delete(site);
                        subdomainFound = true;
                    }
                });
    
                whitelistedWebsites.forEach(site => {
                    if (hostname.endsWith(site) || site.endsWith(hostname)) {
                        whitelistedWebsites.delete(site);
                        subdomainFound = true;
                    }
                });
    
                saveBlacklistedWebsites(blacklistedWebsites);
                saveWhitelistedWebsites(whitelistedWebsites);
    
                if (subdomainFound) {
                    alert(`Subdomains of ${hostname} were found and removed.`);
                } else {
                    alert(`${hostname} was not found in the blacklist or whitelist, and no subdomains were found.`);
                }
            }
        }
    });
    

    Summary

    The updated script provides a more robust mechanism for managing popups by introducing a blacklist and a whitelist, improving persistent storage, and handling subdomains more effectively. Additionally, it refines the user prompts and event handling to provide a smoother user experience.

  • v2.7.3 2024-05-25
  • v2.7.2 2024-05-08
  • v2.7.1 2024-05-08
  • v2.7 2024-05-08
  • v2.6 2024-05-06

    Refined Popup Blocker - Script Modification Documentation

    This document outlines the modifications made to the "Refined Popup Blocker" Tampermonkey script. The original script was designed to block unwanted popups by only allowing popups from a predefined list of trusted websites. The modification enhances the script's functionality by refining its criteria to better distinguish between unwanted popups and legitimate navigational links, particularly those commonly found in emails and newsletters.

    Original Script Functionality

    The original script operates as follows:

    • It maintains a list of trusted websites. Any popup initiated from these websites is allowed.
    • The script overrides the standard window.open method to block all popups that initiate from non-trusted domains.
    • It adds event listeners to the document that prevent default actions for popups, particularly targeting links that open in a new tab (target="_blank").

    Problems Identified in the Original Script

    The original script was blocking legitimate links that were mistakenly not recognized as coming from trusted sources. Specifically, links embedded within emails or newsletters that redirect through tracking URLs (often not directly listed in the trusted domains) were being blocked.

    Modifications Made

    Refined Link Blocking Criteria

    A key modification was made to the event listeners handling clicks on links opening in new tabs:

    • The script now checks if the link's URL contains the substring 'track/click'. This pattern is common in email campaign links and other legitimate redirections which should not be blocked.

    Here is the updated block of code for handling link clicks:

    document.addEventListener('click', function(event) {
        let target = event.target;
        while (target && target.tagName !== 'A') {
            target = target.parentNode;
        }
        if (target && target.tagName === 'A' && target.getAttribute('target') === '_blank') {
            const href = target.getAttribute('href');
            // Check if the URL contains 'track/click', suggesting a legitimate link
            if (href && (!isWhitelisted(href) && !href.includes('track/click'))) {
                event.preventDefault();
                console.log('Blocked popup triggered by target="_blank" with href:', href);
            }
        }
    }, true);
    

    Benefits of the Modification

    This adjustment provides the following benefits:

    • Reduces False Positives: Legitimate links that redirect through tracking URLs are no longer incorrectly blocked.
    • Improves User Experience: Users can now follow links from trusted sources, such as newsletters, without disruption.
    • Maintains Security: The script continues to block truly unwanted popups, maintaining a secure browsing experience.

    Conclusion

    The modifications to the Refined Popup Blocker script improve its ability to differentiate between unwanted popups and legitimate navigational links. This enhancement ensures that the script better serves its purpose without hindering user interaction with valid links.

  • v2.5 2024-05-03

    Removed a depreciated code method that is soon to be completely removed from the chrome browser. This makes it necessary or eventually the script will cease to function anymore.

  • v2.4 2024-05-03
  • v2.3 2024-05-03

    Fixed live-streaming videos being blocked among other issues that could arise from the code as it were.
    Fixed the whitelist not being utilized by the script correctly.

  • v2.2 2024-05-03

    Robust Popup Blocker with Whitelist - Update Summary

    Version 2.2 of the "Robust Popup Blocker with Whitelist" script brings several enhancements and improvements to provide better protection against unwanted popups. The key changes in this update include:

    Enhanced Whitelist Functionality

    • Improved the isWhitelisted function to perform a more precise comparison between the hostname and the trusted domains.
    • The function now splits both the hostname and the trusted domain into parts and compares the corresponding parts from the end, ensuring that the entire trusted domain matches the end of the hostname.
    • This enhancement fixes false negatives for domains like "openai.com" and ensures that subdomains of whitelisted domains are properly allowed.

    Blocking Popups Triggered by document.write

    • Added an override for document.write to block popups triggered by document.write that include window.open.
    • When document.write is called with content that includes window.open, the script now logs a message indicating that a popup triggered by document.write has been blocked.

    Blocking Popups Triggered by target="_blank"

    • Implemented an event listener for the click event to block popups triggered by target="_blank" on anchor elements.
    • When a click event occurs on an anchor element with target="_blank" and a non-whitelisted href URL, the script prevents the default behavior and logs a message indicating that a popup triggered by target="_blank" has been blocked.

    Blocking Popups Triggered by Dynamic Iframe Creation

    • Overridden document.createElement to block popups triggered by dynamic iframe creation.
    • When document.createElement is called with the tag name "iframe", the script logs a message indicating that a popup triggered by dynamic iframe creation has been blocked and returns null.

    Blocking Popups Triggered by Form Submission

    • Added an event listener for the submit event to block popups triggered by form submission with non-whitelisted action URLs.
    • When a form is submitted with a non-whitelisted action URL, the script prevents the default behavior and logs a message indicating that a popup triggered by form submission has been blocked.

    Blocking Popups Triggered by Mouse Events

    • Implemented event listeners for click, mousedown, and mouseup events to block popups triggered by mouse events on anchor elements with target="_blank" and non-whitelisted href URLs.
    • When a mouse event occurs on an anchor element with target="_blank" and a non-whitelisted href URL, the script prevents the default behavior and logs a message indicating that a popup triggered by a mouse event has been blocked.

    Blocking Popups Triggered by Static Iframes

    • Added a function blockStaticIframe to block popups triggered by static iframes with src attributes containing javascript: or data:.
    • When a static iframe with a src attribute containing javascript: or data: is encountered, the script sets the src attribute to about:blank and logs a message indicating that a popup triggered by a static iframe has been blocked.

    Blocking Popups Triggered by Cross-Origin Iframes

    • Implemented a function blockCrossOriginIframe to block popups triggered by cross-origin iframes with non-whitelisted src URLs.
    • When a cross-origin iframe with a non-whitelisted src URL is encountered, the script sets the src attribute to about:blank and logs a message indicating that a popup triggered by a cross-origin iframe has been blocked.

    Blocking Access to contentWindow and contentDocument

    • Overridden the contentWindow and contentDocument properties of HTMLIFrameElement.prototype to block access to these properties for iframes with non-whitelisted src URLs.
    • When accessing contentWindow or contentDocument of an iframe with a non-whitelisted src URL, the script logs a message indicating that access has been blocked and returns null.

    Blocking Popups Triggered by base Element with target="_blank"

    • Added a check for the presence of a base element with target="_blank".
    • If a base element with target="_blank" is found, the script removes the target attribute and logs a message indicating that target="_blank" has been removed from the base element.

    These enhancements aim to provide a more comprehensive and effective popup blocking solution, covering various techniques websites may use to bypass blockers. By updating to version 2.2, users can benefit from improved protection against unwanted popups while still allowing popups from trusted domains specified in the whitelist.

    We encourage all users to update to the latest version of the "Robust Popup Blocker with Whitelist" script to take advantage of these improvements and ensure a safer browsing experience.

  • v1.2 2024-04-17
  • v1.1 2024-04-17

    Updated list of whitelisted websites

  • v1.0 2024-04-15