YouTube → SkipCut (Open in New Tab + Instant Redirect)

Open YouTube watch/live/playlist links in a new SkipCut tab and instantly redirect direct loads.

目前為 2025-08-24 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube → SkipCut (Open in New Tab + Instant Redirect)
// @namespace    https://greasyfork.org/users/1197317-opus-x
// @version      1.03
// @description  Open YouTube watch/live/playlist links in a new SkipCut tab and instantly redirect direct loads.
// @author       Opus-X
// @license      MIT
// @icon         https://www.skipcut.com/favicon.ico
// @match        https://www.youtube.com/*
// @run-at       document-start
// ==/UserScript==

(function () {
    'use strict';

    const TARGET_TAB_NAME = 'SkipCutTab';

    /**
     * Build the matching SkipCut URL for a given YouTube URL
     * @param {string} url
     * @returns {string|null}
     */
    function getSkipCutUrl(url) {
        const u = new URL(url);
        let newUrl = null;

        if (u.pathname === '/watch' && u.searchParams.has('v')) {
            newUrl = `https://www.skipcut.com/watch?v=${u.searchParams.get('v')}`;
        }
        else if (u.pathname.startsWith('/live/')) {
            const liveId = u.pathname.split('/')[2];
            if (liveId) {
                newUrl = `https://www.skipcut.com/live/${liveId}`;
            }
        }
        else if (u.pathname === '/playlist' && u.searchParams.has('list')) {
            newUrl = `https://www.skipcut.com/playlist?list=${u.searchParams.get('list')}`;
        }

        return newUrl;
    }

    /**
     * Instantly redirect current tab to SkipCut if current URL matches
     * Called on page load and SPA navigations.
     */
    function tryDirectRedirect() {
        const redirectUrl = getSkipCutUrl(location.href);
        if (redirectUrl) {
            window.stop(); // Stop YouTube from loading anything
            location.replace(redirectUrl);
            return true;
        }
        return false;
    }

    // Check immediately at document-start for direct loads
    if (tryDirectRedirect()) return;

    /**
     * Open a SkipCut link in the same new tab and prevent YouTube SPA navigation.
     * Intercepts clicks before YouTube handles them.
     */
    document.addEventListener('click', (e) => {
        const link = e.target.closest('a');
        if (!link) return;

        // Match relevant YouTube video/playlist/live links
        if (link.href.includes('/watch') || link.href.includes('/live/') || link.href.includes('/playlist')) {
            const redirectUrl = getSkipCutUrl(link.href);
            if (redirectUrl) {
                e.preventDefault();
                e.stopImmediatePropagation(); // Block YouTube SPA navigation
                window.open(redirectUrl, TARGET_TAB_NAME);
            }
        }
    }, true);

    /**
     * Handle SPA navigations triggered internally by YouTube.
     * If the URL changes directly (e.g. typing or script), redirect instantly.
     */
    function handleSpaNavigation() {
        const redirectUrl = getSkipCutUrl(location.href);
        if (redirectUrl) {
            window.stop();
            location.replace(redirectUrl);
        }
    }

    // Patch History API to detect SPA navigations instantly
    const origPushState = history.pushState;
    const origReplaceState = history.replaceState;

    history.pushState = function (...args) {
        origPushState.apply(this, args);
        handleSpaNavigation();
    };

    history.replaceState = function (...args) {
        origReplaceState.apply(this, args);
        handleSpaNavigation();
    };

    window.addEventListener('popstate', handleSpaNavigation);

    // YouTube-specific SPA hooks (extra safety net)
    window.addEventListener('yt-navigate-start', handleSpaNavigation, true);
    window.addEventListener('yt-page-data-updated', handleSpaNavigation, true);

})();