Invidious Injector for YouTube

Injects Invidious embed player instead of YouTube's native one (resistant to SPA reloads and overwrites)

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Invidious Injector for YouTube
// @namespace    https://tampermonkey.net/
// @license      CC-BY-4.0; https://creativecommons.org/licenses/by/4.0/
// @version      1.0
// @description  Injects Invidious embed player instead of YouTube's native one (resistant to SPA reloads and overwrites)
// @author       ilumn
// @match        *://www.youtube.com/watch*
// @grant        none
// ==/UserScript==
// original author: ilumn: https://github.com/ilumn/Invidious-Injector

(function () {
    'use strict';

    const INVIDIOUS_BASE = 'https://inv.nadeko.net';
    let lastInjectedVideoId = null;

    function getVideoId() {
        const params = new URLSearchParams(window.location.search);
        const v = params.get('v');
        return v && v.length === 11 ? v : null;
    }

    function injectIframe(videoId) {
        const playerWrap = document.getElementById('player');
        if (!playerWrap || lastInjectedVideoId === videoId) return;

        // double injection safety
        lastInjectedVideoId = videoId;

        console.log('[InvidiousInjector] Injecting iframe for video:', videoId);

        playerWrap.innerHTML = '';

        const iframe = document.createElement('iframe');
        iframe.src = `${INVIDIOUS_BASE}/embed/${videoId}?autoplay=1`;
        iframe.style.width = '100%';
        iframe.style.height = '100%';
        iframe.style.border = 'none';
        iframe.allow = 'autoplay; encrypted-media';
        iframe.allowFullscreen = true;

        playerWrap.appendChild(iframe);
    }

    function watchForPlayerAndInject() {
        const videoId = getVideoId();
        if (!videoId) return;

        const maxAttempts = 40;
        let attempts = 0;
        const interval = setInterval(() => {
            const player = document.getElementById('player');
            const yVideo = document.querySelector('video');

            if (player && yVideo && yVideo.parentElement.closest('#player')) {
                console.log('[InvidiousInjector] YouTube player found, replacing...');
                player.style.height = "800px"
                clearInterval(interval);
                injectIframe(videoId);
            }

            if (++attempts > maxAttempts) {
                console.warn('[InvidiousInjector] Timed out waiting for player.');
                clearInterval(interval);
            }
        }, 250);
    }

    // detect spa navigations
    let lastUrl = location.href;
    new MutationObserver(() => {
        const currentUrl = location.href;
        if (currentUrl !== lastUrl) {
            lastUrl = currentUrl;
            lastInjectedVideoId = null;
            setTimeout(watchForPlayerAndInject, 500);
        }
    }).observe(document.body, { childList: true, subtree: true });

    window.addEventListener('load', () => {
        setTimeout(watchForPlayerAndInject, 500);
    });
})();