Invidious (yewtu.be) embed

Replace YouTube embeds with yewtu.be embeds.

目前為 2022-09-07 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Invidious (yewtu.be) embed
// @description Replace YouTube embeds with yewtu.be embeds.
// @author      Backend & SkauOfArcadia
// @homepage https://skau.neocities.org/
// @contactURL https://t.me/SkauOfArcadia
// @include     *
// @exclude *://*.youtube.com/*
// @exclude *://*.google.com/*
// @exclude *://turntable.fm/*
// @exclude *://web.archive.org/web/*
// @exclude *://*/embed/*
// @exclude *://*/watch?v=*
// @inject-into content
// @version     2022.09
// @grant       none
// @allFrames   true
// @namespace https://greasyfork.org/users/751327
// ==/UserScript==
(function() {
    "use strict";
    const instance = "yewtu.be";
    const a = -1; //defines autoplay value on the initial page load
    const b = -1; //defines autoplay for embedded videos that appear on page interaction
    // -1 = will only autoplay if the embed has the "autoplay=1" parameter
    // 0 = disables autoplay
    // 1 = enables autoplay
    const dash = true; //defines if the quality=dash parameter will be used

    var observer = new MutationObserver(mutate);
    observer.observe(document, {
        childList: true,
        attributes: true,
        subtree: true
    });

    function mutate() {
        go(b);
    }

    function go(auto) {
        let frames = document.body.querySelectorAll('iframe[src], embed[src]');
        frames = Object.values(frames).filter(youtubeiFrame);

        for (let i = 0; i < frames.length; i++) {
            let frame = frames[i];
            let invid = frame.src;
            let params = new URLSearchParams();
            if (invid.indexOf('?') !== -1) {
                params = new URLSearchParams(invid.substring(invid.indexOf('?') + 1));
                invid = invid.split('?')[0];
            } else if (invid.indexOf('&') !== -1) {
                params = new URLSearchParams(invid.substring(invid.indexOf('&') + 1));
                invid = invid.split('&')[0];
            }
            params.delete('controls');
            
            if (params.get('v'))
            {
                invid = 'https://' + instance + '/embed/' + params.get('v');
                params.delete('v');
            } 
            else if (invid.toLowerCase().indexOf('/embed/') !== -1)
            {
                invid = 'https://' + instance + '/embed/' + invid.substring(invid.toLowerCase().indexOf('/embed/') + 7).split('/')[0];
            }
            else if (invid.toLowerCase().indexOf('/v/') !== -1)
            {
                invid = 'https://' + instance + '/embed/' + invid.substring(invid.toLowerCase().indexOf('/v/') + 3).split('/')[0];
            }
            else
            {
                invid = 'https://' + instance + '/embed/' + invid.split('/')[3];
            }

            if (auto !== -1) {
                params.set('autoplay', auto);
            } else if (!params.get('autoplay')) {
                params.set('autoplay', 0);
            }

            if ((parseInt(frame.width, 10) <= 4 || parseInt(frame.style.width, 10) <= 4)
            && (parseInt(frame.height, 10) <= 4 || parseInt(frame.style.height, 10) <= 4)
            && params.get('autoplay') === '1') {
                params.set('listen', 1);
            }

            if(dash){ params.set('quality', 'dash'); }

            invid += '?' + params;
            frame.setAttribute('src', invid);
            
            if (frame.hasAttribute('srcdoc')) { frame.removeAttribute('srcdoc'); }
            
            if (frame.tagName.toLowerCase() === 'embed'){
              let newframe = document.createElement('iframe');
              newframe.innerHTML = frame.innerHTML;
              frame.getAttributeNames().forEach(attrName => { newframe.setAttribute(attrName, frame.getAttribute(attrName)); });
              frame.parentNode.replaceChild(newframe, frame);
            }
        }
      
        //Replace thumbnail embeds
        let thumbs = document.body.querySelectorAll('img[src*="img.youtube.com"]');
      
        for (let x = 0; x < thumbs.length; x++) 
        {
            let thumb = thumbs[x];
            let thumbsrc = new URL(thumb.src);
            
            if (thumbsrc.hostname === "img.youtube.com")
            {
                if (thumb.hasAttribute('srcset') && thumb.srcset.indexOf(thumbsrc) !== -1)
                {
                    thumb.setAttribute('srcset', thumb.srcset.replace(thumbsrc, thumbsrc.protocol + '//' + instance + thumbsrc.pathname + thumbsrc.search + thumbsrc.hash));
                }
                thumb.setAttribute('src', thumbsrc.protocol + '//' + instance + thumbsrc.pathname + thumbsrc.search + thumbsrc.hash);
            }
        }
    }

    function youtubeiFrame(el) {
        try {
            let url = new URL(el.src);
            return (url.hostname === "youtube.com" || url.hostname.endsWith(".youtube.com")
                   || url.hostname === "youtube-nocookie.com" || url.hostname.endsWith(".youtube-nocookie.com")
                   || url.hostname === "youtu.be" || url.hostname.endsWith(".youtu.be"));
        } catch (_) {
            return false;  
        }
    }

    go(a);
})();