Skip to content

Commit f8d9131

Browse files
committed
Improve theatre mode handling
1 parent ca9fb9f commit f8d9131

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/scripts/pages/watchnebula/_nebula.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { videosettings, ytvideo } from "../../_shared";
2-
import { c, getBrowserInstance, injectScript } from "../../_sharedBrowser";
2+
import { c, getBrowserInstance, injectScript, isChrome } from "../../_sharedBrowser";
33
import iconWatchLater from "./../../../icons/watchlater.svg";
44
import { addToStore, enqueue, enqueueNow, gotoNextInQueue, init as initQueue, isEmptyQueue, setQueue, videoUrlMatch } from "./_queue";
55
import { init as initDrag } from "./_queueDrag";
@@ -26,7 +26,7 @@ export const nebula = async () => {
2626

2727
const isAndroid: boolean = await getBrowserInstance().runtime.sendMessage("isAndroid");
2828
const { youtube, theatre, customScriptPage } = await getFromStorage({ youtube: false, theatre: false, customScriptPage: '' });
29-
console.debug(youtube, theatre);
29+
console.debug('Youtube:', youtube, 'Theatre Mode:', theatre, 'Video page?', isVideoPage());
3030
theatreMode = theatre;
3131

3232
const r = refreshTheatreMode.bind(null, menu);
@@ -36,9 +36,7 @@ export const nebula = async () => {
3636
Array.from(document.querySelectorAll<HTMLImageElement>(`${videoselector} img`)).forEach(createLink);
3737
if (youtube)
3838
maybeLoadComments();
39-
cancelTheatreMode();
40-
if (!isAndroid)
41-
updateTheatreMode(menu);
39+
domRefreshTheatreMode(isAndroid, menu);
4240
const f = document.querySelector('iframe');
4341
if (!f) return;
4442
f.removeEventListener('fullscreenchange', r);
@@ -47,7 +45,7 @@ export const nebula = async () => {
4745
});
4846
const m = new MutationObserver(cb);
4947
m.observe(document.querySelector('#root'), { subtree: true, childList: true });
50-
window.addEventListener('resize', () => updateTheatreMode(menu));
48+
window.addEventListener('resize', updateTheatreMode.bind(null, menu));
5149
cb();
5250

5351
// inject custom script (if available)
@@ -115,10 +113,10 @@ const createLink = (img: HTMLElement) => {
115113
};
116114
const mutation = (func: () => void) => {
117115
let timeout = 0;
118-
return function () {
116+
return () => {
119117
clearTimeout(timeout);
120118
timeout = window.setTimeout(func, 500);
121-
}
119+
};
122120
};
123121

124122
const click = async (e: MouseEvent) => {
@@ -170,7 +168,8 @@ const loadComments = async () => {
170168
const title = h2[0].textContent;
171169
const creator = h2[1].textContent;
172170
if (!title || !creator) return;
173-
if (!h2[0].nextElementSibling || h2[0].nextElementSibling.querySelector('.enhancer-yt'))
171+
const e = h2[0].nextElementSibling;
172+
if (!e || e.querySelector('.enhancer-yt'))
174173
return; // already requested
175174
console.debug(`Requesting '${title}' by ${creator}`);
176175

@@ -183,10 +182,11 @@ const loadComments = async () => {
183182
a.target = '_blank';
184183
a.textContent = a.href;
185184
v.append(a, ` (${(vid.confidence * 100).toFixed(1)}%)`);
186-
h2[0].nextElementSibling.append(h2[0].nextElementSibling.querySelector('span[class]')?.cloneNode(true), v); // dot
185+
e.append(e.querySelector('span[class]')?.cloneNode(true), v); // dot
187186
} catch (err) {
188187
console.error(err);
189188
}
189+
console.debug('Loading comments done.');
190190
};
191191

192192
const maybeGoTheatreMode = (menu: HTMLElement) => {
@@ -196,6 +196,8 @@ const maybeGoTheatreMode = (menu: HTMLElement) => {
196196
const goTheatreMode = (menu: HTMLElement) => {
197197
const mh = menu.getBoundingClientRect().height;
198198
const frame = document.querySelector('iframe');
199+
if (!frame)
200+
return;
199201
const ratio = frame.clientWidth / frame.clientHeight;
200202
const top = +window.getComputedStyle(frame.parentElement.parentElement).paddingTop.slice(0, -2);
201203
if (!ratio)
@@ -226,11 +228,24 @@ const updateTheatreMode = (menu: HTMLElement) => {
226228
maybeGoTheatreMode(menu);
227229
};
228230
const refreshTheatreMode = (menu: HTMLElement) => {
229-
if (!theatreMode)
231+
if (!theatreMode || !isVideoPage())
230232
return;
231233
cancelTheatreMode();
232234
setTimeout(goTheatreMode, 0, menu);
233235
};
236+
const domRefreshTheatreMode = (() => {
237+
let hadIFrame = false;
238+
return (isAndroid: boolean, menu: HTMLElement) => {
239+
if (isAndroid)
240+
return;
241+
const hasIFrame = document.querySelector('iframe');
242+
if (!hadIFrame && hasIFrame)
243+
refreshTheatreMode(menu);
244+
if (theatreMode && !hasIFrame.style.height)
245+
goTheatreMode(menu);
246+
hadIFrame = !!hasIFrame;
247+
};
248+
})();
234249

235250
const focusIframe = (iter = 0) => {
236251
if (!isVideoPage()) return;

src/scripts/pages/watchnebula/_queue.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ export const gotoQueue = (index: number, go = true) => {
103103
} else {
104104
clearText();
105105
}
106+
// chrome does not do this for us
107+
const yt = document.querySelector('.enhancer-yt');
108+
if (yt) {
109+
yt.previousElementSibling.remove();
110+
yt.remove();
111+
}
106112
};
107113
export const gotoNextInQueue = () => gotoQueue(queuepos + 1);
108114
export const gotoPrevInQueue = () => gotoQueue(queuepos - 1);

0 commit comments

Comments
 (0)