目录
方式2: 通过storage,一方写入,一方读取实现间接通信。
例子1: devtools.js 通过storage方式通信、通过直接调用background.js方法通信
2、panel.js 通过storage方式通信、通过直接调用background.js方法通信
3、background.js / contentScripts.js通过storage方式通信
一、页面间通信的方式
方式1: 通过消息通信
通信API包括:
chrome.extension.sendMessage
chrome.runtime.sendMessage
chrome.tabs.sendMessage
- content.js 给 background.js、popup.js 发消息, background.js给popup.js发消息, 这些通信都用 chrome.runtime.sendMessage 这个方法
- background.js给content.js发消息, popup.js给 content.js发消息 都用 chrome.tabs.sendMessage 方法
接受消息都用:
- chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ alert(JSON.stringify(message)) //这里获取消息 }