0

I'm developing an extension to the Chrome and I'd like to filter WebNavigation.OnCompleted event so that it's only fired on certain domains.

I'm using it like this:

chrome.webNavigation.onCompleted.addListener(function(details) {
    // some code here...
}, {
      url : {hostEquals : 'www.foo.bar'}
   });

That works. Then I started testing this on a test page, which is located on my local computer. That's when I ran into the problem: what's the value of the filter when URL points to a file located on the local computer (i.e. what's the hostname of a local file from Chrome event filter perspective):

file:///home/usr/testfile.html

I know, the URL doesn't really contain a hostname, but I think it should be possible to filter these kind's of addresses too. I've tested different variations, like 'file:///', 'localhost' and '/' but none of them seem to get the job done. Leaving the filter empty equals no filtering at all.

The extension works fine without the filter, but I need to get this system to work with it.

1 Answer 1

1

To match a file URL that starts with "/home/user/", just use urlPrefix, e.g. as follows:

chrome.webNavigation.onCompleted.addListener(function(details) {
    // Some code here...
}, {
    url: [{
        hostEquals: 'example.com',
    }, {
        // Note: This filter will only match if
        // 1) You've declared the "file://*" or "<all_urls>" permission
        //    in manifest.json, and
        // 2) You've visited chrome://extensions and ticked the checkbox
        //    "Allow access to file URLs" at your extension.
        urlPrefix: 'file:///home/user/'
    }],
});

For other filters, see the documentation at chrome.webNavigation.onCompleted.

PS. The host name of a file:-URL is whatever that comes between the file:// and the path. In case of file:///, it's an empty string.

1
  • Ok, I thought there would be all-in-one solution that works for every situation.
    – Gigga
    Dec 14, 2015 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.