Skip to content

Commit b6b0b99

Browse files
committed
[#76] Refactor button detection
Accounts for missing subtitle button, as might be the case on long videos
1 parent 64c2737 commit b6b0b99

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/scripts/page/components.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,25 @@ export const minMaxPos = (settings: Partial<Settings>, includeDisalbed = false)
5555
}, { min: 0, max: 0 });
5656
export const slot = <S, T>(name: Comp, comp: PlayerComponentSetting, left: S, right: T): S | T => (comp?.position ?? defaultPositions[name]) < 0 ? right : left;
5757
export const setDefaultIds = () => {
58-
const buttons = document.querySelectorAll('#video-controls button');
59-
let given = builtin.filter(i => i != 'time');
60-
// Chromecast and Picture-in-Picture are only available in Chrome
61-
if (buttons.length == given.length - 2) given = given.filter(i => i != 'chromecast-button' && i != 'picture-in-picture-button');
62-
if (buttons.length != given.length) throw new Error('Nebula changed player buttons, not attempting to re-ID');
63-
for (let i = 0; i < buttons.length; ++i)
64-
buttons[i].id = given[i];
65-
document.querySelector('#video-controls > :last-child > :first-child > :last-child').id = 'time';
58+
const left = document.querySelectorAll('#video-controls > :last-child > :first-child > *');
59+
const right = document.querySelectorAll('#video-controls > :last-child > :last-child > *');
60+
if (left.length != 3)
61+
throw new Error('Expected three buttons on left');
62+
const hasChromecast = document.querySelector('#video-controls > div > :last-child > [aria-label="Chromecast"]');
63+
if (hasChromecast) {
64+
if (right.length != 6 && right.length != 7)
65+
throw new Error('Chromecast detected, expected 6 or 7 buttons on right');
66+
} else if (right.length != 4 && right.length != 5)
67+
throw new Error('No Chromecast detected, expected 4 or 5 buttons on right');
68+
const expectNoSubtitle = hasChromecast ? right.length === 6 : right.length === 4;
69+
const givenLeft = builtin.slice(0, left.length);
70+
const givenRight = builtin.slice(left.length)
71+
.filter(hasChromecast ? () => true : e => e != 'picture-in-picture-button' && e != 'chromecast-button')
72+
.filter(expectNoSubtitle ? e => e != 'subtitles-toggle-button' : () => true);
73+
if (givenRight.length != right.length)
74+
throw new Error(`Logic error, found ${right.length} expected ${givenRight.length}`);
75+
for (let i = 0; i < left.length; ++i)
76+
left[i].id = givenLeft[i];
77+
for (let i = 0; i < right.length; ++i)
78+
right[i].id = givenRight[i];
6679
};

0 commit comments

Comments
 (0)