Google chrome 背景脚本chrome.tabs在chrome扩展中未定义?
我试过这个: background.jsGoogle chrome 背景脚本chrome.tabs在chrome扩展中未定义?,google-chrome,google-chrome-extension,Google Chrome,Google Chrome Extension,我试过这个: background.js chrome.browserAction.onClicked.addListener(function (activeTab) { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
chrome.browserAction.onClicked.addListener(function (activeTab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
});
foo.js:
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
new contentFunction()
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello") {
sendResponse({ farewell: "goodbye" });
}
});
舱单:
{
"manifest_version": 2,
"name": "foo",
"browser_action": {
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"activeTab",
"tabs",
"<all_urls>"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["foo.js"]
}
]
}
{
“清单版本”:2,
“名称”:“foo”,
“浏览器操作”:{
},
“背景”:{
“脚本”:[“background.js”]
},
“权限”:[
“活动标签”,
“标签”,
“我正试图通过按扩展按钮调用contentscript js,它在不同的上下文中运行,因此您需要使用@wOxxOm thx,我看不到在网络选项卡中下载的内容脚本进行测试。扩展单击调用后台函数,这很好。您的代码几乎正确:使用chrome。tabs
。sendMessage。”(activeTab.id,{问候语:'hello'},…chrome.tabs未定义@wOxxOm