我是谷歌Chrome扩展程序的新用户,我为我们的网站创建了一个检查您所在网页内容的网站,并根据该网页获取服务器的ID(我们有一个包含4个虚拟机的网络农场主) 。现在使用服务器ID,我不想更改扩展图标以显示那里的数字。我尝试过使用:
chrome.browserAction.setIcon({
path : folder + icons[2],
tabId: tab.id
});
但是我收到了这个错误: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.
我尝试使用Google搜索错误并一直在搜索文档,但无法找到导致此错误的原因...
答案 0 :(得分:21)
内容脚本无权访问大多数扩展API。相反,您需要使用message passing让内容脚本警报通知后台页面需要完成的工作。
您的内容脚本应使用chrome.runtime.sendMessage
发送消息,后台页面应使用chrome.runtime.onMessage.addListener
收听:
内容脚本:
if(shouldChangeIcon) {
// send message to background script
chrome.runtime.sendMessage({ "newIconPath" : folder + icons[2] });
}
背景页:
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
});
});