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

Commit a9b58ee

Browse files
authored
Fixes #2919 Fixes #2673 Fixes #2641 Hide Youtube fixes (#3394)
* Hide Youtube overlays in immersive Rebase fixes wip * Fix for the player chrome controls fast forwarding * Use the parent container for chrome controls event handling * Ensure player chrome controls are always visible in any resolution
1 parent 3e06922 commit a9b58ee

File tree

1 file changed

+82
-20
lines changed
  • app/src/main/assets/web_extensions/webcompat_youtube

1 file changed

+82
-20
lines changed

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

+82-20
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ const YT_SELECTORS = {
1010
thumbnail: '.ytp-cued-thumbnail-overlay-image',
1111
embedTitle: '.ytp-title-text',
1212
queueHandle: 'ytd-playlist-panel-video-renderer',
13-
playbackControls: '.ytp-left-controls'
13+
playbackControls: '.ytp-chrome-bottom',
14+
overlays: '.videowall-endscreen, .ytp-upnext, .ytp-ce-element'
1415
};
1516
const ENABLE_LOGS = true;
1617
const logDebug = (...args) => ENABLE_LOGS && console.log(LOGTAG, ...args);
1718
const logError = (...args) => ENABLE_LOGS && console.error(LOGTAG, ...args);
19+
const CHROME_CONTROLS_MIN_WIDTH = 480;
1820

1921
class YoutubeExtension {
2022
// We set a custom UA to force Youtube to display the most optimal
@@ -75,11 +77,27 @@ class YoutubeExtension {
7577
return text.includes('360');
7678
}
7779

80+
is360Video() {
81+
const targets = [
82+
document.querySelector(YT_SELECTORS.disclaimer),
83+
document.querySelector(YT_SELECTORS.embedTitle)
84+
];
85+
return targets.some((node) => node && this.is360(node.textContent));;
86+
}
87+
7888
isStereo(text) {
7989
const words = text.toLowerCase().split(/\s+|\./);
8090
return words.includes('stereo') || words.includes('3d') || words.includes('vr');
8191
}
8292

93+
isStereoVideo() {
94+
const targets = [
95+
document.querySelector(YT_SELECTORS.disclaimer),
96+
document.querySelector(YT_SELECTORS.embedTitle)
97+
];
98+
return targets.some((node) => node && this.isStereo(node.textContent));;
99+
}
100+
83101
// Automatically select a video projection if needed
84102
overrideVideoProjection() {
85103
if (!this.isWatchingPage()) {
@@ -94,14 +112,8 @@ class YoutubeExtension {
94112
}
95113
// There is no standard API to detect video projection yet.
96114
// Try to infer it from the video disclaimer or title for now.
97-
const targets = [
98-
document.querySelector(YT_SELECTORS.disclaimer),
99-
document.querySelector(YT_SELECTORS.embedTitle)
100-
];
101-
const is360 = targets.some((node) => node && this.is360(node.textContent));
102-
if (is360) {
103-
const stereo = targets.some((node) => node && this.isStereo(node.textContent));
104-
qs.set('mozVideoProjection', stereo ? '360s_auto' : '360_auto');
115+
if (this.is360Video()) {
116+
qs.set('mozVideoProjection', this.isStereoVideo() ? '360s_auto' : '360_auto');
105117
this.updateURL(qs);
106118
this.updateVideoStyle();
107119
logDebug(`Video projection set to: ${qs.get(VIDEO_PROJECTION_PARAM)}`);
@@ -119,7 +131,7 @@ class YoutubeExtension {
119131
}
120132

121133
overrideClick(event) {
122-
this.overrideVideoProjection();
134+
this.playerFixes();
123135
const player = this.getPlayer();
124136
if (!this.isWatchingPage() || !this.hasVideoProjection() || document.fullscreenElement || !player) {
125137
return; // Only override click in the Youtube watching page for 360 videos.
@@ -146,7 +158,8 @@ class YoutubeExtension {
146158

147159
// Fix for the draggable items to continue being draggable when a context menu is displayed.
148160
// https://github.com/MozillaReality/FirefoxReality/issues/2611
149-
fixQueueContextMenu() {
161+
queueContextMenuFix() {
162+
logDebug('queueContextMenuFix');
150163
const handles = document.querySelectorAll(YT_SELECTORS.queueHandle);
151164
for (var i=0; i<handles.length; i++) {
152165
handles[i].removeEventListener('contextmenu', this.onContextMenu);
@@ -169,9 +182,10 @@ class YoutubeExtension {
169182
}
170183

171184
// Prevent the double click to reach the player to avoid double clicking
172-
// to trigger a playback forward event.
185+
// to trigger a playback forward/backward event.
173186
// https://github.com/MozillaReality/FirefoxReality/issues/2947
174187
videoControlsForwardFix() {
188+
logDebug('videoControlsForwardFix');
175189
const playbackControls = document.querySelector(YT_SELECTORS.playbackControls);
176190
playbackControls.removeEventListener("touchstart", this.videoControlsOnTouchStart);
177191
playbackControls.addEventListener("touchstart", this.videoControlsOnTouchStart);
@@ -189,9 +203,28 @@ class YoutubeExtension {
189203
return false;
190204
}
191205

206+
playerControlsMarginFix() {
207+
if (youtube.isInFullscreen()) {
208+
if (window.innerHeight < CHROME_CONTROLS_MIN_WIDTH) {
209+
var of = CHROME_CONTROLS_MIN_WIDTH - window.innerHeight;
210+
console.log(of)
211+
document.querySelector(".ytp-chrome-bottom").style.setProperty("margin-bottom", `${of}px`)
212+
} else {
213+
document.querySelector(".ytp-chrome-bottom").style.removeProperty("margin-bottom")
214+
}
215+
216+
} else {
217+
document.querySelector(".ytp-chrome-bottom").style.removeProperty("margin-bottom")
218+
}
219+
}
220+
192221
playerFixes() {
193-
this.fixQueueContextMenu();
222+
this.overrideVideoProjection();
223+
this.overrideQualityRetry();
224+
this.hideOverlaysFix();
225+
this.queueContextMenuFix();
194226
this.videoControlsForwardFix();
227+
this.playerControlsMarginFix();
195228
}
196229

197230
// Runs the callback when the video is ready (has loaded the first frame).
@@ -252,13 +285,41 @@ class YoutubeExtension {
252285
return video && video.readyState >=2;
253286
}
254287

288+
// Hide overlays when in immersive mode
289+
// https://github.com/MozillaReality/FirefoxReality/issues/2673
290+
hideOverlaysFix() {
291+
if (youtube.is360Video() || youtube.isStereoVideo()) {
292+
logDebug('hideOverlaysFix');
293+
var overlays = document.querySelectorAll(YT_SELECTORS.overlays);
294+
var observer = new MutationObserver((mutations) => {
295+
if (youtube.isInFullscreen()) {
296+
for (const mutation of mutations) {
297+
if (mutation.target) {
298+
mutation.target.style.display = 'none';
299+
}
300+
}
301+
}
302+
});
303+
for(const overlay of overlays) {
304+
observer.observe(overlay, { attributes: true });
305+
}
306+
}
307+
}
308+
309+
isInFullscreen() {
310+
return !((document.fullScreenElement !== undefined && document.fullScreenElement === null) ||
311+
(document.msFullscreenElement !== undefined && document.msFullscreenElement === null) ||
312+
(document.mozFullScreen !== undefined && !document.mozFullScreen) ||
313+
(document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen));
314+
}
315+
255316
// Utility function to retry tasks max n times until the execution is successful.
256317
retry(taskName, task, attempts = 10, interval = 200) {
257318
let succeeded = false;
258319
try {
259320
succeeded = task();
260321
} catch (ex) {
261-
logError(`Got exception runnning ${taskName} task: ${ex}`);
322+
logError(`Got exception running ${taskName} task: ${ex}`);
262323
}
263324
if (succeeded) {
264325
logDebug(`${taskName} succeeded`);
@@ -278,6 +339,8 @@ class YoutubeExtension {
278339
window.history.replaceState({}, document.title, newUrl);
279340
logDebug(`update URL to ${newUrl}`);
280341
}
342+
343+
281344
}
282345

283346
logDebug(`Initializing youtube extension in frame: ${window.location.href}`);
@@ -286,18 +349,17 @@ youtube.overrideUA();
286349
youtube.overrideViewport();
287350
window.addEventListener('load', () => {
288351
logDebug('page load');
289-
youtube.overrideVideoProjection();
290-
youtube.fixQueueContextMenu();
291352
// Wait until video has loaded the first frame to force quality change.
292353
// This prevents the infinite spinner problem.
293354
// See https://github.com/MozillaReality/FirefoxReality/issues/1433
294355
if (youtube.isWatchingPage()) {
295-
youtube.waitForVideoReady(() => youtube.overrideQualityRetry());
356+
youtube.waitForVideoReady(() => youtube.playerFixes());
296357
}
297358
});
298-
359+
window.addEventListener("resize", () => youtube.playerControlsMarginFix());
360+
document.addEventListener("fullscreenchange", () => youtube.playerControlsMarginFix());
299361
window.addEventListener('pushstate', () => youtube.overrideVideoProjection());
300362
window.addEventListener('popstate', () => youtube.overrideVideoProjection());
301363
window.addEventListener('click', event => youtube.overrideClick(event));
302-
window.addEventListener('mouseup', event => youtube.playerFixes());
303-
window.addEventListener("yt-navigate-start", () => youtube.playerFixes());
364+
window.addEventListener('mouseup', event => youtube.queueContextMenuFix());
365+
window.addEventListener("yt-navigate-finish", () => youtube.playerFixes());

0 commit comments

Comments
 (0)