Skip to content

Commit

Permalink
Merge pull request #104 from alyyousuf7/clean-sponsorblock-on-home
Browse files Browse the repository at this point in the history
stop sponsorblock on playback preview videos
  • Loading branch information
throwaway96 authored Oct 19, 2024
2 parents 987e0c3 + 3662fc6 commit bd7e72c
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/sponsorblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,31 @@ class SponsorBlockHandler {
// shows my lack of understanding of javascript. (or both)
window.sponsorblock = null;

function uninitializeSponsorblock() {
if (!window.sponsorblock) {
return;
}
try {
window.sponsorblock.destroy();
} catch (err) {
console.warn('window.sponsorblock.destroy() failed!', err);
}
window.sponsorblock = null;
}

window.addEventListener(
'hashchange',
() => {
const newURL = new URL(location.hash.substring(1), location.href);
// uninitialize sponsorblock when not on `/watch` path, to prevent
// it from attaching to playback preview video element loaded on
// home page
if (newURL.pathname !== '/watch' && window.sponsorblock) {
console.info('uninitializing sponsorblock on a non-video page');
uninitializeSponsorblock();
return;
}

const videoID = newURL.searchParams.get('v');
const needsReload =
videoID &&
Expand All @@ -397,14 +418,7 @@ window.addEventListener(
);

if (needsReload) {
if (window.sponsorblock) {
try {
window.sponsorblock.destroy();
} catch (err) {
console.warn('window.sponsorblock.destroy() failed!', err);
}
window.sponsorblock = null;
}
uninitializeSponsorblock();

if (configRead('enableSponsorBlock')) {
window.sponsorblock = new SponsorBlockHandler(videoID);
Expand Down

0 comments on commit bd7e72c

Please sign in to comment.