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 για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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";
                }
            });
        }
    });
})();