Close Reels

Closes tab when URL contains "/reel/" including dynamic changes

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Close Reels
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Closes tab when URL contains "/reel/" including dynamic changes
// @match        *://*facebook.com/*
// @run-at       document-start
// @author harvastum
// @license MIT
// ==/UserScript==


    'use strict';

    let isClosing = false;
    const targetPath = '/reel/';

    function checkAndClose() {
        if (isClosing) return;

        if (window.location.href.toLowerCase().includes(targetPath)) {
            isClosing = true;
            console.log('Closing reel tab:', window.location.href);
            window.close();
            // Add slight delay for better reliability
            setTimeout(() => {
                window.close();
                // Fallback if close fails
                if (!window.closed) {
                    window.close()
                    window.location.href = 'about:blank';
                    window.close()
                }
            }, 100);
        }
    }

    // Monitor history API changes
    const originalPushState = history.pushState;
    const originalReplaceState = history.replaceState;

    history.pushState = function() {
        originalPushState.apply(this, arguments);
        checkAndClose();
    };

    history.replaceState = function() {
        originalReplaceState.apply(this, arguments);
        checkAndClose();
    };

    // Monitor various navigation events
    window.addEventListener('popstate', checkAndClose);
    window.addEventListener('hashchange', checkAndClose);
    window.addEventListener('load', checkAndClose);
    window.addEventListener('spalocationchange', checkAndClose);

    // Monitor DOM changes as last resort
    const observer = new MutationObserver(checkAndClose);
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true
    });

    // Initial check
    checkAndClose();