From 3662fc6634cacf201af769938ae76ee5144c3316 Mon Sep 17 00:00:00 2001 From: Ali Yousuf Date: Thu, 2 Nov 2023 22:58:51 -0400 Subject: [PATCH] stop sponsorblock on playback preview videos --- src/sponsorblock.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/sponsorblock.js b/src/sponsorblock.js index 9a1ea348..3ab933d9 100644 --- a/src/sponsorblock.js +++ b/src/sponsorblock.js @@ -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 && @@ -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);