Redirect YouTube Shorts to Watch

Redirect /shorts to /watch

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Redirect YouTube Shorts to Watch
// @namespace    http://tampermonkey.net/
// @version      0.1.3
// @description  Redirect /shorts to /watch
// @author       CY Fung
// @license      MIT
// @run-at       document-start
// @match        https://*.youtube.com/*
// @match        http://*.youtube.com/*
// @match        https://youtube.com/*
// @match        http://youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @unwrap
// @noframes
// @inject-into page
// ==/UserScript==

(function () {
    'use strict';

    let lastPathname = '';
    let lastId = '';

    let lastRedirection = '';
    let lastRedirectionCache = sessionStorage.getItem('v5Nmolo6');
    if (typeof lastRedirectionCache === 'string') {
        let s = lastRedirectionCache.split('|');
        if (s.length === 2) {
            let lastRedirectionTime = +s[1];
            if (Date.now() - lastRedirectionTime < 2300) {
                lastRedirection = s[0];
            }
        }
    }

    /**
     *
     * @param {Event?} evt
     */
    function checkRedirect(evt) {
        let pathname = location.pathname;
        if (lastPathname !== pathname) {
            let id = '';
            if (pathname && pathname.startsWith('/shorts')) {
                let m = /\/shorts\/([\w\-\_\+\=]+)/.exec(pathname)
                if (m) {
                    id = m[1];
                }
            }
            lastPathname = pathname;
            lastId = id;
        }
        let id = lastId;
        if (id) {
            if (evt) {
                evt.stopPropagation();
                evt.stopImmediatePropagation();
            }
            if (lastRedirection !== id) {
                lastRedirection = id;
                sessionStorage.setItem('v5Nmolo6', id + '|' + Date.now());
                location.replace('/watch?v=' + id);
            }
        }
    }

    for (const s of ['yt-navigate', 'yt-navigate-start', 'yt-page-data-fetched', 'yt-page-data-updated', 'yt-navigate-finish']) {
        document.addEventListener(s, checkRedirect, true);
    }
    checkRedirect();


    // Your code here...
})();