1

I'm developing a Google Chrome extension, which produces an error I can't fix.

My manifest.json looks like this:

{
    "name": "my extension",
    "version": "1.0",
    "background_page": "background.html",
    "permissions": [
        "tabs",
        "<all_urls>"
    ],
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["content.js"],
            "all_frames": true
        }
    ]
}

background.html tries to talk with content.js:

<script>
chrome.tabs.onUpdated.addListener
(
    function(tabId, changeInfo) 
    { 
        chrome.tabs.sendRequest(tabId, {action : 'getMyValue'}, function(output) {
            console.log(output);
        });
    }
);
</script>

Finally, content.js:

chrome.extension.onRequest.addListener(function(request, sender, callback)
{
    if (request.action == 'getMyValue')
    {    
        callback('test');
    }
});

Developer Tools console prints: "Port error: Could not establish connection. Receiving end does not exist." in "miscellaneous_bindings" on line 232.

Any ideas?

Rob W
  • 341,306
  • 83
  • 791
  • 678
TorbenL
  • 1,219
  • 1
  • 11
  • 15
  • 1
    Can't reproduce. Any conflicting extensions? Also, are you aware that `onUpdated` is fired twice for each pageview? – Rob W Jul 04 '12 at 13:34
  • How can i figure out if there are any conflicting extensions? Is it possible to debug extensions in incognito mode? – TorbenL Jul 04 '12 at 14:50
  • 1
    A1. Either start Chrome using a new profile (`chrome --user-data-dir=%tmp%\%random%` in Windows, `chromium-browser --user-data-dir=/tmp/$RANDOM` under Linux), or disable all extensions, and add them one-by-one. A2. See [this answer](http://stackoverflow.com/a/10258029/938089?google-chrome-extension-logging-from-background-js). – Rob W Jul 04 '12 at 14:56
  • @RobW same error in incognito mode (no other extensions activated) :( – TorbenL Jul 04 '12 at 15:08
  • @RobW New chrome profile prints "Port error: Could not establish connection. Receiving end does not exist.", too. function: chromeHidden.Port.dispatchOnDisconnect – TorbenL Jul 04 '12 at 15:12
  • @RobW "Chrome 20.0.1132.47 m" under Windows 7. – TorbenL Jul 04 '12 at 15:38
  • Another way, which solve my problem with exactly this same error: http://stackoverflow.com/a/13565529/390747 – WooCaSh Nov 26 '12 at 13:13

1 Answers1

4

chrome.tabs.onUpdated runs when a tab updates. Dev tools, chrome (extension) pages etc. are also included. To get rid of the error, you have to filter the URLs you cannot access.

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
    // Example: allow http:, https: and file:
    if (changeInfo.status === 'complete' &&
       /^(https?|file):/.test(changeInfo.url)) {
        chrome.tabs.sendRequest(tabId, {action: 'getMyValue'}, function(output) {
            console.log(output);
        });
    }
});
Rob W
  • 341,306
  • 83
  • 791
  • 678