最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Jul 4, 2015 at 4:39 don't train ai on me asked Jul 4, 2015 at 4:20 don't train ai on medon't train ai on me 1,1121 gold badge10 silver badges26 bronze badges 1
  • 1 Also c stackoverflow./q/15959090/632951 – Pacerier Commented Oct 17, 2017 at 8:59
Add a ment  | 

1 Answer 1

Reset to default 7

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.

发布评论

评论列表(0)

  1. 暂无评论