Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension won't identify part of URL after a # (contentscript.js matches)

I am writing an extension for a particular website (which I do not own), whose urls only vary after a /#/.

Example: starting from the url

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

clicking the "timeline" link leads to the next 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 # and beyond is not included in the matching, so it thinks these are the same page. Also, looking at the console reveals that it doesn't even see any new page loads, despite the fact that I am clicking links, the entire page's content changes, and the url after the /#/ differs.

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

Any thrifty solution would be appreciated!

like image 598
doublefelix Avatar asked Jul 04 '15 04:07

doublefelix


People also ask

Why are my Chrome extensions not working?

Option 1: Clear your cache In the Chrome menu, go to Preferences ⇨ Advanced ⇨ Privacy and Security ⇨ Clear Browsing Data. Make sure the following options are selected: Choose "Since the beginning of time" Cookies and other site and plugin data.

What happened to my Chrome extensions?

If you are using extensions in your Google Chrome web browser, such as Snap & Read, Co:Writer, Kurzweil or Read&Write, you may have come back from summer break and can't find them in your Chrome browser. Until recently you turned on/off your extensions in Google's Chrome web browser by going into Settings > Extensions.

What is ChromeNativeMessaging EXE?

The ChromeNativeMessaging.exe enables communication between the UiPath Extension for Chrome and the UiPath Studio/Robot. Open Chrome. Open Task Manager. Check if the ChromeNativeMessaging.exe process is running.


1 Answers

I believe the anchor bit of the url, also known as the location fragment is always ignored in the Chrome extension match pattern.

However, what you can do is match that particular domain, and use document.location.hash to identify the location anchor, and only run the function in your contentscript.js if it's the correct page. For instance:

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

Naturally, your contentscript.js will only run once per page (since it thinks all the location fragment changes are on the same page). Hence, you'll have to watch out for hash changes using the onhashchange event and rerun the script accordingly.

like image 86
Huey Avatar answered Sep 24 '22 13:09

Huey