Skip to content

Commit

Permalink
Fix #87
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrews54757 committed Feb 22, 2024
1 parent 0f368a9 commit c2bc544
Showing 1 changed file with 60 additions and 18 deletions.
78 changes: 60 additions & 18 deletions chrome/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}

0 comments on commit c2bc544

Please sign in to comment.