1、插件内部发送消息

//onMessage消息监听
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
	console.log(request.text); 			//打印出来的值：“我是个测试内容”
	sendResponse({'msg':'触发成功了'});	//返回一个内容到发送消息的回调函数中
});

//发送消息，触发上面的onMessage
chrome.runtime.sendMessage('', {text: '我是个测试内容'}, function (response) {
	console.log(response.msg); 			//打印的内容是：“触发成功了”
});

2、外部网站给插件发送消息，需要在manifest里面配置

externally_connectable:{matches:["https://*.xxx.com/"]}

只有配置了对应的地址的页面才能给插件发送消息

//onMessageExternal消息监听

chrome.runtime.onMessageExternal.addListener(function (request, sender, sendResponse) {
	console.log(request.text); 			//打印出来的值：“我是个测试内容”
	sendResponse({'msg':'触发成功了'});	//返回一个内容到发送消息的回调函数中
});

//发送消息，触发上面的onMessageExternal
//第一个参数是插件Id，指定要发送给哪个插件
//第二个参数是想要传给插件的数据信息
//第三个是让插件那边调用的回调函数，触发回来
chrome.runtime.sendMessage('chromeId', {text: '我是个测试内容'}, function (response) {
	console.log(response.msg); 			//打印的内容是：“触发成功了”
});
