Redirect YouTube Shorts

Seamlessly redirect YouTube Shorts to the regular video player WITHOUT page reload

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Redirect YouTube Shorts
// @version      2.0.7
// @description  Seamlessly redirect YouTube Shorts to the regular video player WITHOUT page reload
// @run-at       document-start
// @inject-into  page
// @match        https://www.youtube.com/*
// @exclude      https://*.youtube.com/live_chat*
// @exclude      https://*.youtube.com/embed*
// @exclude      https://*.youtube.com/tv*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @author       Fznhq
// @namespace    https://github.com/fznhq
// @homepageURL  https://github.com/fznhq/userscript-collection
// @license      GNU GPLv3
// ==/UserScript==

(function () {
    if (location.pathname.startsWith("/shorts")) {
        return location.replace(location.href.replace("/shorts/", "/watch?v="));
    }

    /**
     * @param {object} obj
     * @param {string} target
     * @returns {any}
     */
    function dig(obj, target) {
        if (obj && typeof obj === "object") {
            if (target in obj && !dig(obj[target], target)) return obj;
            for (const k in obj) {
                const result = dig(obj[k], target);
                if (result !== undefined) return result;
            }
        }
    }

    /**
     * @param {HTMLAnchorElement} element
     * @returns {object | undefined}
     */
    function findShortData(element) {
        while (element && element.tagName !== "YTD-APP") {
            const data = dig(element.data, "reelWatchEndpoint");
            if (data) return data;
            element = element.parentElement;
        }
    }

    /**
     * @param {string} id
     */
    function redirectShorts(id) {
        const elements = document.querySelectorAll(`a[href*="shorts/${id}"]`);

        for (const element of elements) {
            const command = findShortData(element);
            const url = (element.href = `/watch?v=${id}`);

            if (command && command.reelWatchEndpoint.videoId === id) {
                const metadata = dig(command, "url");
                metadata.url = url;
                metadata.webPageType = "WEB_PAGE_TYPE_WATCH";
                command.watchEndpoint = { videoId: id };
                command.reelWatchEndpoint = {};
            }
        }
    }

    const idRegex = /(?:shorts\/|watch\?v=)([^#\&\?]*)/;

    function handleShorts(/** @type {MouseEvent} */ ev) {
        /** @type {HTMLElement} */
        const target = ev.target;

        if (target.closest) {
            const query = "a[href*='/shorts/'], a[href*='/watch?v=']";
            const url = target.closest(query);
            if (url) redirectShorts(url.href.match(idRegex)[1]);
        }
    }

    window.addEventListener("click", handleShorts, true);
    window.addEventListener("mouseover", handleShorts, true);
})();