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

1

1

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.

Ingeingeberg answered 6/2, 2020 at 16:4 Comment(3)
This could be a duplicate of this question. This might be a heisenbug, and not be related to the change of IP.Ere
Also, if you are on a Windows domain system, Chrome can be affected by Group Policy settings ("Trusted Sites"), which would naturally exclude localhost. I don't think this is what is causing this issue, but its something to investigate as a possibility, at least.Ere
@Amy I took a look into your linked question. I've no breakpoints set and console was closed and this still appears. I'll add the information that I'm working on a Mac.Ingeingeberg
T
4

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
   }
}
Terle answered 13/5, 2021 at 5:5 Comment(1)
This should be accepted as the answer, I would also suggest editing your host file for your OS and pointing a generic local domain like myapp.local to the local network ip then adding myapp.local to the manifestErb

© 2022 - 2024 — McMap. All rights reserved.