YouTube /embed/ forwarder

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

Fra og med 21.08.2025. Se den nyeste version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

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