YoutubeEmbed/background.js

22 lines
637 B
JavaScript

// Listen for web requests
browser.webRequest.onBeforeRequest.addListener(
function(details) {
const url = new URL(details.url);
// Check if the URL is a YouTube URL
if (url.hostname === "www.youtube.com" || url.hostname === "youtube.com") {
// Extract the video ID from the URL
const videoId = url.searchParams.get("v");
if (videoId) {
// Rewrite to the embedded URL format
const embedUrl = `https://www.youtube.com/embed/${videoId}`;
return { redirectUrl: embedUrl };
}
}
},
{ urls: [ "*://www.youtube.com/*",
"*://youtube.com/*"] },
["blocking"]
);