-
Notifications
You must be signed in to change notification settings - Fork 215
/
main.js
365 lines (329 loc) · 13.9 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
const CUSTOM_USER_AGENT = 'Mozilla/5.0 (Linux; Android 7.1.1; Quest) AppleWebKit/537.36 (KHTML, like Gecko) OculusBrowser/7.0.13.186866463 SamsungBrowser/4.0 Chrome/77.0.3865.126 Mobile VR Safari/537.36';
const LOGTAG = '[firefoxreality:webcompat:youtube]';
const VIDEO_PROJECTION_PARAM = 'mozVideoProjection';
const YT_SELECTORS = {
disclaimer: '.yt-alert-message, yt-alert-message',
player: '#movie_player',
embedPlayer: '.html5-video-player',
largePlayButton: '.ytp-large-play-button',
thumbnail: '.ytp-cued-thumbnail-overlay-image',
embedTitle: '.ytp-title-text',
queueHandle: 'ytd-playlist-panel-video-renderer',
playbackControls: '.ytp-chrome-bottom',
overlays: '.videowall-endscreen, .ytp-upnext, .ytp-ce-element'
};
const ENABLE_LOGS = true;
const logDebug = (...args) => ENABLE_LOGS && console.log(LOGTAG, ...args);
const logError = (...args) => ENABLE_LOGS && console.error(LOGTAG, ...args);
const CHROME_CONTROLS_MIN_WIDTH = 480;
class YoutubeExtension {
// We set a custom UA to force Youtube to display the most optimal
// and high-resolution layout available for playback in a mobile VR browser.
overrideUA() {
Object.defineProperty(navigator, 'userAgent', {
get: () => CUSTOM_USER_AGENT
});
logDebug(`Youtube UA overriden to: ${navigator.userAgent}`)
}
// If missing, inject a `<meta name="viewport">` tag to trigger YouTube's mobile layout.
overrideViewport() {
const content = `width=device-width;maximum-scale=1;minimum-scale=1;initial-scale=1;`;
let viewport = document.querySelector('meta[name="viewport"]');
if (viewport) {
viewport.setAttribute('content', content);
} else {
const container = document.head || document.documentElement;
container.insertAdjacentHTML('afterbegin', `<meta name="viewport" content="${content}"/>`);
}
logDebug(`Youtube viewport updated: ${window.innerWidth}x${window.innerHeight} `);
}
// Select a better youtube video quality
overrideQuality() {
logDebug('overrideQuality attempt');
const player = this.getPlayer();
if (!player) {
logDebug('player not ready');
return false;
}
const preferredLevels = this.getPreferredQualities();
const currentLevel = player.getPlaybackQuality();
logDebug(`Video getPlaybackQuality: ${currentLevel}`);
let availableLevels = player.getAvailableQualityLevels();
logDebug(`Video getAvailableQualityLevels: ${availableLevels}`);
for (const level of preferredLevels) {
if (availableLevels.indexOf(level) >= 0) {
if (currentLevel !== level) {
player.setPlaybackQualityRange(level, level);
logDebug(`Video setPlaybackQualityRange: ${level}`);
} else {
logDebug('Best quality already selected');
}
return true;
}
}
return false;
}
overrideQualityRetry() {
this.retry("overrideQuality", () => this.overrideQuality());
}
is360(text) {
return text.includes('360');
}
is360Video() {
const targets = [
document.querySelector(YT_SELECTORS.disclaimer),
document.querySelector(YT_SELECTORS.embedTitle)
];
return targets.some((node) => node && this.is360(node.textContent));;
}
isStereo(text) {
const words = text.toLowerCase().split(/\s+|\./);
return words.includes('stereo') || words.includes('3d') || words.includes('vr');
}
isStereoVideo() {
const targets = [
document.querySelector(YT_SELECTORS.disclaimer),
document.querySelector(YT_SELECTORS.embedTitle)
];
return targets.some((node) => node && this.isStereo(node.textContent));;
}
// Automatically select a video projection if needed
overrideVideoProjection() {
if (!this.isWatchingPage()) {
logDebug("is not watching page");
return; // Only override projection in the Youtube watching page.
}
const qs = new URLSearchParams(window.location.search);
if (qs.get(VIDEO_PROJECTION_PARAM)) {
logDebug(`Video has already a video projection selected: ${qs.get(VIDEO_PROJECTION_PARAM)}`);
this.updateVideoStyle();
return;
}
// There is no standard API to detect video projection yet.
// Try to infer it from the video disclaimer or title for now.
if (this.is360Video()) {
qs.set('mozVideoProjection', this.isStereoVideo() ? '360s_auto' : '360_auto');
this.updateURL(qs);
this.updateVideoStyle();
logDebug(`Video projection set to: ${qs.get(VIDEO_PROJECTION_PARAM)}`);
} else {
logDebug(`Video is flat, no projection selected`);
}
}
updateVideoStyle() {
const video = this.getVideo();
if (video) {
video.classList.add('fxr-vr-video');
logDebug('Added video projection style');
}
}
overrideClick(event) {
this.overrideVideoProjection();
const player = this.getPlayer();
if (!this.isWatchingPage() || !this.hasVideoProjection() || document.fullscreenElement || !player) {
return; // Only override click in the Youtube watching page for 360 videos.
}
if (this.isEmbed() && !this.isVideoReady()) {
return false; // Embeds videos are only loaded after the first click
}
const target = event.target;
let valid = target.tagName.toLowerCase() === 'video' ||
target === document.querySelector(YT_SELECTORS.thumbnail) ||
target === document.querySelector(YT_SELECTORS.largePlayButton) ||
target == player;
if (valid) {
player.playVideo();
player.requestFullscreen();
// Force video play when entering immersive mode.
setTimeout(() => this.retry("PlayVideo", () => {
player.playVideo();
return !this.getVideo().paused;
}), 200);
}
}
// Fix for the draggable items to continue being draggable when a context menu is displayed.
// https://github.com/MozillaReality/FirefoxReality/issues/2611
queueContextMenuFix() {
logDebug('queueContextMenuFix');
const handles = document.querySelectorAll(YT_SELECTORS.queueHandle);
for (var i=0; i<handles.length; i++) {
handles[i].removeEventListener('contextmenu', this.onContextMenu);
handles[i].addEventListener('contextmenu', this.onContextMenu);
}
}
onContextMenu(event) {
setTimeout(() => {
var evt = document.createEvent("MouseEvents");
evt.initEvent("mouseup", true, true);
event.target.dispatchEvent(evt);
});
// This is supposed to prevent the context menu from showing but it doesn't seem to work
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
return false;
}
// Prevent the double click to reach the player to avoid double clicking
// to trigger a playback forward/backward event.
// https://github.com/MozillaReality/FirefoxReality/issues/2947
videoControlsForwardFix() {
logDebug('videoControlsForwardFix');
const playbackControls = document.querySelector(YT_SELECTORS.playbackControls);
playbackControls.removeEventListener("touchstart", this.videoControlsOnTouchStart);
playbackControls.addEventListener("touchstart", this.videoControlsOnTouchStart);
playbackControls.removeEventListener("touchend", this.videoControlsOnTouchEnd);
playbackControls.addEventListener("touchend", this.videoControlsOnTouchEnd);
}
videoControlsOnTouchStart(evt) {
evt.stopPropagation();
return false;
}
videoControlsOnTouchEnd(evt) {
evt.stopPropagation();
return false;
}
playerControlsMarginFix() {
if (youtube.isInFullscreen()) {
if (window.innerHeight < CHROME_CONTROLS_MIN_WIDTH) {
var of = CHROME_CONTROLS_MIN_WIDTH - window.innerHeight;
console.log(of)
document.querySelector(".ytp-chrome-bottom").style.setProperty("margin-bottom", `${of}px`)
} else {
document.querySelector(".ytp-chrome-bottom").style.removeProperty("margin-bottom")
}
} else {
document.querySelector(".ytp-chrome-bottom").style.removeProperty("margin-bottom")
}
}
playerFixes() {
this.overrideVideoProjection();
this.overrideQualityRetry();
this.hideOverlaysFix();
this.queueContextMenuFix();
this.videoControlsForwardFix();
this.playerControlsMarginFix();
}
// Runs the callback when the video is ready (has loaded the first frame).
waitForVideoReady(callback) {
this.retry("VideoReady", () => {
const video = this.getVideo();
if (!video) {
return false;
}
if (video.readyState >= 2) {
callback();
} else {
video.addEventListener("loadeddata", callback, {once: true});
}
return true;
});
}
// Get's the Youtube player elements which contains the API functions.
getPlayer() {
let player = document.querySelector(this.isEmbed() ? YT_SELECTORS.embedPlayer : YT_SELECTORS.player);
if (!player || !player.wrappedJSObject) {
return null;
}
return player.wrappedJSObject;
}
getVideo() {
return document.getElementsByTagName('video')[0];
}
// Get's the preferred video qualities for the current device.
getPreferredQualities() {
// Disable 5k video until issue can be resolved in Gecko Media Process
// see https://github.com/MozillaReality/FirefoxReality/issues/3193
// let all = ['hd2880', 'hd2160','hd1440', 'hd1080', 'hd720', 'large', 'medium'];
let all = ['hd2160','hd1440', 'hd1080', 'hd720', 'large', 'medium'];
return all;
}
// Returns true if we are in a video watching page.
isWatchingPage() {
return window.location.pathname.startsWith('/watch') || this.isEmbed();
}
isEmbed() {
return window.location.pathname.startsWith('/embed');
}
// Returns true if we are in a video watching page.
hasVideoProjection() {
const qs = new URLSearchParams(window.location.search);
return !!qs.get(VIDEO_PROJECTION_PARAM);
}
isVideoReady() {
const video = this.getVideo();
return video && video.readyState >=2;
}
// Hide overlays when in immersive mode
// https://github.com/MozillaReality/FirefoxReality/issues/2673
hideOverlaysFix() {
if (youtube.is360Video() || youtube.isStereoVideo()) {
logDebug('hideOverlaysFix');
var overlays = document.querySelectorAll(YT_SELECTORS.overlays);
var observer = new MutationObserver((mutations) => {
if (youtube.isInFullscreen()) {
for (const mutation of mutations) {
if (mutation.target) {
mutation.target.style.display = 'none';
}
}
}
});
for(const overlay of overlays) {
observer.observe(overlay, { attributes: true });
}
}
}
isInFullscreen() {
return !((document.fullScreenElement !== undefined && document.fullScreenElement === null) ||
(document.msFullscreenElement !== undefined && document.msFullscreenElement === null) ||
(document.mozFullScreen !== undefined && !document.mozFullScreen) ||
(document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen));
}
// Utility function to retry tasks max n times until the execution is successful.
retry(taskName, task, attempts = 10, interval = 200) {
let succeeded = false;
try {
succeeded = task();
} catch (ex) {
logError(`Got exception running ${taskName} task: ${ex}`);
}
if (succeeded) {
logDebug(`${taskName} succeeded`);
return;
}
attempts--;
logDebug(`${taskName} failed. Remaining attempts ${attempts}`);
if (attempts > 0) {
setTimeout(() => {
this.retry(taskName, task, attempts, interval);
})
};
}
// Utility function to replace current URL params and update history.
updateURL(qs) {
const newUrl = `${window.location.pathname}?${qs}`;
window.history.replaceState({}, document.title, newUrl);
logDebug(`update URL to ${newUrl}`);
}
}
logDebug(`Initializing youtube extension in frame: ${window.location.href}`);
const youtube = new YoutubeExtension();
youtube.overrideUA();
youtube.overrideViewport();
window.addEventListener('load', () => {
logDebug('page load');
// Wait until video has loaded the first frame to force quality change.
// This prevents the infinite spinner problem.
// See https://github.com/MozillaReality/FirefoxReality/issues/1433
if (youtube.isWatchingPage()) {
youtube.waitForVideoReady(() => youtube.playerFixes());
}
});
window.addEventListener("resize", () => youtube.playerControlsMarginFix());
document.addEventListener("fullscreenchange", () => youtube.playerControlsMarginFix());
window.addEventListener('pushstate', () => youtube.overrideVideoProjection());
window.addEventListener('popstate', () => youtube.overrideVideoProjection());
window.addEventListener('click', event => youtube.overrideClick(event));
window.addEventListener('mouseup', event => youtube.queueContextMenuFix());
window.addEventListener("yt-navigate-finish", () => youtube.playerFixes());