-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
41 lines (33 loc) · 1.13 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const PREFIX = "/shorts/";
const PREFIX_LENGTH = PREFIX.length;
const getVideoUrl = (videoId) => `https://www.youtube.com/watch?v=${encodeURIComponent(videoId)}`;
const extractShortVideoId = (url) => {
const { pathname } = new URL(url);
if (!pathname.startsWith(PREFIX)) {
return null;
}
return pathname.slice(PREFIX_LENGTH);
};
const getRedirectConfigForYoutubeUrl = (url) => {
const videoId = extractShortVideoId(url);
if (videoId === null) {
return {};
}
const redirectUrl = getVideoUrl(videoId);
return { redirectUrl };
}
const handleBeforeRequest = (requestDetails) => {
return getRedirectConfigForYoutubeUrl(requestDetails.url);
}
const handleHistoryStateUpdated = (requestDetails) => {
const { redirectUrl: url } = getRedirectConfigForYoutubeUrl(requestDetails.url);
if (url) {
browser.tabs.update(undefined, { url });
}
}
browser.webRequest.onBeforeRequest.addListener(
handleBeforeRequest,
{urls: ["https://www.youtube.com/shorts/*"], types: ["main_frame"]},
["blocking"]
);
browser.webNavigation.onHistoryStateUpdated.addListener(handleHistoryStateUpdated)