Why does chrome.runtime does not work on local ip address?

1.5k Views Asked by At

I am using a small function (first answer), that detects the browser. My setup is an apache server which is started with MAMP (Mac) on my local machine. The script is in a javascript file, which is loaded thru the header in html. But even if you test it in the chrome console, you have the same result.

Everything works well, when I use Chrome and localhost as address. (e.g. http://localhost:8888/index.html)

But as soon as I use my local ip address instead of localhost (e.g. http://192.168.0.1:8888/index.html), following javascript code returns false instead of true in Chrome:

!!window.chrome.runtime //it is undefined

And I have absolutely no idea, why this is so or how I can figure out the issue.

JS-Function            |   localhost:8888  |  192.168.0.1:8888
---------------------------------------------------------------
window.chrome.runtime  |     function      |     undefined

Why using local ip address? Because I want to show my colleagues sometimes something without deploying it.

1

There are 1 best solutions below

1
Will Parker On

I had a similar issue where I was trying to call chrome.runtime from a website I was using to check if my extension was installed and it was coming back as undefined.

chrome.runtime.sendMessage(extensionId, { message: 'version' }, (response: any) => {
    if (! response) {
        console.log('No extension');
    }

    console.log('Extension found');
});

The fix for me was to include the IP address in the manifest.json file of my extension under the externally_connectable section:

// manifest.json
{
    // Rest of manifest file
   "externally_connectable": {
       "matches": ["http://<IP_ADDRESS>/*"],
       "accept_tls_channel_id": true
   }
}