Blair
Blair

Reputation: 101

Is it possible to match the url fragment in a chrome extension?

Currently using match patterns in chrome.contextMenus.create#targetUrlPatterns for a chromium extension, however I've run into an issue. Is it possible to somehow filter URLs by their fragment (the string following the hash)?

My understanding is that match patterns do not apply to the fragment, however for content scripts you can use include_globs and exclude_globs to filter. Is there a similar alternative for the target url patterns in context menus?

Please note that I am reluctant to manually check in the onclick function using regex, whilst I have already tested this, it is desirable to hide a context menu rather than having it do nothing.

Example urls I would like to match:

www.example.com/foo.php#123bar456 //should show contextmenu
www.example.com/foo.php#123baz456 //should hide contextmenu

Please note that the numbers in the example are unknown. My initial attempt is below:

//background.js
chrome.contextMenus.create({
    ...
    contexts: ["link"]
    targetUrlPatterns: "*://*.example.com/foo.php*bar*"

Obviously the above won't work, but is there any alternative to having to manually check the fragment on click?

Upvotes: 0

Views: 198

Answers (0)

Related Questions