From c2bc5441d195dd847cdeaaa27d625e422a190d8a Mon Sep 17 00:00:00 2001 From: Andrew S Date: Wed, 21 Feb 2024 20:38:13 -0600 Subject: [PATCH] Fix #87 --- chrome/content.js | 78 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/chrome/content.js b/chrome/content.js index 4c293d43..fa7ac641 100644 --- a/chrome/content.js +++ b/chrome/content.js @@ -212,24 +212,7 @@ chrome.runtime.onMessage.addListener( OverridenYTKeys = true; return true; } else if (request.type === 'remove_players') { - iframeMap.forEach((iframeObj) => { - unmakeMiniPlayer(iframeObj); - }); - - players.forEach((player) => { - if (player.isYt) { - showYT(player.old); - player.iframe.parentNode.removeChild(player.iframe); - } else { - player.iframe.parentNode.replaceChild(player.old, player.iframe); - } - - removePauseListeners(player.old); - }); - - players.length = 0; - FoundYTPlayer = null; - OverridenYTKeys = false; + removePlayers(); sendResponse('ok'); } else if (request.type === 'get_video_size') { getVideo().then((video) => { @@ -239,6 +222,27 @@ chrome.runtime.onMessage.addListener( } }); +function removePlayers() { + iframeMap.forEach((iframeObj) => { + unmakeMiniPlayer(iframeObj); + }); + + players.forEach((player) => { + if (player.isYt) { + showYT(player.old); + player.iframe.parentNode.removeChild(player.iframe); + } else { + player.iframe.parentNode.replaceChild(player.old, player.iframe); + } + + removePauseListeners(player.old); + }); + + players.length = 0; + FoundYTPlayer = null; + OverridenYTKeys = false; +} + function updateMiniPlayer(iframeObj) { if (iframeObj.isMini) { const element = iframeObj.placeholder; @@ -797,6 +801,24 @@ function get_yt_video_elements() { return elements; } +function isLinkToDifferentPageOnWebsite(url) { + try { + url = new URL(url, window.location.href); + } catch (e) { + return false; + } + + if (url.origin !== window.location.origin) { + return false; + } + + if (url.pathname !== window.location.pathname) { + return true; + } + + return false; +} + function getKeyString(e) { const metaPressed = e.metaKey && e.key !== 'Meta'; const ctrlPressed = e.ctrlKey && e.key !== 'Control'; @@ -892,5 +914,25 @@ if (is_url_yt(window.location.href)) { e.stopImmediatePropagation(); } }, true); +} else { + document.addEventListener('click', (e) => { + let current = e.target; + while (current) { + if (current.tagName === 'A') { + break; + } + current = current.parentElement; + } + + if (!current || !current.href) { + return; + } + + // check if href leads to different page and origin + const url = current.href; + if (isLinkToDifferentPageOnWebsite(url)) { + removePlayers(); + } + }, true); }