content-script 发消息给后台步骤如下
content-script.js
/* content-script */
console.log('content-script 运行了 ...');
// 发消息给后台演示
chrome.runtime.sendMessage(
{greeting: '你好,我是 content-script ,我主动发消息给后台!'},
function(response) {
console.log('收到来自后台的回复:' + response);
}
);
background.js
// background.js
// 监听来自消息
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
console.log('收到消息:');
console.log(request, sender, sendResponse);
sendResponse('我是后台,我已收到你的消息:' + JSON.stringify(request));
});