Skip to content

Commit

Permalink
Use WebExtension for youtube redirects instead of the GV API (Mozilla…
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored and Alexandre Lissy committed Jan 21, 2020
1 parent 138cd34 commit 4c0a47f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 61 deletions.
Expand Up @@ -42,6 +42,7 @@
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
import org.mozilla.vrbrowser.utils.BitmapCache;
import org.mozilla.vrbrowser.utils.InternalPages;
import org.mozilla.vrbrowser.utils.StringUtils;
import org.mozilla.vrbrowser.utils.SystemUtils;

import java.net.URI;
Expand Down Expand Up @@ -870,12 +871,6 @@ public void onCanGoForward(@NonNull GeckoSession aSession, boolean aCanGoForward

Log.d(LOGTAG, "onLoadRequest: " + uri);

String uriOverride = SessionUtils.checkYoutubeOverride(uri);
if (uriOverride != null) {
aSession.loadUri(uriOverride);
return GeckoResult.DENY;
}

if (aSession == mState.mSession) {
Log.d(LOGTAG, "Testing for UA override");

Expand Down
Expand Up @@ -69,48 +69,4 @@ private static void addOptionalPref(FileOutputStream out, String aKey, Bundle aE
out.write(String.format("pref(\"%s\", %s);\n", aKey, value ? "true" : "false").getBytes());
}
}

/**
* 1. Disable YouTube's Polymer layout (which makes YouTube very slow in non-Chrome browsers)
* via a query-string parameter in the URL.
* 2. Rewrite YouTube URLs from `m.youtube.com` -> `youtube.com` (to avoid serving YouTube's
* video pages intended for mobile phones, as linked from Google search results).
*/
public static String checkYoutubeOverride(String aUri) {
try {
Uri uri = Uri.parse(aUri);
String hostLower = uri.getHost().toLowerCase();
if (!hostLower.endsWith(".youtube.com") && !hostLower.endsWith(".youtube-nocookie.com")) {
return null;
}

Uri.Builder uriBuilder = uri.buildUpon();
Boolean updateUri = false;

if (!uri.getScheme().equalsIgnoreCase("https")) {
uriBuilder.scheme("https");
updateUri = true;
}
if (hostLower.startsWith("m.")) {
uriBuilder.authority(hostLower.replaceFirst("m.", "www."));
updateUri = true;
}
String queryDisablePolymer = uri.getQueryParameter("disable_polymer");
if (queryDisablePolymer == null) {
uriBuilder.appendQueryParameter("disable_polymer", "1");
updateUri = true;
}

if (!updateUri) {
return null;
}

return uriBuilder.build().toString();
} catch (Exception ex) {
Log.e(LOGTAG, "Unable to construct transformed URL: " + ex.toString());
}

return null;
}

}
33 changes: 33 additions & 0 deletions app/src/main/assets/web_extensions/webcompat_youtube/background.js
@@ -0,0 +1,33 @@
const targetUrls = [
"https://*.youtube.com/*",
"https://*.youtube-nocookie.com/*"
];

/**
* 1. Disable YouTube's Polymer layout (which makes YouTube very slow in non-Chrome browsers)
* via a query-string parameter in the URL.
* 2. Rewrite YouTube URLs from `m.youtube.com` -> `youtube.com` (to avoid serving YouTube's
* 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() };
}

browser.webRequest.onBeforeRequest.addListener(
redirectUrl,
{ urls: targetUrls, types: ["main_frame"]},
["blocking"]
);
10 changes: 0 additions & 10 deletions app/src/main/assets/web_extensions/webcompat_youtube/main.js
Expand Up @@ -297,16 +297,6 @@ try {
window.wrappedJSObject.ytplayer.config.args.jsapicallback = 'onYouTubePlayerReady';
}
});

window.addEventListener("beforeunload", function (e) {
// Make sure that the disable_polymer parameter is kept. Youtube processes the parameter but removes it from the URL.
// See https://github.com/MozillaReality/FirefoxReality/issues/1426
let qs = new URLSearchParams(window.location.search);
qs.set('disable_polymer', '1');
let url = getNewUrl(qs);
window.history.replaceState({}, document.title, url);
});

} catch (err) {
console.error(LOGTAG, 'Encountered error:', err);
}
Expand Up @@ -18,5 +18,10 @@
"run_at": "document_start",
"all_frames": true
}
]
],
"permissions": ["webRequest", "webRequestBlocking", "*://*.youtube.com/*", "*://*.youtube-nocookie.com/*"],
"background": {
"scripts": ["background.js"]
}

}

0 comments on commit 4c0a47f

Please sign in to comment.