From 8931ff5c3493591658fc809a2eccfdb758e4dc43 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 0b8500d2..526d6766 100644 --- a/src/sponsorblock.js +++ b/src/sponsorblock.js @@ -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 && @@ -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);