小编Pan*_*ora的帖子

多个 chrome.runtime.onMessage-Listeners - 断开端口错误

I'm上使用IndexedDB的-包装铬扩展工作Dexie,几个jQuery的库和syncfusions eGrid显示和修改数据库。我知道这个问题以前被问过几次,但现在我发现了一些奇怪的行为。

我的设置:

  • 背景脚本
  • 内容脚本(注入网站)
  • index.html(用于显示网格)
  • 清单文件

当我重新加载我的扩展程序时,一切都很好。我正在用我的内容脚本注入一个可滚动的名称列表。当我重新加载选项卡时,这也有效。但是当我之前打开 index.html (官方文档中的 popup.html)时,来自后台脚本的 sendResponse 会在一段时间后停止工作。重新加载选项卡将无济于事。我搜索了几个小时,在 googles bugtracker 上找到了很多类似的案例和一些条目,他们声称这个问题已经解决。我也知道如果我有像 indexedDB 这样的异步函数,我必须使用 return true 。

因此,为了给您更多信息,我将列出我缩短的脚本。

内容脚本

chrome.runtime.sendMessage({greeting: 'getNames'},function(response){
    $('.names-container').append(response.farewell);
});
chrome.runtime.sendMessage({greeting: 'getData'+name},function(name){
    chrome.runtime.sendMessage({greeting: 'delete'},function(response){
        console.log(response.farewell);
    });
    chrome.runtime.sendMessage({greeting: 'set'}, function(response) {
        console.log(response.farewell);
    });
});
Run Code Online (Sandbox Code Playgroud)

后台脚本

chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
  if(message.greeting == 'getNames'){
    // Get array of names from idb and send it back
    idb.opendb().then(function(a){
      sendResponse(a);
    });
    return true;
  } else if(message.greeting.indexOf('getData') >= 0){
    // get more information for selected name
    idb.opendb().then(function(a){
      sendResponse(a); …
Run Code Online (Sandbox Code Playgroud)

javascript jquery json google-chrome-extension indexeddb

5
推荐指数
1
解决办法
2664
查看次数