Invidious (yewtu.be) embed

Replace YouTube embeds with yewtu.be embeds.

当前为 2022-01-26 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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 *://www.youtube.com/*
// @exclude *://*.google.com/*
// @exclude *://turntable.fm/*
// @exclude *://web.archive.org/web/*
// @version     2.58
// @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

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];
            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 ((frame.width === '0' || frame.width === '1' || frame.style.width === '0px' || frame.style.width === '1px')
        && (frame.height === '0' || frame.height === '1' || frame.style.height === '0px' || frame.style.height === '1px')
        && params.get('autoplay') === '1') {
            params.set('listen', 1);
        }

        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') !== -1 || el.getAttribute('src').indexOf('youtu.be') !== -1);
}

go(a);