Invidious (yewtu.be) embed

Replace YouTube embeds with yewtu.be embeds.

目前為 2022-04-03 提交的版本,檢視 最新版本

// ==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/*
// @inject-into content
// @version     2.62
// @namespace https://greasyfork.org/users/751327
// ==/UserScript==
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 = false; //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) {
    var frames = document.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.getAttribute('src');
        if (location.hostname.indexOf('duckduckgo.com') !== -1 && invid.indexOf('/video_frame?url=') === 0) {
            invid = decodeURIComponent(src.replace('/video_frame?url=', ''));
        }

        let params = new URLSearchParams();
        if (invid.indexOf('?') !== -1) {
            params = new URLSearchParams(invid.split('?').pop());
            invid = invid.split('?')[0];
        } else if (invid.indexOf('&') !== -1) {
            params = new URLSearchParams(invid.split(invid.split('&')[0]).pop());
            invid = invid.split('&')[0];
        }
        params.delete('controls');
      
        if (!params.get('v')) {
            invid = invid.
            replace('www.', '').
            replace('youtube.com/', 'yewtu.be/').
            replace('youtube-nocookie.com/', 'yewtu.be/').
            replace('/v/', '/embed/')
                .replace('youtu.be/', 'yewtu.be/embed/');
        } else {
            invid = 'https://yewtu.be/embed/' + params.get('v');
            params.delete('v');
        }

        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.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);
        }
    }
}

function youtubeiFrame(el) {
    return (el.getAttribute('src').indexOf('youtube.com/') !== -1 || el.getAttribute('src').indexOf('youtube-nocookie.com/') !== -1 || el.getAttribute('src').indexOf('youtu.be/') !== -1);
}

go(a);