0

我尝试连接到刚刚创建的新选项卡,我的最终任务是每次尝试通过 id 连接到选项卡时以任何方式更新所有打开的选项卡上的 html 小部件,但因错误而失败。我正在使用版本 23.0.1271.64 m

var channelTabs = [];
  function isInclude(arr,obj) {
    return (arr.indexOf(obj) != -1);
}
    chrome.tabs.onCreated.addListener(function(tab) {

      // add tab when opened
     if (channelTabs.indexOf(tab.id) == -1) {
        channelTabs.push(tab.id);
     }

    chrome.windows.getCurrent(function(win)
    {
        // get an array of the tabs in the window
        chrome.tabs.getAllInWindow(win.id, function(tabs)
        {
            for (i in tabs) // loop over the tabs
            {
                // if the tab is not the selected one
                if(isInclude(channelTabs,tabs[i].id))
                {
                    if(/^(https?|file):/.test(tabs[i].url))
                    {
                        console.log('Debug Background sending update to open tab id:'+tabs[i].id);
                        var port = chrome.tabs.connect(tabs[i].id,{name: "content_tab_request"});
                        port.postMessage({resp: "tab_update",data:"some string song1"});
                    }
                }

            }
        });
    });


    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo,tab) {
    // Example: allow http:, https: and file:
    if (/^(https?|file):/.test(tab.url)) {
            console.log('Debug Background sending onUpdated to open tab id:'+tabId);
            var port = chrome.tabs.connect(tabId,{name: "content_tab_request"});
            port.postMessage({resp: "tab_update",data:"some string song1"});
    }
    });


 });

但每次它尝试 chrome.tabs.connect 它给我:

Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:235
chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:235
chrome.Event.dispatch_ event_bindings:371
dispatchArgs event_bindings:249
chromeHidden.Event.dispatchEvent

和内容脚本:

// Register this tab to the background script
  var port = chrome.extension.connect({name: "content_request"});
  port.postMessage({req: "Hello"});

  port.onMessage.addListener(function(msg) {

    if (msg.resp == "World")
    {      
      port.postMessage({answer: "good"});
    }
    else if(msg.answer == "bye")
    {
         console.log('Debug contentscript.js reciving answer from background msg.answer:'+msg.answer);
    }
    else
    {
         console.log('Debug contentscript.js reciving answer from background is wrong:'+msg);
    }

    if(port.name == "content_tab_request")
    {
        console.log('Debug contentscript.js reciving request from background Tab function:'+msg);
        if(msg.resp=="tab_update ")
        {
            var data_recive = msg.data;
            console.log('Debug contentscript.js reciving request data from background Tab to update page data_recive:'+data_recive);

        }
    }
  });

.json 文件:

{
   "background": {
      "page": "background.html" 
   },
   "content_scripts": [
    {
      "matches": ["<all_urls>"],       
      "js": ["contentscript.js"],
      "run_at": "document_start",
      "all_frames": true
    }
   ],
   "web_accessible_resources": [
    "script_inpage.js"
   ],
   "browser_action": {
      "default_icon": "icon19.png",
      "default_popup": "popup.html",
      "default_title": "Simple test"
   },
   "content_security_policy": "script-src 'self'; media-src *; object-src 'self'",
   "description": "Simple test.",
   "icons": {
      "128": "icon128.png",
      "16": "icon16.png",
      "32": "icon32.png",
      "48": "icon48.png"
   },

   "manifest_version": 2,
   "minimum_chrome_version": "20",
   "name": "Simple test",
   "permissions": [ 
        "unlimitedStorage",
        "http://*/",
        "<all_urls>",
        "tabs"
   ],

   "version": "2.6"
}

顺便说一句,从内容脚本到后台的连接工作得很好!

4

2 回答 2

3

好的,您似乎正在尝试同步异步事件,在简化您的代码后,我通过修改使其运行并实现了您的posting message to all tabs when a new tab is created.

Output:

我能够在所有选项卡上看到此消息

在此处输入图像描述

清单.json

{
"name":"Sample communication from content to background",
"description":"This is a sample for Simulating communication from content to background",
"manifest_version":2,
"version":"2",
"background":{
    "scripts":["background.js"]
},
"content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["myscript.js"],
      "run_at":"document_start"
    }
  ]
}

“背景.js”

chrome.tabs.onCreated.addListener(function(tab) {
     chrome.windows.getCurrent(function(win){
        chrome.tabs.getAllInWindow(win.id, function(tabs){
            for (i=0;i<tabs.length;i++){
                console.log(tabs[i]);
                chrome.tabs.sendMessage(tabs[i].id,{name: "content_tab_request"});
            }
        });
    });
});

“我的脚本.js”

chrome.extension.onMessage.addListener(function(msg) {
    console.log("Message Recieved   "+ msg); 
  });
于 2012-11-26T14:25:09.133 回答
0

此外,一些 chrome 扩展可能会产生此错误。我遇到了这个问题,问题在于扩展(用于将文章保存到 Pocket)。错误是:

端口错误:无法建立连接。接收端不存在。miscellaneous_bindings:235 chromeHidden.Port.dispatchOnDisconnect

于 2012-12-30T08:28:08.967 回答