How to get my extension's id from JavaScript?

后端 未结 3 1454
滥情空心
滥情空心 2020-12-23 09:32

I\'m writing a Chrome extension, I need to get my extension\'s id in my code, so I don\'t need to change it manually every time. How can I do this?

3条回答
  •  伪装坚强ぢ
    2020-12-23 09:55

    In WebExtensions, you have two options:

    1. chrome.runtime.id
    2. chrome.i18n.getMessage("@@extension_id")

    In Chrome and Opera, they will return the same value, but in Firefox there is a difference.

    In Firefox, chrome.runtime.id will return the so called "Extension ID", but chrome.i18n.getMessage("@@extension_id") will return the "Internal UUID". The extension ID is the same for all users, but the internal UUID is created when the extension is installed and is unique per user.

    Depending on the context, the extension ID will not be what you want. For example, Firefox uses the internal UUID to fill the origin header, not the extension ID.

    Example 1: Ghostery in Firefox 61

    chrome.runtime.id                        --> "firefox@ghostery.com"
    chrome.i18n.getMessage("@@extension_id") --> "e3225586-81a0-47c3-8612-d95fb0c2a609"
    

    For fetch requests from within the extension, Firefox will add the header

    origin: moz-extension://e3225586-81a0-47c3-8612-d95fb0c2a609
    

    Example 2: Ghostery in Chrome

    chrome.runtime.id                        --> "mlomiejdfkolichcflejclcbmpeaniij"
    chrome.i18n.getMessage("@@extension_id") --> "mlomiejdfkolichcflejclcbmpeaniij" 
    

    For fetch requests from within the extension, Chrome will add the header

    origin: chrome-extension://mlomiejdfkolichcflejclcbmpeaniij
    

提交回复
热议问题