1

So simply when I land a page on youtube which starts with w* (meaning that a specific video with its ID) I'm firing alert(url) and it works however it only works when I land that page from the URL and/or on refresh. What I'm trying to achieve is to fire that alert when navigating to that page (watch?=...) FROM another youtube page meaning that I'm navigating internally.

as an example: www.youtube.com -> this is the landing page of youtube I click on a video and I navigate to: www.youtube.com/watch?=aiFUQ9981Qk

this DOES NOT trigger my alert HOWEVER when I refresh, it does trigger. how do i trigger even when i navigate 'FROM' 'WITHIN' youtube ? Any input would be appreciated. manifest and Content.js attached below...

manifest.json

{
    "manifest_version": 2,
    "name": "youtest",
    "version": "1.0.0",
    "content_scripts": [
        {
            "matches": ["https://*.youtube.com/watch?*"],
            "js": ["Content.js"]
        }
    ]
}

Content.js

console.log(window.location.href);
alert(window.location.href);
emre-ozgun
  • 676
  • 1
  • 7
  • 17
  • 1
    I havent used chrome extensions before but I imagine you need a eventlistner perhaps listen to `popstate` and then run whatever you like – Breezer Nov 14 '21 at 02:03
  • I think I've observed the problem. Thank you for your input. Not having a full page refresh seems to be the problem. – emre-ozgun Nov 14 '21 at 02:09

1 Answers1

0
function runScript() {
    if (window.location.href.indexOf('watch?') > -1) {
        alert('this is a video');
        let videoID = window.location.href.slice(
            window.location.href.lastIndexOf('/')
        );
        alert(videoID);
    } else {
        alert('not a video');
    }
}

document.addEventListener('yt-navigate-start', runScript);
emre-ozgun
  • 676
  • 1
  • 7
  • 17