0

I would like to obtain the IP address of the user in a Chrome app. The Content Security Policy directive prevents me from using a service such as IPify, or an ajax request, such as:

$.ajax({
    url: 'https://freegeoip.net/json/',
    type: 'POST',
    dataType: 'jsonp',
    success: function(location) {
        alert(location.ip);
    }
});

The error message is:

Refused to load the script 'https://freegeoip.net/json/?callback=jQuery21304975464364979416_1442806213407&_=1442806213408' because it violates the following Content Security Policy directive: "default-src 'self' blob: filesystem: chrome-extension-resource:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

Thanks,

Edit for clarity: I am looking for the IP address as it is seen from a server on the global internet network, not the local IP.

  • Why use a 3rd party service? Is your app accessing any Internet server under your control? – DanSut Sep 21 '15 at 15:10
  • possible duplicate of [Get JSON in a Chrome extension](http://stackoverflow.com/questions/11842954/get-json-in-a-chrome-extension) – Xan Sep 24 '15 at 23:05

2 Answers2

0

Well, one possible way is by using Chrome API chrome.system.network.getNetworkInterfaces(). The example is like this one.

chrome.system.network.getNetworkInterfaces(function(interfaces){
    console.log(interfaces.address);
});

And in the manifest.json file, you probably need to give the permission for the API like this.

"permissions": [
  "system.network"
]
kucing_terbang
  • 4,991
  • 2
  • 22
  • 28
  • Thanks for the idea. I tried this, but I obtained the local IP address, i.e. 192.168.1.xx. How could I get my global IP address? – Charles Blouin Sep 21 '15 at 04:17
  • hmmm, not so sure about the global IP address though.. But, do you know the URL of the address you're trying to retrieve? If yes, then, I guess it is possible to use `chrome.webRequest` to retrieve the public IP – kucing_terbang Sep 21 '15 at 04:46
  • @CharlesBlouin You should probably clarify in the Q that you want the IP address with which your app client is seen as when it accesses the Internet (after NAT, etc) - your comment here suggests this is the case and your example code infers it but your don't say that specifically. – DanSut Sep 21 '15 at 15:08
  • `'system.network' is only allowed for packaged apps, but this is a extension.` – Sudhir Kaushik Jun 08 '23 at 17:00
-1

It does require internet permissions, but the only way to do that is to call a web service that returns your external IP.

I found this service in this answer.

Community
  • 1
  • 1
Kevin Coulombe
  • 1,517
  • 1
  • 17
  • 35
  • Thanks for you help, but it does not seem to work. I have internet access (the app sends data), but the content security policy does not allow to receive information simply. – Charles Blouin Sep 24 '15 at 17:23