遇到的问题:
Chrome 插件开发,需要实现 content 页面使用 chrome.runtime.sendMessage 发送消息给 background,background 需要异步处理完消息以后再发送处理结果给content 页面。
解决思路和方法:
google 找到的解决方法:
https://stackoverflow.com/questions/14094447/chrome-extension-dealing-with-asynchronous-sendmessage
- 关键代码(解决我问题的答案,来自链接网页)
// content.js
chrome.runtime.sendMessage({
type: "GET_FOO" }, function (response) {
console.log(response.foo);
});
// background.js
// replace with a real call that
// needs to run asynchronously
async function getFoo() {
return