Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Youtube Web Extension refactor #2487

Merged
merged 1 commit into from Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 40 additions & 19 deletions app/src/main/assets/web_extensions/webcompat_youtube/background.js
@@ -1,6 +1,7 @@
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';
const targetUrls = [
"https://*.youtube.com/*",
"https://*.youtube-nocookie.com/*"
"https://*.youtube.com/*",
"https://*.youtube-nocookie.com/*"
];

/**
Expand All @@ -10,24 +11,44 @@ const targetUrls = [
* video pages intended for mobile phones, as linked from Google search results).
*/
function redirectUrl(req) {
let redirect = false;
const url = new URL(req.url);
if (url.host.startsWith("m.")) {
url.host = url.host.replace("m.", "www.");
redirect = true;
}
if (!url.searchParams.get("disable_polymer")) {
url.searchParams.set("disable_polymer", "1");
redirect = true;
}
if (!redirect) {
return null;
}
return { redirectUrl: url.toString() };
let redirect = false;
const url = new URL(req.url);
if (url.host.startsWith("m.")) {
url.host = url.host.replace("m.", "www.");
redirect = true;
}
if (!url.searchParams.get("disable_polymer")) {
url.searchParams.set("disable_polymer", "1");
redirect = true;
}
if (!redirect) {
return null;
}
return { redirectUrl: url.toString() };
}

/**
* Override UA. This is required to get the equirectangular video formats from Youtube.
* Otherwise youtube uses custom 360 controls.
*/
function overrideUA(req) {
for (const header of req.requestHeaders) {
if (header.name.toLowerCase() === "user-agent") {
header.value = CUSTOM_USER_AGENT;
}
}
return { requestHeaders: req.requestHeaders };
Comment on lines +34 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to work around an issue with GV UA override?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's the only way I found to apply the UA in youtube iframes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you file a GV issue on bugzilla outlining the problems you encountered. It might end up being that webext is the preferred way to solve the problem but would like it to at least be on the teams radar.

}

browser.webRequest.onBeforeRequest.addListener(
redirectUrl,
{ urls: targetUrls, types: ["main_frame"]},
["blocking"]
redirectUrl,
{ urls: targetUrls, types: ["main_frame"]},
["blocking"]
);

browser.webRequest.onBeforeSendHeaders.addListener(
overrideUA,
{ urls: targetUrls },
["blocking", "requestHeaders"]
);

5 changes: 5 additions & 0 deletions app/src/main/assets/web_extensions/webcompat_youtube/main.css
Expand Up @@ -4,3 +4,8 @@
.ytp-generic-popup, ytp-generic-popup {
display: none;
}

/* Improve readability a bit in FxR. */
hr, html, i, iframe, img, ins, kbd, label, legend, li, menu, object, ol, p, pre, q, s, samp, small, span, strike, strong, sub {
font-size: 110%;
}