Greasy Fork

来自缓存

Greasy Fork is available in English.

Redirect YouTube Shorts

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

当前为 2025-06-30 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Redirect YouTube Shorts
// @version      2.0.4
// @description  Seamlessly redirect YouTube Shorts to the regular video player WITHOUT a 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*
// @exclude      https:/tv.youtube.com/*
// @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
     * @param {boolean} returnParent
     * @returns {any}
     */
    function dig(obj, target, returnParent = false) {
        if (obj && typeof obj === "object") {
            if (target in obj && !dig(obj[target], target)) {
                return returnParent ? obj : obj[target];
            }
            for (const k in obj) {
                const result = dig(obj[k], target, returnParent);
                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", true);
            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);

            if (command && command.reelWatchEndpoint.videoId === id) {
                const metadata = dig(command, "url", true);
                metadata.url = `/watch?v=${id}`;
                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);
})();