Greasy Fork

AniWatch - Ultimate Anti-Redirect Shield

Block auto redirections so you can have the viewing pleasure of watching one piece

目前为 2025-04-28 提交的版本。查看 最新版本

// ==UserScript==
// @name         AniWatch - Ultimate Anti-Redirect Shield
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Block auto redirections so you can have the viewing pleasure of watching one piece
// @author       [NotYou]
// @match        https://aniwatchtv.to/watch/*
// @run-at       document-start
// @grant        none
// @license     MIT
// ==/UserScript==

(function() {
    'use strict';

    let userClicked = false;

    // Track user actions (click, keypress, tap)
    const enableRedirectAfterClick = () => {
        userClicked = true;
    };

    window.addEventListener('click', enableRedirectAfterClick, true);
    window.addEventListener('keydown', enableRedirectAfterClick, true);
    window.addEventListener('touchstart', enableRedirectAfterClick, true);

    // Block scripts before they even load
    window.addEventListener('beforescriptexecute', function(e) {
        const scriptText = e.target.innerText || '';
        const scriptSrc = e.target.src || '';

        if (!userClicked) {
            if (scriptText.includes('location.href') ||
                scriptText.includes('window.location') ||
                scriptText.includes('window.open') ||
                scriptSrc.includes('/home') ||
                scriptSrc.includes('aniwatchtv.to/home')) {
                console.warn('[AniWatch Shield] Blocking auto-redirect script:', scriptSrc || '(inline)');
                e.preventDefault();
                e.stopImmediatePropagation();
            }
        }

        // Allow iframe-embed / video player sources automatically
        if (scriptSrc.includes('/iframe-embed/') || scriptSrc.includes('player')) {
            console.log('[AniWatch Shield] Allowing player script:', scriptSrc);
            return; // Let these through
        }

        // Otherwise block weird external scripts early
        if (scriptSrc && !userClicked) {
            if (!scriptSrc.includes('aniwatchtv.to')) {
                console.warn('[AniWatch Shield] Blocking suspicious external script:', scriptSrc);
                e.preventDefault();
                e.stopImmediatePropagation();
            }
        }

    }, true);

    // Protect iframe content as well
    const protectIframe = (iframe) => {
        try {
            const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
            if (iframeDoc) {
                new MutationObserver((mutations) => {
                    for (const mutation of mutations) {
                        for (const node of mutation.addedNodes) {
                            if (node.tagName === 'SCRIPT') {
                                const nodeSrc = node.src || '';
                                if (!nodeSrc.includes('iframe-embed') && !nodeSrc.includes('player')) {
                                    console.warn('[AniWatch Shield] Removing bad script inside iframe');
                                    node.remove();
                                }
                            }
                        }
                    }
                }).observe(iframeDoc, { childList: true, subtree: true });
            }
        } catch (err) {
            console.warn('[AniWatch Shield] Could not protect iframe:', err);
        }
    };

    // Watch for iframe loading
    window.addEventListener('load', () => {
        const iframes = document.querySelectorAll('iframe');
        iframes.forEach(protectIframe);

        new MutationObserver((mutations) => {
            for (const mutation of mutations) {
                for (const node of mutation.addedNodes) {
                    if (node.tagName === 'IFRAME') {
                        protectIframe(node);
                    }
                }
            }
        }).observe(document.body, { childList: true, subtree: true });
    });

})();