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 Nov 3, 2023
1 parent aebb41e commit 8931ff5
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 @@ -303,10 +303,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 @@ -321,14 +342,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 8931ff5

Please sign in to comment.