Joe*_*lum 4 javascript-events google-chrome-extension
我正在尝试使用一个消息界面在我的弹出窗口之间传递消息到背景页面(我使用类似于Google示例中的代码).
弹出:
chrome.extension.sendMessage( {msg: "this is popup's msg"}, function(b){
alert('this is popups callback func' +b.backgroundMsg);
});
Run Code Online (Sandbox Code Playgroud)
这就是我在background.js中监听(和回复)的方式:
chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
sendResponse({backgroundMsg: "this is background msg"});
});
Run Code Online (Sandbox Code Playgroud)
现在,当我检查控制台中的所有内容时,消息交换正常但我收到以下错误:
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)
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
复制代码示例并使用清单和弹出结构填充空白会导致完全正常工作的扩展没有错误.不确定为什么会收到您分享的错误.尝试我的代码,看看你是否可以摆脱错误.
{
"name": "Stackoverflow Message Passing Example",
"manifest_version": 2,
"version": "0.1",
"description": "Simple popup with message passing for Stackoverflow question",
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
}
}
Run Code Online (Sandbox Code Playgroud)
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
sendResponse({backgroundMsg: "this is background msg"});
});
Run Code Online (Sandbox Code Playgroud)
<html>
<head>
<script src="popup.js"></script>
<style type="text/css" media="screen">
body { width: 100px; height: 100px; }
</style>
</head>
<body>
<p>Check the console</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
chrome.runtime.sendMessage( {msg: "this is popup's msg"}, function(b){
console.log('this is popups callback func ' + b.backgroundMsg);
});
Run Code Online (Sandbox Code Playgroud)
单击带有浏览器窗口的扩展按钮,打开到exampley.com

扩展弹出窗口的控制台日志

这是我使用的文件的压缩文件http://mikegrace.s3.amazonaws.com/stackoverflow/simple-popup.zip
| 归档时间: |
|
| 查看次数: |
4138 次 |
| 最近记录: |