46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
browser.webRequest.onBeforeRequest.addListener(
|
|
function(details) {
|
|
//console.log(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 : "")
|
|
};
|
|
}
|
|
} else if (url.hostname === "github.com") {
|
|
const GOTHUB = "gothub.lunar.icu";
|
|
if (details.originUrl === undefined) { // entered in the address bar
|
|
return
|
|
}
|
|
origin = new URL(details.originUrl).hostname;
|
|
if (origin === GOTHUB || origin === "github.com") { // click within gothub or github page
|
|
return
|
|
}
|
|
// click from everywhere else
|
|
path = url.pathname.split("/");
|
|
org = path[1];
|
|
repo = path[2];
|
|
return {
|
|
redirectUrl: `https://${GOTHUB}/${org}/${repo}`
|
|
};
|
|
} else if (url.hostname === "www.reddit.com") {
|
|
url.hostname = "old.reddit.com"
|
|
return {
|
|
redirectUrl: url.toString()
|
|
};
|
|
}
|
|
},
|
|
{ urls: [
|
|
"*://www.youtube.com/*",
|
|
"*://youtube.com/*",
|
|
"*://github.com/*",
|
|
"*://www.reddit.com/*",
|
|
]},
|
|
["blocking"]
|
|
);
|