控制台中的Chrome扩展程序抛出错误

时间:2012-09-19 19:38:56

标签: google-chrome-extension

我的内容脚本向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);

这使它工作没有上述错误。仍然,在搜索文档或谷歌后,我无法找到这背后的确切原因。有人可以解释一下,如果文档中包含特定的方法吗?

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。这是由于在响应处理程序中调用的函数中的错误(我的代码的错误)。

这是我的代码:

在background.js中 - 听力方

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    var update_msg = 'test';
    sendResponse({data: update_msg});
    return false; // -> Add this or not add this do not matter
  });
}

在发送方:

function updateObjDatabase(obj) {
  chrome.runtime.sendMessage({data: obj}, function(response) {
    // Update when it's done
    update_msg_handler(response.data);
  });
}

function update_msg_handler(msg) {
  ..... error code here
}

错误的代码实际上在函数update_msg_handler中。但Chrome控制台不会告诉您这些细节。如果没有IDE,则必须添加一些console.log进行调试。

答案 1 :(得分:2)

我有同样的错误。这是iMacros应用程序。删除后没有错误。

相关问题