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-23 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube → SkipCut (Open in New Tab + Instant Redirect)
// @namespace    https://greasyfork.org/users/1197317-opus-x
// @version      1.01
// @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';

    /**
     * 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 a 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, '_blank', 'noopener,noreferrer');
            }
        }
    }, 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);

})();