Check if Chrome extension installed in unpacked mode

前端 未结 3 1318
独厮守ぢ
独厮守ぢ 2020-12-08 10:23

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?

相关标签:
3条回答
  • 2020-12-08 10:51

    Here is a code sample how to do this:

    function isDevMode() {
        return !('update_url' in chrome.runtime.getManifest());
    }
    
    0 讨论(0)
  • 2020-12-08 10:59

    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');
    
    0 讨论(0)
  • 2020-12-08 11:09

    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.

    0 讨论(0)
提交回复
热议问题