Redirect YouTube Shorts to Watch

Redirect /shorts to /watch

目前為 2023-06-10 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Redirect YouTube Shorts to Watch
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  Redirect /shorts to /watch
// @author       CY Fung
// @license      MIT
// @run-at       document-start
// @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 = '';

    /**
     * 
     * @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();
            }
            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...
})();