Skip to content

Commit

Permalink
add feature to quickly toggle on/off sponsorblock
Browse files Browse the repository at this point in the history
  • Loading branch information
alyyousuf7 committed Nov 1, 2023
1 parent 1a177c8 commit d67f311
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
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 official webOS/webOS OSE SDK: `ares-install youtube...ipk` (for webOS SDK 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
8 changes: 2 additions & 6 deletions src/sponsorblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,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();
}
},
false
Expand Down
67 changes: 46 additions & 21 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 Down Expand Up @@ -192,4 +216,5 @@ export function showNotification(text, time = 3000) {

setTimeout(() => {
showNotification('Press 🟩 to open YTAF configuration screen');
showNotification('Press 🟥 to toggle on/off SponsorBlock');
}, 2000);

0 comments on commit d67f311

Please sign in to comment.