Forwards YouTube links to the youtube.com/embed/* page, so there's just the video in your window and nothing else.
目前為
// ==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.6
// @grant none
// @license MIT
// ==/UserScript==
(function () {
const embedBaseUrl = 'https://www.youtube.com/embed/';
function getVideoAndPlaylist(url) {
const videoMatch = /(?:[?&]v=|\/(?:embed\/|v\/|shorts\/))([^&?/]+)/.exec(url);
const listMatch = /[?&]list=([^&]+)/.exec(url);
return {
videoId: videoMatch ? videoMatch[1] : '',
listId: listMatch ? listMatch[1] : ''
};
}
function ensureReferrerMeta() {
const head = document.head || document.documentElement;
if (!head.querySelector('meta[name="referrer"]')) {
const meta = document.createElement('meta');
meta.name = 'referrer';
meta.content = 'origin';
head.prepend(meta);
}
}
function updateLinks(links) {
links.forEach(link => {
const { videoId, listId } = getVideoAndPlaylist(link.href);
if (videoId) {
let embedUrl = embedBaseUrl + videoId;
if (listId) embedUrl += '?list=' + listId;
link.href = embedUrl;
}
});
}
function observeDOM() {
const targetNode = document.body;
const observer = new MutationObserver(mutationsList => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes[0]) {
const newLinks = mutation.addedNodes[0].querySelectorAll?.(
'a[href*="/watch?v="], a[href*="/shorts/"], a[href*="/watch?app=desktop&v="]'
);
if (newLinks?.length) updateLinks(newLinks);
}
}
});
observer.observe(targetNode, { childList: true, subtree: true });
}
const url = window.location.href;
const { videoId, listId } = getVideoAndPlaylist(url);
if (videoId) {
let embedUrl = embedBaseUrl + videoId;
if (listId) embedUrl += '?list=' + listId;
if (embedUrl !== url) {
ensureReferrerMeta();
window.location.replace(embedUrl);
}
} else {
observeDOM();
}
})();