Content script not loading if orignial url doesn't match

I’m working on an extension that edits specifically you-tube video pages so i added the following to my manifest.json:
"content_scripts": [ { "matches": ["*://*.youtube.com/watch*"], "js": ["src/content/blockComments.js"] } ]
However i noticed that the content script doesn’t always load in the you-tube page and after some testing i found out how to make it appear:

  1. Open the video page in a tab making it’s original url match the one for the content script.
  2. If i entered the video page where the original url of the tab wasn’t a youtube video refresh the tab and content script will now be added.

I tested this with simple logs and got the same results, is this intentional or a weird bug or am i missing something?

1 Like

This is a consequence of how youtube.com does navigation, it uses history.pushState to change the url without reloading the page. Unfortunately you’ll have to match against youtube.com/* and check when it “navigates” to another page to inject (again).

1 Like

Yeah after testing with pure youtube path it works, kind of annoying since it means that having for more specific paths just doesn’t work for sites like youtube.

Thanks for the help :slight_smile: