YouTube /embed/ forwarder

Forwards YouTube links to the youtube.com/embed/* page, so there's just the video in your window and nothing else.

Verze ze dne 21. 08. 2025. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name        YouTube /embed/ forwarder
// @description Forwards YouTube links to the youtube.com/embed/* page, so there's just the video in your window and nothing else.
// @namespace   https://greasyfork.org/en/users/1148791-vuccala
// @author      Vuccala
// @icon        https://archive.org/download/yt_icon/yt.png
// @match       *://*.youtube.com/*
// @match       *://*.youtu.be/*
// @run-at      document-start
// @version     0.6
// @grant       none
// @license     MIT
// ==/UserScript==
(function () {
    const embedBaseUrl = 'https://www.youtube.com/embed/';

    function getVideoAndPlaylist(url) {
        const videoMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(url);
        const listMatch = /[?&]list=([^&]+)/.exec(url);
        return {
            videoId: videoMatch ? videoMatch[1] : '',
            listId: listMatch ? listMatch[1] : ''
        };
    }

    function ensureReferrerMeta() {
        const head = document.head || document.documentElement;
        if (!head.querySelector('meta[name="referrer"]')) {
            const meta = document.createElement('meta');
            meta.name = 'referrer';
            meta.content = 'origin';
            head.prepend(meta);
        }
    }

    function updateLinks(links) {
        links.forEach(link => {
            const { videoId, listId } = getVideoAndPlaylist(link.href);
            if (videoId) {
                let embedUrl = embedBaseUrl + videoId;
                if (listId) embedUrl += '?list=' + listId;
                link.href = embedUrl;
            }
        });
    }

    function observeDOM() {
        const targetNode = document.body;
        const observer = new MutationObserver(mutationsList => {
            for (const mutation of mutationsList) {
                if (mutation.type === 'childList' && mutation.addedNodes[0]) {
                    const newLinks = mutation.addedNodes[0].querySelectorAll?.(
                        'a[href*="/watch?v="], a[href*="/shorts/"], a[href*="/watch?app=desktop&v="]'
                    );
                    if (newLinks?.length) updateLinks(newLinks);
                }
            }
        });
        observer.observe(targetNode, { childList: true, subtree: true });
    }

    const url = window.location.href;
    const { videoId, listId } = getVideoAndPlaylist(url);

    if (videoId) {
        let embedUrl = embedBaseUrl + videoId;
        if (listId) embedUrl += '?list=' + listId;

        if (embedUrl !== url) {
            ensureReferrerMeta();
            window.location.replace(embedUrl);
        }
    } else {
        observeDOM();
    }
})();