Chrome extension won't identify part of url after matching # (contentscript.js)

I'm writing an extension for a specific website (which I don't have) whose urls only change after / # /.

Example: starting with a URL

.../#/PF/charts/patients/419d3081-5e20-4347-a852-52b2c333ce85/summary

      

clicking on the "timeline" link leads to the following url

.../#/PF/charts/patients/419d3081-5e20-4347-a852-52b2c333ce85/timeline/encounter

      

... but when trying to match the url to load my content script everything from C # and up doesn't match, so it thinks it's the same page. Also, looking at the console shows that it doesn't even see any new page loads, even though I click the links, the content of the whole page changes, and the url after / # / is different.

How can I get my articlecript.js to only work where I need it to, if the normal tools (setting "content_scripts" in the manifest) won't work? Or, can I make the url after / # / visible somehow?

Any economical solution would be appreciated!

+3


source to share


1 answer


I suppose the url anchor bit, also known as location fragment, is always ignored in the chrome extension matching pattern .

However, what you can do is match that specific domain and use document.location.hash

location anchor and only run the function in yours contentscript.js

if it is the correct page. For example:



if (("onhashchange" in window){
    window.onhashchange = function () {
        if(document.location.hash == '#/PF/charts/patients/419d3081-5e20-4347-a852-52b2c333ce85/summary'){
            //do summary stuff
        }
    }
} 

      

Naturally, yours contentscript.js

will only work once per page (since it assumes that all location snippet changes are on the same page). Hence, you will need to watch for hash changes using an event onhashchange

and rerun the script.

+4


source







All Articles