Robust Popup Blocker with Whitelist

Blocks all popups unless they originate from a whitelisted domain, with enhanced handling.

目前為 2024-05-03 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Robust Popup Blocker with Whitelist
// @namespace    http://tampermonkey.net/
// @version      2.2
// @description  Blocks all popups unless they originate from a whitelisted domain, with enhanced handling.
// @author       Your Name
// @match        *://*/*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const trustedWebsites = [
        '500px.com', 'accuweather.com', 'adobe.com', 'adulttime.com', 'alibaba.com', 'amazon.com',
        'amazonaws.com', 'amd.com', 'americanexpress.com', 'anaconda.com', 'angular.io', 'ap.org',
        'apache.org', 'apnews.com', 'apple.com', 'arstechnica.com', 'artstation.com', 'asana.com',
        'asus.com', 'atlassian.com', 'autodesk.com', 'axios.com', 'battle.net', 'bbc.co.uk', 'bbc.com',
        'behance.net', 'bestbuy.com', 'bing.com', 'bitbucket.org', 'blogger.com', 'bloomberg.com',
        'bmw.com', 'boeing.com', 'booking.co.uk', 'booking.com', 'bootstrapcdn.com', 'breitbart.com',
        'buzzfeed.com', 'canva.com', 'capitalone.com', 'cbsnews.com', 'character.ai', 'chase.com',
        'chaturbate.com', 'cisco.com', 'citi.com', 'cnbc.com', 'cnet.com', 'cnn.com', 'codecademy.com',
        'constantcontact.com', 'coursera.org', 'craigslist.org', 'dailymail.co.uk', 'dell.com',
        'deviantart.com', 'discord.com', 'disney.com', 'django.com', 'docker.com', 'docusign.com',
        'dribbble.com', 'dropbox.com', 'duckduckgo.com', 'duolingo.com', 'duosecurity.com', 'ebay.com',
        'economist.com', 'edx.org', 'elsevier.com', 'engadget.com', 'epicgames.com', 'eporner.com',
        'espn.com', 'etsy.com', 'eurogamer.net', 'expedia.com', 'facebook.com', 'fandom.com', 'fedex.com',
        'figma.com', 'finance.yahoo.com', 'flickr.com', 'flipkart.com', 'forbes.com', 'foxnews.com',
        'framer.com', 'freecodecamp.org', 'gamespot.com', 'gartner.com', 'gettyimages.com', 'git-scm.com',
        'github.com', 'gizmodo.com', 'go.com', 'godaddy.com', 'gog.com', 'goldmansachs.com', 'google.com',
        'healthline.com', 'hilton.com', 'homedepot.com', 'hp.com', 'hubspot.com', 'huffpost.com',
        'hulu.com', 'humblebundle.com', 'ibm.com', 'ieee.org', 'ifixit.com', 'ign.com', 'ikea.com',
        'imdb.com', 'imgur.com', 'indeed.com', 'instagram.com', 'instructure.com', 'intel.com',
        'intuit.com', 'invisionapp.com', 'itch.io', 'java.com', 'jetbrains.com', 'joomla.org',
        'jquery.com', 'khanacademy.org', 'kotaku.com', 'kotlinlang.org', 'laravel.com', 'lenovo.com',
        'lg.com', 'lifehacker.com', 'linkedin.com', 'live.com', 'lowes.com', 'lynda.com', 'macys.com',
        'mailchimp.com', 'marriott.com', 'mashable.com', 'masterclass.com', 'mcdonalds.com', 'medium.com',
        'mercedes-benz.com', 'microsoft.com', 'microsoftonline.com','mit.edu', 'mongodb.com', 'moodle.org',
        'mozilla.org', 'msn.com', 'msnbc.com', 'nasa.gov', 'nationalgeographic.com', 'nbc.com', 'nbcnews.com',
        'netflix.com', 'nextdoor.com', 'nih.gov', 'npr.org', 'nvidia.com','nypost.com', 'nytimes.com',
        'office.com', 'okta.com', 'onlyfans.com', 'openai.com', 'oracle.com', 'oreilly.com', 'origin.com',
        'outlook.com', 'overstock.com', 'patreon.com', 'paypal.com', 'pcgamer.com', 'pexels.com', 'php.net',
        'pinterest.com', 'pixabay.com', 'pluralsight.com', 'polygon.com', 'pornhub.com', 'python.org',
        'quizlet.com', 'quora.com', 'reactjs.org', 'realtor.com', 'reddit.com', 'redhat.com', 'roblox.com',
        'rubyonrails.org', 'salesforce.com', 'samsung.co.kr', 'samsung.com', 'sap.com', 'sciencedirect.com',
        'scopus.com', 'sears.com', 'sharepoint.com', 'shutterstock.com', 'siemens.com', 'sketch.com',
        'skillshare.com', 'skype.com', 'slack.com', 'sony.com', 'soundcloud.com', 'spotify.com', 'spring.io',
        'stackoverflow.com', 'steamcommunity.com', 'steampowered.com', 'surveymonkey.com', 'symantec.com',
        'target.com', 'techcrunch.com', 'temu.com', 'tesla.com', 'texasinstruments.com', 'theguardian.com',
        'thenextweb.com', 'theverge.com', 'tiktok.com', 'time.com', 'toyota.com', 'trello.com', 'trip.com',
        'tripadvisor.com', 'tumblr.com', 'twitch.tv', 'twitter.com', 'uber.com', 'ucla.edu', 'ucsf.edu',
        'udemy.com', 'unity.com', 'unsplash.com', 'ups.com', 'usatoday.com', 'usnews.com', 'usps.com',
        'verizon.com', 'vice.com', 'Vice.com', 'vimeo.com', 'vk.com', 'vmware.com', 'volkswagen.com', 'vox.com',
        'walmart.com', 'washingtonpost.com', 'weather.com', 'weather.gov', 'webmd.com', 'whatsapp.com',
        'wikimedia.org', 'wikipedia.org', 'wired.com', 'wordpress.com', 'wsj.com', 'wunderground.com', 'x.com',
        'xerox.com', 'xfinity.com', 'xhamster.com', 'xilinx.com', 'xnxx.com', 'xvideos.com', 'yahoo.com',
        'yelp.com', 'youtube.com', 'zapier.com', 'zendesk.com', 'zeplin.io','zillow.com', 'zoom.us', 'new.reddit.com'
    ];

    function isWhitelisted(url) {
        try {
            const hostname = new URL(url).hostname;
            const whitelisted = trustedWebsites.some(domain => {
                if (hostname === domain) {
                    return true;
                }
                const domainParts = hostname.split('.');
                const domainPartsLength = domainParts.length;
                const trustedDomainParts = domain.split('.');
                const trustedDomainPartsLength = trustedDomainParts.length;
                return domainParts.slice(-trustedDomainPartsLength).join('.') === domain;
            });
            console.log('Whitelist check for', url, ':', whitelisted);
            return whitelisted;
        } catch (e) {
            console.error('Error checking whitelist:', e);
            return false;
        }
    }

    // Override window.open
    const originalOpen = window.open;
    window.open = function(url, name, features) {
        console.log('window.open called with:', url);
        if (!url || isWhitelisted(url)) {
            console.log('Allowing popup for:', url);
            return originalOpen.call(this, url, name, features);
        } else {
            console.log('Blocked popup from:', url);
            return null;
        }
    };

    // Override document.write
    const originalDocumentWrite = document.write;
    document.write = function(content) {
        if (content && content.includes('window.open')) {
            console.log('Blocked popup triggered by document.write');
            return;
        }
        return originalDocumentWrite.apply(this, arguments);
    };

    // Block popups triggered by target="_blank"
    document.addEventListener('click', function(event) {
        if (event.target.tagName === 'A' && event.target.getAttribute('target') === '_blank') {
            const href = event.target.getAttribute('href');
            if (href && !isWhitelisted(href)) {
                event.preventDefault();
                console.log('Blocked popup triggered by target="_blank" with href:', href);
            }
        }
    }, true);

    // Block popups triggered by dynamic iframe creation
    const originalCreateElement = document.createElement;
    document.createElement = function(tagName) {
        if (tagName.toLowerCase() === 'iframe') {
            console.log('Blocked popup triggered by dynamic iframe creation');
            return null;
        }
        return originalCreateElement.apply(this, arguments);
    };

    // Block popups triggered by form submission
    document.addEventListener('submit', function(event) {
        const form = event.target;
        const action = form.getAttribute('action');
        if (action && !isWhitelisted(action)) {
            event.preventDefault();
            console.log('Blocked popup triggered by form submission with action:', action);
        }
    }, true);

    // Block popups triggered by mouse events
    const blockMouseEvent = function(event) {
        if (event.button === 0 || event.button === 1) {
            const target = event.target;
            if (target.tagName === 'A' && target.getAttribute('target') === '_blank') {
                const href = target.getAttribute('href');
                if (href && !isWhitelisted(href)) {
                    event.preventDefault();
                    console.log('Blocked popup triggered by mouse event with href:', href);
                }
            }
        }
    };
    document.addEventListener('click', blockMouseEvent, true);
    document.addEventListener('mousedown', blockMouseEvent, true);
    document.addEventListener('mouseup', blockMouseEvent, true);

    // Block popups triggered by static iframes with specific src attributes
    const blockStaticIframe = function(iframe) {
        const src = iframe.getAttribute('src');
        if (src && (src.startsWith('javascript:') || src.startsWith('data:'))) {
            iframe.setAttribute('src', 'about:blank');
            console.log('Blocked popup triggered by static iframe with src:', src);
        }
    };
    const iframes = document.getElementsByTagName('iframe');
    for (let i = 0; i < iframes.length; i++) {
        blockStaticIframe(iframes[i]);
    }

    // Block popups triggered by cross-origin iframes
    const blockCrossOriginIframe = function(iframe) {
        const src = iframe.getAttribute('src');
        if (src && !isWhitelisted(src)) {
            iframe.setAttribute('src', 'about:blank');
            console.log('Blocked popup triggered by cross-origin iframe with src:', src);
        }
    };
    for (let i = 0; i < iframes.length; i++) {
        blockCrossOriginIframe(iframes[i]);
    }

    // Block access to contentWindow and contentDocument properties
    const originalContentWindow = Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, 'contentWindow');
    const originalContentDocument = Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, 'contentDocument');
    Object.defineProperties(HTMLIFrameElement.prototype, {
        contentWindow: {
            get: function() {
                const src = this.getAttribute('src');
                if (src && !isWhitelisted(src)) {
                    console.log('Blocked access to contentWindow for iframe with src:', src);
                    return null;
                }
                return originalContentWindow.get.call(this);
            }
        },
        contentDocument: {
            get: function() {
                const src = this.getAttribute('src');
                if (src && !isWhitelisted(src)) {
                    console.log('Blocked access to contentDocument for iframe with src:', src);
                    return null;
                }
                return originalContentDocument.get.call(this);
            }
        }
    });

    // Block popups triggered by base element with target="_blank"
    const baseElement = document.querySelector('base[target="_blank"]');
    if (baseElement) {
        baseElement.removeAttribute('target');
        console.log('Removed target="_blank" from base element');
    }

    console.log('Popup blocker initialized.');
})();

QingJ © 2025

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