我试过这个:
背景.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) {
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"]
}
]
}