17

I'm new to Google Chrome extensions and I've created one for our website that checks the content of the page you're on and bases on that gets the ID of the server (we have a webfarm with 4 VM's). Now using the server ID, I wan't to change the extension icon to show the number there. I've tried using :

chrome.browserAction.setIcon({
    path : folder + icons[2],
    tabId: tab.id
});

But I'm getting this error: chrome.browserAction is not available: You do not have permission to access this API. Ensure that the required permission or manifest property is included in your manifest.json.

I've tried googling the error and have been searching through the documentation, but can't find what is causing this...

Richard
  • 4,341
  • 5
  • 35
  • 55
  • Do you have a `browser_action` defined in your manifest? Missing that in your manifest is the only thing I can think of that would cause such an error (outside of using the canary/dev/beta version of the browser). – apsillers Jun 04 '13 at 15:33
  • ` "browser_action": { "default_icon": "numbers/1green.png", "default_popup": "index.html" }` Is what I have.. – Richard Jun 04 '13 at 15:35
  • I've tried in my "normal" Chrome in Dev mode and on Canary – Richard Jun 04 '13 at 15:37
  • 1
    *Where* are you trying to run this code? I would expect it to succeed in a background page, and fail in a content script. I'm unsure about browser action or page action popups. – apsillers Jun 04 '13 at 15:44
  • It is indeed in a content script. I moved the file declaration to the `background`, but now nothing seems to happen. FYI; what I aim to do here is update the icon based on the content of the page that I visit in the browser. I'm not performing any background tasks to poll a database or anything and display results from that. – Richard Jun 04 '13 at 17:30

2 Answers2

30

Content scripts don't have access to most extension APIs. Instead, you'll need to use message passing to have the content script alert notify the background page of what works needs to be done.

Your content script should send a message using chrome.runtime.sendMessage, and the background page should listen using chrome.runtime.onMessage.addListener:

Content script:

if(shouldChangeIcon) {
    // send message to background script
    chrome.runtime.sendMessage({ "newIconPath" : folder + icons[2] });
}

Background page:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        // read `newIconPath` from request and read `tab.id` from sender
        chrome.browserAction.setIcon({
            path: request.newIconPath,
            tabId: sender.tab.id
        });
    });
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • Okay, is there any way I can track what's happening in the background? Have set it up now, but the icon isn't changing. Have got a background.js that is declared in `background` and a myscript.js as `content_script`. Stuff in myscript.js that fires `console.log()` does work, but still the same icon.. – Richard Jun 04 '13 at 17:55
  • 1
    Perhaps you could use [Where to read console messages from background.js in a Chrome extension?](http://stackoverflow.com/a/10258029/710446) – apsillers Jun 04 '13 at 17:58
3
Unchecked runtime.lastError: Icon invalid.

Just in case that any one encounter the above error when change browserAction icon, I need to clarify: not all sizes of image is supported, but 48x48 is a valid one.

Iceberg
  • 2,744
  • 19
  • 19