Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ const configOptions = new Map([
desc: 'Display time in UI'
}
],
[
'keepWatchPinned',
{
default: false,
desc: 'Keep Time Pinned'
}
],
[
'forceHighResVideo',
{
Expand Down
1 change: 1 addition & 0 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function createOptionsPanel() {
elmContainer.appendChild(createConfigCheckbox('upgradeThumbnails'));
elmContainer.appendChild(createConfigCheckbox('hideLogo'));
elmContainer.appendChild(createConfigCheckbox('showWatch'));
elmContainer.appendChild(createConfigCheckbox('keepWatchPinned'));
elmContainer.appendChild(createConfigCheckbox('removeShorts'));
elmContainer.appendChild(createConfigCheckbox('forceHighResVideo'));
elmContainer.appendChild(createConfigCheckbox('removeEndscreen'));
Expand Down
44 changes: 33 additions & 11 deletions src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Watch {
#watch;
#timer;
#attrChanges;
#player = null;
#pinned = false;
#PLAYER_SELECTOR = 'ytlr-watch-default';

constructor() {
Expand Down Expand Up @@ -40,23 +42,35 @@ class Watch {
}

playerAppear(video) {
this.changeVisibility(video);
this.changeVisibility();
this.playerObserver(video);
}

changeVisibility(video) {
const focused = video.getAttribute('hybridnavfocusable') === 'true';
changeVisibility() {
// When pinned the clock stays visible regardless of whether the player is
// focused.
if (this.#pinned) {
this.#watch.style.display = 'block';
return;
}

const focused = this.#player?.getAttribute('hybridnavfocusable') === 'true';
this.#watch.style.display = focused ? 'none' : 'block';
}

setPinned(pinned) {
this.#pinned = pinned;
this.changeVisibility();
}

async playerEvents() {
const player = await requireElement(this.#PLAYER_SELECTOR, HTMLElement);
this.playerAppear(player);
this.#player = await requireElement(this.#PLAYER_SELECTOR, HTMLElement);
this.playerAppear(this.#player);
}

playerObserver(node) {
this.#attrChanges = new MutationObserver(() => {
this.changeVisibility(node);
this.changeVisibility();
});

this.#attrChanges.observe(node, {
Expand All @@ -74,17 +88,25 @@ class Watch {

let watchInstance = null;

function toggleWatch(show) {
if (show) {
function refreshWatch() {
const pinned = configRead('keepWatchPinned');

// The clock exists while either the config option is on or it's pinned.
if (configRead('showWatch') || pinned) {
watchInstance = watchInstance ? watchInstance : new Watch();
watchInstance.setPinned(pinned);
} else {
watchInstance?.destroy();
watchInstance = null;
}
}

toggleWatch(configRead('showWatch'));
refreshWatch();

configAddChangeListener('showWatch', () => {
refreshWatch();
});

configAddChangeListener('showWatch', (evt) => {
toggleWatch(evt.detail.newValue);
configAddChangeListener('keepWatchPinned', () => {
refreshWatch();
});