Alexander Gromnitsky
Alexander Gromnitsky

Reputation: 3029

Check if Chrome extension installed in unpacked mode

Is there a way to detect whether I'm running an extension that was installed from my .crx file or the extension was loaded as via 'Load unpacked extension...' button?

I'm aware about ID differences in that case, but I don't want to rely on hardcoded strings in the code.

Upvotes: 33

Views: 7024

Answers (3)

Dmitry Kaigorodov
Dmitry Kaigorodov

Reputation: 1533

Here is a code sample how to do this:

function isDevMode() {
    return !('update_url' in chrome.runtime.getManifest());
}

or

const isProdMode = 'update_url' in chrome.runtime.getManifest()

Upvotes: 31

Konrad Dzwinel
Konrad Dzwinel

Reputation: 37903

If by "installed from my .crx file" you mean installed from Chrome Web Store you can simply check extension manifest.json for value of update_url attribute. CWS adds it when you upload your extension.

If you have a self-hosted .crx file, get your extension information using chrome.management.getSelf() and check installType of returned ExtensionInfo object. If it says "development" that means that extension was loaded unpacked in developer mode. "normal" means that it was installed from .crx file.

Upvotes: 44

Dennis
Dennis

Reputation: 59449

An extension is running in developer mode (i.e. unpacked), when it does not contain the update_url field in its manifest.

This works because an unpacked extension's JSON manifest file should not contain the update_url field. This field is automatically added when publishing via the Chrome Developer Dashboard.

For example, debug logs that only appear during development.

const IS_DEV_MODE = !('update_url' in chrome.runtime.getManifest());

function debugLog(str) {
  if (IS_DEV_MODE) console.log(str);
}

debugLog('This only appears in developer mode');

Upvotes: 6

Related Questions