我无法将消息从后台页面传递到内容页面,下面是我的代码。我尝试从扩展库中下载代码示例,但即使在此之后也无法正常工作,
background.js:
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.sendMessage(tab.id, {
greeting: "hello"
}, function (response) {
alert(JSON.stringify(response, null, 4));
});
});
content.js:
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
sendResponse({farewell: "goodbye"});
});
的manifest.json:
{
"name": "Test Screenshot Extension",
"version": "1.3",
"description": "Demonstrate screenshot functionality in the chrome.tabs api.",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"icons": {
"128": "icon-128.png",
"48": "icon-48.png"
},
"browser_action": {
"default_icon": "icon-128.png",
"default_title": "Take a screen shot!"
},
"permissions": ["tabs"],
"manifest_version": 2
}
警告线(JSON.stringify(响应,空,4)); 警告"未定义"