I'm making a simple Chrome Extension that pops out a prompt when entering certain websites. My main one would be Youtube. Here's the code that I have so far:
if(window.location.hostname ==="www.youtube.com") {
let why = prompt("Why are you on Youtube/Watching this video?", "");
};
This works fine when entering youtube for the first time, when refreshing a page, or when going back a page using the back arrow. However, I would like it to pop up whenever I click a video, or switch pages (for example, going from the Recommended tab to the Subscriptions tab). How can I do this?
EDIT: I found code that works on this question: Detect Youtube video change with injected javascript Here's my final code:
function run(){
if(window.location.hostname ==="www.youtube.com") {
let why = prompt("Why are you on Youtube/Watching this video?", "");
};
}
window.onload = run;
window.addEventListener('yt-navigate-start', run, true);