burtek
burtek

Reputation: 2685

Chrome WebNavigation Listener not working

I'm creating my own extension for Google Chrome (for my own use, not to be published). At the moment I have two files:

manifest.json:

{
  "manifest_version": 2,
  "name": "abcdef",
  "description": "abcdef",
  "version": "0.1",
  "permissions": [
    "tabs",
    "webNavigation",
    "http://www.ztm.waw.pl/*"
  ],
  "background": {
    "scripts": ["bg.js"],
    "persistent": false
  }
}

bg.js:

chrome.webNavigation.onCompleted.addListener(function(o) {
  chrome.tabs.executeScript(o.tabId, {
    code: "alert('ok');"
  });
}, {
  url: {
    hostContains: 'ztm.waw.pl'
  }
});

I want the alert box to appear when I navigate to http://www.ztm.waw.pl, but it does not work. Could someone tell me why?

Upvotes: 0

Views: 3902

Answers (1)

user2384183
user2384183

Reputation:

The url property of chrome.webNavigation.onCompleted accepts an array of chrome.events.UrlFilter (source), so you'll need to change your bg.js to this (note the square and curly brackets in the url property):

chrome.webNavigation.onCompleted.addListener(function(o) {
  chrome.tabs.executeScript(o.tabId, {
    code: "alert('ok');"
  });
}, {
  url: [
    {hostContains: 'ztm.waw.pl'}
  ]
});

Upvotes: 3

Related Questions