YouTube /embed/ forwarder

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

Você precisará instalar uma extensão como Tampermonkey, Greasemonkey ou Violentmonkey para instalar este script.

Você precisará instalar uma extensão como Tampermonkey para instalar este script.

Você precisará instalar uma extensão como Tampermonkey ou Violentmonkey para instalar este script.

Você precisará instalar uma extensão como Tampermonkey ou Userscripts para instalar este script.

Você precisará instalar uma extensão como o Tampermonkey para instalar este script.

Você precisará instalar um gerenciador de scripts de usuário para instalar este script.

(Eu já tenho um gerenciador de scripts de usuário, me deixe instalá-lo!)

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

(Eu já possuo um gerenciador de estilos de usuário, me deixar fazer a instalação!)

// ==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.8
// @grant       none
// @license     whatever
// ==/UserScript==

(function () {
    // Wechsel von nocookie auf die Standard-Embed-Domain, um Player-Konfigurationsfehler (Fehler 153) zu vermeiden
    const EMBED_BASE = 'https://www.youtube.com/embed/';

    // Schutz vor Endlosschleifen, falls wir bereits im Embed-Modus sind
    if (window.location.pathname.startsWith('/embed/')) {
        return;
    }

    // Regex catches watch, embed, v/, shorts/, and ?list=
    const ID_REGEX = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)|(?:[?&]list=)([^&?/]+)/g;

    function parseYouTubeUrl(url) {
        const parsed = new URL(url);
        const params = parsed.searchParams;

        let videoId = null;
        let playlistId = null;

        // Handle youtu.be short links
        if (parsed.hostname === 'youtu.be' && parsed.pathname.length > 1) {
            videoId = parsed.pathname.slice(1);
        }

        let match;
        while ((match = ID_REGEX.exec(url)) !== null) {
            if (match[1]) videoId = match[1];
            if (match[2]) playlistId = match[2];
        }

        // Extract playlist index
        const index = params.has('index') ? parseInt(params.get('index'), 10) : null;

        // Extract start time (from t= or start=)
        let start = null;
        if (params.has('t')) {
            start = parseTime(params.get('t'));
        } else if (params.has('start')) {
            start = parseInt(params.get('start'), 10) || null;
        }

        if (!videoId) return null;

        return playlistId
            ? { type: 'playlist', videoId, playlistId, index, start }
            : { type: 'video', videoId, index, start };
    }

    function parseTime(t) {
        // Wenn es nur aus reinen Zahlen besteht, direkt als Sekunden interpretieren
        if (/^\d+$/.test(t)) {
            return parseInt(t, 10);
        }

        // t kann sein wie "1m30s", "2h3m5s"
        const timeRegex = /(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?/;
        const match = timeRegex.exec(t);
        if (!match || match[0] === '') return null;

        const hours = parseInt(match[1] || 0, 10);
        const minutes = parseInt(match[2] || 0, 10);
        const seconds = parseInt(match[3] || 0, 10);

        return hours * 3600 + minutes * 60 + seconds;
    }

    function createSpoofPage(embedUrl) {
        const html = `
            <html>
                <head>
                    <meta http-equiv="refresh" content="0; url='${embedUrl}'" />
                    <meta name="referrer" content="origin" />
                </head>
                <body>
                    <p>Redirecting to YouTube embed...</p>
                </body>
            </html>
        `;
        const blob = new Blob([html], { type: 'text/html' });
        return URL.createObjectURL(blob);
    }

    const currentUrl = window.location.href;
    const ids = parseYouTubeUrl(currentUrl);
    if (!ids) return;

    let embedUrl;

    if (ids.type === 'playlist') {
        if (ids.index > 200) {
            // Fallback: YouTube embed player won’t load >200, so just load the video
            embedUrl = `${EMBED_BASE}${ids.videoId}?autoplay=1&list=${ids.playlistId}`;
        } else {
            // Preserve playlist and index when possible
            embedUrl = `${EMBED_BASE}${ids.videoId}?list=${ids.playlistId}&autoplay=1`;
            if (ids.index) embedUrl += `&index=${ids.index}`;
        }
    } else {
        // Normal video
        embedUrl = `${EMBED_BASE}${ids.videoId}?autoplay=1`;
    }

    // Add timestamp if available
    if (ids.start) {
        embedUrl += `&start=${ids.start}`;
    }

    // Direkte und saubere Weiterleitung ohne den fehlerhaften Blob-Umweg
    if (embedUrl && embedUrl !== currentUrl) {
        window.location.replace(embedUrl);
    }
})();