YouTube /embed/ forwarder

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

当前为 2023-12-18 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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.4
// @grant       none
// @license     MIT

// ==/UserScript==

(function() {
    var embedBaseUrl = 'https://youtube.com/embed/';

    function getId(u) {
        var idMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(u);
        return idMatch ? idMatch[1] : '';
    }

    function updateLinks(links) {
        links.forEach(function(link) {
            var linkHref = link.getAttribute('href');
            var videoIdMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(linkHref);

            if (videoIdMatch) {
                var videoId = videoIdMatch[1];
                var embedUrl = embedBaseUrl + videoId;
                link.href = embedUrl;
            }
        });
    }

    function observeDOM() {
        var targetNode = document.body;

        var observer = new MutationObserver(function(mutationsList) {
            for (var mutation of mutationsList) {
                if (mutation.type === 'childList') {
                    var newLinks = mutation.addedNodes[0].querySelectorAll('a[href*="/watch?v="], a[href*="/shorts/"], a[href*="/watch?app=desktop&v="]');
                    updateLinks(newLinks);
                }
            }
        });

        var observerConfig = { childList: true, subtree: true };
        observer.observe(targetNode, observerConfig);
    }

    var url = window.location.href;

    if (url.includes('/watch?v=') || url.includes('/shorts/') || url.includes('/watch?app=desktop&v=')) {
        var videoId = getId(url);
        var newURL = embedBaseUrl + videoId;

        // Change the location to the new URL
        if (newURL !== url) {
            window.location.href = newURL;
        }
    } else {
        observeDOM();
    }
})();