Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit c4770a3

Browse files
MortimerGorokeianhzo
authored andcommitted
Youtube Web Extension refactor (#2487)
1 parent 8dd6bff commit c4770a3

File tree

4 files changed

+232
-290
lines changed

4 files changed

+232
-290
lines changed
Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
const CUSTOM_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12) AppleWebKit/602.1.21 (KHTML, like Gecko) Version/9.2 Safari/602.1.21';
12
const targetUrls = [
2-
"https://*.youtube.com/*",
3-
"https://*.youtube-nocookie.com/*"
3+
"https://*.youtube.com/*",
4+
"https://*.youtube-nocookie.com/*"
45
];
56

67
/**
@@ -10,24 +11,44 @@ const targetUrls = [
1011
* video pages intended for mobile phones, as linked from Google search results).
1112
*/
1213
function redirectUrl(req) {
13-
let redirect = false;
14-
const url = new URL(req.url);
15-
if (url.host.startsWith("m.")) {
16-
url.host = url.host.replace("m.", "www.");
17-
redirect = true;
18-
}
19-
if (!url.searchParams.get("disable_polymer")) {
20-
url.searchParams.set("disable_polymer", "1");
21-
redirect = true;
22-
}
23-
if (!redirect) {
24-
return null;
25-
}
26-
return { redirectUrl: url.toString() };
14+
let redirect = false;
15+
const url = new URL(req.url);
16+
if (url.host.startsWith("m.")) {
17+
url.host = url.host.replace("m.", "www.");
18+
redirect = true;
19+
}
20+
if (!url.searchParams.get("disable_polymer")) {
21+
url.searchParams.set("disable_polymer", "1");
22+
redirect = true;
23+
}
24+
if (!redirect) {
25+
return null;
26+
}
27+
return { redirectUrl: url.toString() };
28+
}
29+
30+
/**
31+
* Override UA. This is required to get the equirectangular video formats from Youtube.
32+
* Otherwise youtube uses custom 360 controls.
33+
*/
34+
function overrideUA(req) {
35+
for (const header of req.requestHeaders) {
36+
if (header.name.toLowerCase() === "user-agent") {
37+
header.value = CUSTOM_USER_AGENT;
38+
}
39+
}
40+
return { requestHeaders: req.requestHeaders };
2741
}
2842

2943
browser.webRequest.onBeforeRequest.addListener(
30-
redirectUrl,
31-
{ urls: targetUrls, types: ["main_frame"]},
32-
["blocking"]
44+
redirectUrl,
45+
{ urls: targetUrls, types: ["main_frame"]},
46+
["blocking"]
3347
);
48+
49+
browser.webRequest.onBeforeSendHeaders.addListener(
50+
overrideUA,
51+
{ urls: targetUrls },
52+
["blocking", "requestHeaders"]
53+
);
54+

app/src/main/assets/web_extensions/webcompat_youtube/main.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
.ytp-generic-popup, ytp-generic-popup {
55
display: none;
66
}
7+
8+
/* Improve readability a bit in FxR. */
9+
hr, html, i, iframe, img, ins, kbd, label, legend, li, menu, object, ol, p, pre, q, s, samp, small, span, strike, strong, sub {
10+
font-size: 110%;
11+
}

0 commit comments

Comments
 (0)