我的 chrome 扩展使用长寿命的“端口”对象在“内容脚本”和“弹出”页面之间传递消息。“弹出窗口”能够向“内容脚本”事件侦听器发送消息。但是,“内容脚本”中的“端口”对象无法将消息发送到“弹出”页面。
var port = chrome.extension.connect({"name":"swap"});
// listener for incoming connections
chrome.extension.onConnect.addListener(function( incomingPort ){
// listener on incoming messages
incomingPort.onMessage.addListener(function( msg ){
if( msg.command === 'get_scripts' ){
//do work
}
var scrs = { 'scripts' : 'name' };
var result = port.postMessage( scrs );
});
});
执行 'port.postMessage(Object obj)' 时,插件会抛出以下错误,
Error in event handler for 'undefined': Attempting to use a disconnected port object Error: Attempting to use a disconnected port object
at PortImpl.postMessage (miscellaneous_bindings:54:5)
at chrome-extension://loiamkgdhfjdlkcpehnebipeinpcicfj/swap.js:27:31
at [object Object].dispatch (event_bindings:203:41)
at Object.<anonymous> (miscellaneous_bindings:250:22) event_bindings:207
我尝试使用“端口”对象和“传入端口”对象,都抛出相同的“错误”。感觉它与预先创建的“端口”对象的范围有关。
插件代码可在此 git 存储库https://github.com/snambi/chrome_plugin/tree/master/src/chrome
这个插件有什么问题?