本文介绍了在内容脚本中调用chrome.browserAction.setIcon的方式与在后台脚本中完成的方式相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对Chrome进行扩展。它从网页提取数据并通过本地电子邮件客户端发送电子邮件。我有一个工具栏按钮,用户必须单击才能调用脚本。

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.

我的脚本适用于几个选定的URL。我希望我的工具栏按钮根据url是否在我们的列表中来更改图标。例如,site1应该是redicon.png,而site2应该是blueicon.png。我可以使用chrome.browserAction.setIcon更改按钮图标。但问题是这个API在内容脚本中不起作用。它在background.js文件中工作正常,但不在content.js中。请告诉我如何做到这一点。

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.

我知道使用pageAction可以做到这一点,但我的客户要求是工具栏图标应该改变而不是出现并消失。 / p>

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.

推荐答案

您需要阅读的内容是。你说得对,内容脚本的Chrome API有限。但是,您可以联系内容脚本的后台页面,并告诉它从Chrome API执行任何操作。首先,您需要在将等待邮件的背景页上创建然后从内容脚本。

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.

这篇关于在内容脚本中调用chrome.browserAction.setIcon的方式与在后台脚本中完成的方式相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:37