我的内容脚本向background.js发送消息。在响应的回调处理程序中,我有最后一行为"alert(alrt_msg)"
。在执行的代码,我得到的警告框,但点击“确定”后,在红继铬控制台显示:控制台中的Chrome扩展程序投掷错误
Error in event handler for 'undefined': Cannot call method 'disconnect' of null TypeError: Cannot call method 'disconnect' of null
at chromeHidden.Port.sendMessageImpl (miscellaneous_bindings:285:14)
at chrome.Event.dispatch (event_bindings:237:41)
at Object.chromeHidden.Port.dispatchOnMessage (miscellaneous_bindings:250:22)
它没有太多的信息,但直觉我知道这个错误是因为一些结构的地方就是垃圾当用户点击“确定”时收集。所以我把我的代码
window.setTimeout(function() { alert(alrt_msg); } , 1);
这使得它没有上面的错误工作。尽管如此,在搜索文档或谷歌后,我无法找到背后的确切原因。有人可以解释发生了什么,如果文档包含一个特定的方法来做到这一点?
你的代码是什么?如果在'onMessage'事件监听器的末尾插入'return true;'会怎么样? –
我相信原因是消息响应的处理程序不会等待警报被点击;所以当你关闭警报以允许脚本继续时,处理程序不再存在。我不确定这是否可以被视为一个错误。 – Goodwine
@Goodwine,我相信你是对的。 – Methos