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

Commit b7ea596

Browse files
authored
Use WebExtension for youtube redirects instead of the GV API (#2378)
1 parent 1cc152f commit b7ea596

File tree

5 files changed

+40
-61
lines changed

5 files changed

+40
-61
lines changed

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/Session.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.mozilla.vrbrowser.telemetry.TelemetryWrapper;
4343
import org.mozilla.vrbrowser.utils.BitmapCache;
4444
import org.mozilla.vrbrowser.utils.InternalPages;
45+
import org.mozilla.vrbrowser.utils.StringUtils;
4546
import org.mozilla.vrbrowser.utils.SystemUtils;
4647

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

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

873-
String uriOverride = SessionUtils.checkYoutubeOverride(uri);
874-
if (uriOverride != null) {
875-
aSession.loadUri(uriOverride);
876-
return GeckoResult.DENY;
877-
}
878-
879874
if (aSession == mState.mSession) {
880875
Log.d(LOGTAG, "Testing for UA override");
881876

app/src/common/shared/org/mozilla/vrbrowser/browser/engine/SessionUtils.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,48 +69,4 @@ private static void addOptionalPref(FileOutputStream out, String aKey, Bundle aE
6969
out.write(String.format("pref(\"%s\", %s);\n", aKey, value ? "true" : "false").getBytes());
7070
}
7171
}
72-
73-
/**
74-
* 1. Disable YouTube's Polymer layout (which makes YouTube very slow in non-Chrome browsers)
75-
* via a query-string parameter in the URL.
76-
* 2. Rewrite YouTube URLs from `m.youtube.com` -> `youtube.com` (to avoid serving YouTube's
77-
* video pages intended for mobile phones, as linked from Google search results).
78-
*/
79-
public static String checkYoutubeOverride(String aUri) {
80-
try {
81-
Uri uri = Uri.parse(aUri);
82-
String hostLower = uri.getHost().toLowerCase();
83-
if (!hostLower.endsWith(".youtube.com") && !hostLower.endsWith(".youtube-nocookie.com")) {
84-
return null;
85-
}
86-
87-
Uri.Builder uriBuilder = uri.buildUpon();
88-
Boolean updateUri = false;
89-
90-
if (!uri.getScheme().equalsIgnoreCase("https")) {
91-
uriBuilder.scheme("https");
92-
updateUri = true;
93-
}
94-
if (hostLower.startsWith("m.")) {
95-
uriBuilder.authority(hostLower.replaceFirst("m.", "www."));
96-
updateUri = true;
97-
}
98-
String queryDisablePolymer = uri.getQueryParameter("disable_polymer");
99-
if (queryDisablePolymer == null) {
100-
uriBuilder.appendQueryParameter("disable_polymer", "1");
101-
updateUri = true;
102-
}
103-
104-
if (!updateUri) {
105-
return null;
106-
}
107-
108-
return uriBuilder.build().toString();
109-
} catch (Exception ex) {
110-
Log.e(LOGTAG, "Unable to construct transformed URL: " + ex.toString());
111-
}
112-
113-
return null;
114-
}
115-
11672
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const targetUrls = [
2+
"https://*.youtube.com/*",
3+
"https://*.youtube-nocookie.com/*"
4+
];
5+
6+
/**
7+
* 1. Disable YouTube's Polymer layout (which makes YouTube very slow in non-Chrome browsers)
8+
* via a query-string parameter in the URL.
9+
* 2. Rewrite YouTube URLs from `m.youtube.com` -> `youtube.com` (to avoid serving YouTube's
10+
* video pages intended for mobile phones, as linked from Google search results).
11+
*/
12+
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() };
27+
}
28+
29+
browser.webRequest.onBeforeRequest.addListener(
30+
redirectUrl,
31+
{ urls: targetUrls, types: ["main_frame"]},
32+
["blocking"]
33+
);

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,6 @@ try {
297297
window.wrappedJSObject.ytplayer.config.args.jsapicallback = 'onYouTubePlayerReady';
298298
}
299299
});
300-
301-
window.addEventListener("beforeunload", function (e) {
302-
// Make sure that the disable_polymer parameter is kept. Youtube processes the parameter but removes it from the URL.
303-
// See https://github.com/MozillaReality/FirefoxReality/issues/1426
304-
let qs = new URLSearchParams(window.location.search);
305-
qs.set('disable_polymer', '1');
306-
let url = getNewUrl(qs);
307-
window.history.replaceState({}, document.title, url);
308-
});
309-
310300
} catch (err) {
311301
console.error(LOGTAG, 'Encountered error:', err);
312302
}

app/src/main/assets/web_extensions/webcompat_youtube/manifest.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@
1818
"run_at": "document_start",
1919
"all_frames": true
2020
}
21-
]
21+
],
22+
"permissions": ["webRequest", "webRequestBlocking", "*://*.youtube.com/*", "*://*.youtube-nocookie.com/*"],
23+
"background": {
24+
"scripts": ["background.js"]
25+
}
26+
2227
}

0 commit comments

Comments
 (0)