javascript - 即使我返回 true,Chrome 事件侦听器 SendResponse 也不会发送
问题描述
遇到与Chrome 扩展消息传递相同的问题:未发送响应。但是,当我实施建议的解决方案时,它并不能解决我的问题。我得到了完全相同的错误,并且在返回响应之前还调用了一个异步方法。这里出了什么问题???
背景.js
chrome.runtime.onMessage.addListener(
async function (request, sender, sendResponse) {
if (request.action === "getAllTabs") {
// if I uncomment this line, content.js receives this response
// sendResponse({data: "hey"})
chrome.tabs.query({}, function(tabs){
// but not this one??
sendResponse({data: tabs});
})
console.log('returning true');
return true;
我的 content.js:
chrome.runtime.sendMessage({action: "getAllTabs"}, function(response) {
console.log(response);
});
解决方案
推荐阅读
- ios - 我想要自定义的 UITextfield,它具有与 android 相同的占位符行为,我也想要 UITextfield 中的错误标签
- phpstorm - PhpStorm is not opening on my Ubuntu 14.04
- iphone - 屏幕冻结,需要重新启动才能快速制作可点击的屏幕
- selenium - 使用封闭标记下方的文本查找 XPath
- java - 完成后如何反转动画?
- html - xs bootstrap4 中的订单优先
- reactjs - ElectronJS React 自定义菜单事件未定义
- macos - 当架构正确时,为什么链接器会抱怨“文件是为归档而不是被链接的架构而构建的”?
- c# - 创建从数据源映射值的 DataGridView 列
- php - Laravel 使用方法 when() 安排作业