1

I have a Chrome Extension (currently manifest v2, but will soon upgrade to v3) that needs to work with a SPA that simulates page changes by changing the hash in the URL.

Example:

Page 1: https://www.example.com/somedir/#page1
Page 2: https://www.example.com/somedir/#page2

I have separate content scripts for #page1 and #page2, and a default content script for other pages on the site.

The problem I'm having is that the page initially loads without a hash, and then after a few seconds of delay, the application adds the #page1 hash. In other words, when the user navigates to #page1:

  • The site initially loads as https://www.example.com/somedir/
  • My Chrome Extension content script for #page1, which looks for the include glob *#page1, does not see the #page1 hash in the URL and falls back to the default content script
  • After a few seconds of delay, the site appends the #page1 hash to the URL, presumably by setting location.hash (i.e., the URL is now https://www.example.com/somedir/#page1)

I think what I'm looking for is some way to detect that the #page1 hash has been added to the URL, and then force the extension to re-execute and inject the correct content scripts for #page1. Is there a way to do that? If not, is there some other method to workaround this timing issue?

Thank you.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Jon Sagara
  • 1,122
  • 2
  • 11
  • 16
  • I don't know if I understand your problem perfectly and if I can give you some good idea. To understand better I need some clarification. 1. Is the content of the page with the hash in URL significantly different from the content of the previously loaded page without the hash? or is the document just scrolled to view an anchor? I am not aware that the hash can also be specified in the possible patterns for injecting content scripts. I did not find anything about it in the official documentation, but that does not mean that it should \ could not really work. – Robbi May 07 '21 at 10:07
  • 2. Could the generic content script you are talking about affect in some way (irreversibly) the operating logic of your extension? In practice, as soon as it intercepts the generic page, does the content script do something that is clearly in contrast with what it will do next when it intercepts the page with the hash? – Robbi May 07 '21 at 10:07
  • 2
    You can use a single content script and detect the changes as explained [here](/a/39508954). – wOxxOm May 07 '21 at 10:59

0 Answers0