Lea*_*way 7 javascript google-chrome-extension
我有一个向后台脚本发送消息的内容脚本:
chrome.runtime.sendMessage({site: "stackoverflow", options: {
url: url
}}, function(res) {
console.log(res);
});
Run Code Online (Sandbox Code Playgroud)
我的后台脚本有监听器:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.site == "stackoverflow") {
fetch(request.options.url)
.then( (res) => {
return res.text()
})
.then( (document_text) => {
sendResponse({"one": "1"})
})
}
//sendResponse({"one": "1"})
});
Run Code Online (Sandbox Code Playgroud)
现在,在获取过程中,当我尝试发回响应时,我会进入undefined内容脚本。如果我在获取之外使用它:
chrome.runtime.onMessage.addListener(
//...
.then( (document_text) => {
//sendResponse({"one": "1"})
})
}
sendResponse({"one": "1"})
});
Run Code Online (Sandbox Code Playgroud)
我的内容脚本日志{"one": "1"}。
为什么我会遇到这个问题?
| 归档时间: |
|
| 查看次数: |
1610 次 |
| 最近记录: |