Close Reels

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();