YouTube Unshortifier

Redirects YouTube Shorts to regular pages.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        YouTube Unshortifier
// @match       https://www.youtube.com/*
// @grant       none
// @version     1.0
// @author      lucasm
// @namespace   luluco250.YouTubeUnshortifier
// @license     CC0
// @description Redirects YouTube Shorts to regular pages.
// ==/UserScript==

(() => {
    "use strict";

    const shortsLinkRegex = /\/shorts\/([A-z0-9_-]+)/;

    function isObject(value) {
        return !!value && typeof value === "object" && !Array.isArray(value);
    }

    function forObjectInArray(array, callback) {
        for (const item of array) {
            if (!item || typeof item !== "object") continue;

            if (Array.isArray(item)) {
                forObjectInArray(item, callback);
                continue;
            }

            callback(item);
        }
    }

    /**
     * Recursively look for a property of a specific name in an object.
     * If it is found, it is returned as an array of the object that contains it and its value.
     * Otherwise it returns null.
     */
    function forEachProp(obj, propName, callback) {
        for (const [key, value] of Object.entries(obj)) {
            if (key === propName) {
                callback(obj, value);
                continue;
            }

            if (!value || typeof value !== "object") continue;

            if (Array.isArray(value)) {
                forObjectInArray(value, x => forEachProp(x, propName, callback));
                continue;
            }

            forEachProp(value, propName, callback);
        }
    }

    function tryGetShortId(uri) {
        if (!uri) return null;
        const match = uri.match(shortsLinkRegex);
        return match === null ? null : match[1];
    }

    function tryUnshortify(uri, callback) {
        const id = tryGetShortId(uri);
        if (id === null) {
            return false;
        }

        console.log(`Unshortifying ${id}`);
        callback(`/watch?v=${id}`);
        return true;
    }

    function unshortifyMany(anchorNodes) {
        if (!anchorNodes) {
            return;
        }

        for (const a of anchorNodes) {
            const href = a.getAttribute("href");
            tryUnshortify(href, x => a.setAttribute("href", x));
        }
    }

    new MutationObserver(mutations => {
        unshortifyMany(mutations.map(x => x.target));
    }).observe(document.body, {
        subtree: true,
        attributeFilter: ["href"],
    });

    window.addEventListener("load", () => {
        console.log("YouTube Unshortifier initialized.");
        console.log("Unshortifying existing anchors...");
        unshortifyMany(document.getElementsByTagName("a"));

        if (window.hasOwnProperty("ytInitialData")) {
            forEachProp(ytInitialData, "url", (obj, url) => {
                if (tryUnshortify(url, x => obj.url = x)) {
                    obj.webPageType = "WEB_PAGE_TYPE_WATCH";
                }
            });
        }
    });
})();