Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature to toggle on/off sponsorblock with a single key press #101

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ YouTube App with extended functionalities
- [SponsorBlock](https://sponsor.ajay.app/) integration
- [Autostart](#autostart)

**Note:** Configuration screen can be opened by pressing 🟩 GREEN button on the remote.
## Remote Buttons

- Press 🟩 GREEN button to open the configuration screen.
- Press 🟥 RED button to toggle on/off the SponsorBlock.

## Pre-requisites

Expand All @@ -25,10 +28,6 @@ YouTube App with extended functionalities
- Use [webOS TV CLI tools](https://webostv.developer.lge.com/develop/tools/cli-installation) -
`ares-install youtube...ipk` (for webOS CLI tools configuration see below)

## Configuration

Configuration screen can be opened by pressing 🟩 GREEN button on the remote.

### Autostart

In order to autostart an application the following command needs to be executed
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ export function configWrite(key, value) {
console.info('Setting key', key, 'to', value);
localConfig[key] = value;
window.localStorage[CONFIG_KEY] = JSON.stringify(localConfig);
if (window.sponsorblock) {
window.sponsorblock.updateSkippableCategories();
}
}
16 changes: 10 additions & 6 deletions src/sponsorblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,16 @@ class SponsorBlockHandler {
this.buildOverlay();
}

updateSkippableCategories() {
this.skippableCategories = this.getSkippableCategories();
}

getSkippableCategories() {
const skippableCategories = [];
if (!configRead('enableSponsorBlock')) {
return skippableCategories;
}

if (configRead('enableSponsorBlockSponsor')) {
skippableCategories.push('sponsor');
}
Expand Down Expand Up @@ -403,12 +411,8 @@ window.addEventListener(
window.sponsorblock = null;
}

if (configRead('enableSponsorBlock')) {
window.sponsorblock = new SponsorBlockHandler(videoID);
window.sponsorblock.init();
} else {
console.info('SponsorBlock disabled, not loading');
}
window.sponsorblock = new SponsorBlockHandler(videoID);
window.sponsorblock.init();
Comment on lines +414 to +415
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the SponsorBlock object always created now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think I see. It's always running, but if it's not enabled, getSkippableCategories() just returns an empty list.

}
},
false
Expand Down
30 changes: 30 additions & 0 deletions src/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,33 @@
padding: 0 1em;
line-height: 0;
}

.ytaf-remote-button {
display: inline-block;
width: 21.34px;
height: 21.34px;
Comment on lines +66 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you get 21.34px?

Copy link
Author

@alyyousuf7 alyyousuf7 Oct 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was the computed height of the text before button. And I wanted to keep the height of the button same as text, so it doesn't look weird.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I think it must have originally been 16pt (21.3333...px).

}

.ytaf-remote-button::before {
content: '\00a0';
}

.ytaf-remote-button.ytaf-remote-button-red {
background-color: #b30000;
color: #b30000;
}

.ytaf-remote-button.ytaf-remote-button-green {
background-color: #006600;
color: #006600;
}

.ytaf-remote-button.ytaf-remote-button-yellow {
background-color: #e0e000;
color: #e0e000;
}

.ytaf-remote-button.ytaf-remote-button-blue {
background-color: #0000cc;
color: #0000cc;
}
82 changes: 59 additions & 23 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ window.__spatialNavigation__.keyMode = 'NONE';

const ARROW_KEY_CODE = { 37: 'left', 38: 'up', 39: 'right', 40: 'down' };

const KEY_MAPPINGS = {
red: [403, 166],
green: [404, 172],
yellow: [405, 170],
blue: [406, 167]
};

const uiContainer = document.createElement('div');
uiContainer.classList.add('ytaf-ui-container');
uiContainer.style['display'] = 'none';
Expand Down Expand Up @@ -62,66 +69,70 @@ uiContainer.innerHTML = `

document.querySelector('body').appendChild(uiContainer);

uiContainer.querySelector('#__adblock').checked = configRead('enableAdBlock');
function updateUI() {
uiContainer.querySelector('#__adblock').checked = configRead('enableAdBlock');
uiContainer.querySelector('#__sponsorblock').checked =
configRead('enableSponsorBlock');
uiContainer.querySelector('#__sponsorblock_sponsor').checked = configRead(
'enableSponsorBlockSponsor'
);
uiContainer.querySelector('#__sponsorblock_intro').checked = configRead(
'enableSponsorBlockIntro'
);
uiContainer.querySelector('#__sponsorblock_outro').checked = configRead(
'enableSponsorBlockOutro'
);
uiContainer.querySelector('#__sponsorblock_interaction').checked = configRead(
'enableSponsorBlockInteraction'
);
uiContainer.querySelector('#__sponsorblock_selfpromo').checked = configRead(
'enableSponsorBlockSelfPromo'
);
uiContainer.querySelector('#__sponsorblock_music_offtopic').checked =
configRead('enableSponsorBlockMusicOfftopic');
}

updateUI();
uiContainer.querySelector('#__adblock').addEventListener('change', (evt) => {
configWrite('enableAdBlock', evt.target.checked);
});

uiContainer.querySelector('#__sponsorblock').checked =
configRead('enableSponsorBlock');
uiContainer
.querySelector('#__sponsorblock')
.addEventListener('change', (evt) => {
configWrite('enableSponsorBlock', evt.target.checked);
});

uiContainer.querySelector('#__sponsorblock_sponsor').checked = configRead(
'enableSponsorBlockSponsor'
);
uiContainer
.querySelector('#__sponsorblock_sponsor')
.addEventListener('change', (evt) => {
configWrite('enableSponsorBlockSponsor', evt.target.checked);
});

uiContainer.querySelector('#__sponsorblock_intro').checked = configRead(
'enableSponsorBlockIntro'
);
uiContainer
.querySelector('#__sponsorblock_intro')
.addEventListener('change', (evt) => {
configWrite('enableSponsorBlockIntro', evt.target.checked);
});

uiContainer.querySelector('#__sponsorblock_outro').checked = configRead(
'enableSponsorBlockOutro'
);
uiContainer
.querySelector('#__sponsorblock_outro')
.addEventListener('change', (evt) => {
configWrite('enableSponsorBlockOutro', evt.target.checked);
});

uiContainer.querySelector('#__sponsorblock_interaction').checked = configRead(
'enableSponsorBlockInteraction'
);
uiContainer
.querySelector('#__sponsorblock_interaction')
.addEventListener('change', (evt) => {
configWrite('enableSponsorBlockInteraction', evt.target.checked);
});

uiContainer.querySelector('#__sponsorblock_selfpromo').checked = configRead(
'enableSponsorBlockSelfPromo'
);
uiContainer
.querySelector('#__sponsorblock_selfpromo')
.addEventListener('change', (evt) => {
configWrite('enableSponsorBlockSelfPromo', evt.target.checked);
});

uiContainer.querySelector('#__sponsorblock_music_offtopic').checked =
configRead('enableSponsorBlockMusicOfftopic');
uiContainer
.querySelector('#__sponsorblock_music_offtopic')
.addEventListener('change', (evt) => {
Expand All @@ -136,7 +147,7 @@ const eventHandler = (evt) => {
evt.keyCode,
evt.defaultPrevented
);
if (evt.charCode == 404 || evt.charCode == 172) {
if (KEY_MAPPINGS.green.includes(evt.charCode)) {
console.info('Taking over!');
evt.preventDefault();
evt.stopPropagation();
Expand All @@ -153,6 +164,19 @@ const eventHandler = (evt) => {
}
return false;
}
if (KEY_MAPPINGS.red.includes(evt.charCode)) {
evt.preventDefault();
evt.stopPropagation();
if (evt.type === 'keydown') {
const newValue = !configRead('enableSponsorBlock');
configWrite('enableSponsorBlock', newValue);
updateUI();
showNotification(
newValue ? 'SponsorBlock toggled on' : 'SponsorBlock toggled off'
);
}
return false;
}
return true;
};

Expand All @@ -173,7 +197,7 @@ export function showNotification(text, time = 3000) {

const elm = document.createElement('div');
const elmInner = document.createElement('div');
elmInner.innerText = text;
elmInner.innerHTML = text;
elmInner.classList.add('message');
elmInner.classList.add('message-hidden');
elm.appendChild(elmInner);
Expand All @@ -190,6 +214,18 @@ export function showNotification(text, time = 3000) {
}, time);
}

function remoteBtnBox(key) {
const el = document.createElement('span');
el.classList.add('ytaf-remote-button', `ytaf-remote-button-${key}`);

return el.outerHTML;
}

setTimeout(() => {
showNotification('Press [GREEN] to open YTAF configuration screen');
showNotification(
`Press ${remoteBtnBox('green')} GREEN to open YTAF configuration screen`
);
showNotification(
`Press ${remoteBtnBox('red')} RED to toggle on/off SponsorBlock`
);
}, 2000);