3

I am making an extension for chrome. It fetches data from webpages and emails it via local email client. I have a toolbar button which user has to click to invoke the script.

My script works for a few selected urls. I want my toolbar button to change icon based on whether the url is among our list or not. For example for site1 it should be redicon.png and for site2 it should be blueicon.png. I can change button icon using chrome.browserAction.setIcon. But the problem is that this API does not work in content script. It works fine in the background.js file but not in content.js. Kindly tell me how to achieve this.

I know using pageAction instead would do the trick but my client requirement is that the toolbar icon should change rather than appear and disappear.

Haider
  • 938
  • 2
  • 11
  • 25

1 Answers1

5

What you need to read about is message passing. You are right, content scripts have limited chrome API. However, you can contact background page from content script and tell it to execute anything from chrome API for you. First, you need to create a listener on a background page that will be waiting for messages and then send a message from a content script.

Konrad Dzwinel
  • 36,825
  • 12
  • 98
  • 105
  • 1
    Yes I have tried that but the problem is that when the user switches to the new tab the icon is not updated accordingly. I was thinking of using chrome.tabs.onUpdated.addListener but this api is not firing in background script. Don't know what is problem with it. Even chrome.tabs.onRemove is not working ins pite of the fact that it even doesn't require permissions. – Haider Jun 22 '12 at 11:58
  • I see, so your real problem is that `chrome.tabs.onUpdated.addListener` is not being fired. You need to post some code/manifest because it should work just fine on the background page. But how about another solution? Have you thought about http://code.google.com/chrome/extensions/pageAction.html instead of a browser action? These are page/tab specific so it should be *much* easier for you to use them. – Konrad Dzwinel Jun 22 '12 at 12:17
  • I have posted my code here: http://stackoverflow.com/questions/11156479/please-post-an-example-of-chrome-tabs-onupdated-addlistener Yes I have also mentioned in my question about pageAction, lets see if I can convince my client on that. Kindly see if you can post answer to the question that I have put in this comment. – Haider Jun 22 '12 at 12:51
  • thanks a lot for your assistance. I was making a mistake because of which chrome.tab api were not working. Now they are working well, so I have achieved my task. – Haider Jun 22 '12 at 13:35
  • I'm glad to hear that. Good luck with your extension! – Konrad Dzwinel Jun 22 '12 at 13:38