Skip to content

Commit

Permalink
stop sponsorblock on playback preview videos
Browse files Browse the repository at this point in the history
  • Loading branch information
alyyousuf7 committed Mar 25, 2024
1 parent 052d9a3 commit 3662fc6
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 @@ -376,10 +376,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 @@ -394,14 +415,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 3662fc6

Please sign in to comment.