1

I develop a chrome extension which is also compatible with Firefox/Edge/Opera.

The extension rely on a REST API accessible over the web. During the web development, I prefer pointing to a development endpoint where it does not affect the production tenant and only affect a development instance and database.

My question is then quite simple, I would like to make similar to this pseudo-code:

if (extension.downloaded_from_store == true)
    endpoint = "https://api-dev.example.com"
else
    // The extension has been installed from a local directory
    endpoint = "https://api-prod.example.com"

Do you have any idea how I could do such thing (preferably from the background.js page) ?

If the solution can be compatible with all browsers, it would be perfect !

omarjmh
  • 13,632
  • 6
  • 34
  • 42
Jonathan DEKHTIAR
  • 3,456
  • 1
  • 21
  • 42

2 Answers2

3

Most google chrome extensions on the store have a permanent extension ID. If you packaged you extension so that it uses the same extension ID with each update then you can simply hardcode that ID and check it inside that first if.

I recommend reading more about packaging chrome extensions here.

Specifically this part.

Luka Čelebić
  • 1,083
  • 11
  • 21
  • It could be an idea. However, I use the Chrome Store so I hope the extension is fixed and does not change with the updates. I need then to find out how to do the same for Firefox extensions, Opera and Edge. – Jonathan DEKHTIAR Sep 11 '17 at 20:51
3

I found the answer in the chrome documentation in the management module:

Link: https://developer.chrome.com/extensions/management#type-ExtensionInstallType

ExtensionInstallType

How the extension was installed. One of:

  • admin: The extension was installed because of an administrative policy
  • development: The extension was loaded unpacked in developer mode
  • normal: The extension was installed normally via a .crx file
  • sideload: The extension was installed by other software on the machine
  • other: The extension was installed by other means Enum "admin", "development", "normal", "sideload", or "other"

This allows me to do the following:

chrome.management.get(chrome.runtime.id, function(app_info){
    if (app_info.installType == "development"){
        endpoint = "https://api-dev.example.com";
    }
    else {
        endpoint = "https://api-prod.example.com";
    }
});
Jonathan DEKHTIAR
  • 3,456
  • 1
  • 21
  • 42