YoutubeEmbed/background.js

21 lines
701 B
JavaScript

browser.webRequest.onBeforeRequest.addListener(
function(details) {
const url = new URL(details.url);
if (url.hostname === "www.youtube.com" || url.hostname === "youtube.com") {
// Extract the video ID from the URL params
const videoId = url.searchParams.get("v");
if (videoId) {
// Rewrite to the embedded URL format, picking timecode if available
const timecode = url.searchParams.get("t");
return {
redirectUrl: `https://www.youtube-nocookie.com/embed/${videoId}?rel=0` + (timecode ? "&start=" + timecode : "");
};
}
}
},
{ urls: [
"*://www.youtube.com/*",
"*://youtube.com/*"
]},
["blocking"]
);