Chrome插件:浏览器后台与页面间通信

mac2022-06-30  91

content.js 与 background.js和popup.js 通信和 background.js与popup.js  这些通信都用 chrome.runtime.sendMessage 这个方法 

 background.js和popup.js 与 content.js通信 都用 chrome.tabs.sendMessage 方法

比如

C->P 或者 C->B 或者 b->p

chrome.runtime.sendMessage({name:value},function(){ })

 

P->C  B->C

chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ chrome.tabs.sendMessage(tabs[0].id, {name:value}, function(response) { }); })

3.接收消息都是 

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){   alert(JSON.stringify(message)) //这里获取消息 })

 

转载于:https://www.cnblogs.com/diligenceday/p/10885860.html

最新回复(0)