60

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?

wong2
  • 34,358
  • 48
  • 134
  • 179
  • I don't believe the extension's id changes once it's installed? Even when installing it "unpacked" for dev purposes, it keeps the same id unless you uninstall and re-install, no? – WesleyJohnson Jun 16 '11 at 14:05
  • When I develop it, I got id1, when I publish it, I got id2, they're different.. – wong2 Jun 16 '11 at 14:06
  • 2
    Why do you need to get the ID? – Adam Heath Jun 16 '11 at 14:16
  • 1
    @Adam Heath because I want to open chrome://{extension-id}/index.html when the button is clicked – wong2 Jun 16 '11 at 14:24
  • 2
    @AdamHeath Im surprised by your question? "Why do you need to get the ID?". There may be many purposes where you need to get the chrome extension id, and yes it is different while in local development and while publishing it. I mean no offence, you can use without id also, but it is good to know. – Sanjay Sep 02 '14 at 02:05
  • @Yegya wow this is an old question! I was asking as if they wanted to link to a page within the extension a relative path would likely work without need for the ID. So there may have been a better solution than getting the ID. – Adam Heath Sep 03 '14 at 05:33
  • I thought to use it for security: `chrome.runtime.onMessage.addListener( (request, sender, sendResponse) => { if ( sender.id == chrome.runtime.id ) {//good}else{//bad}});` ... in the background.js – Pimp Trizkit Sep 30 '17 at 18:02
  • Run `location.href` from the background script of your extension. – Shayan Jan 12 '20 at 18:41

3 Answers3

92

You can get it like this (no extra permissions required) in two different ways:

  1. Using runtime api: var myid = chrome.runtime.id;

  2. Using i18n api: var myid = chrome.i18n.getMessage("@@extension_id");

but you don't need it for opening pages, as chrome.tabs.create() (and some others) understand relative paths.

So to open index.html from your extension folder you should just use:

chrome.tabs.create({url: "index.html"});
Ivan Fraixedes
  • 550
  • 3
  • 12
serg
  • 109,619
  • 77
  • 317
  • 330
15

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
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
9

If you're doing stuff with localization, it looks like the extension mechanics offer some placeholders for accessing your extension ID:

If you're just trying to access URLs for local files to your extension, you can just use chrome.extension.getURL("some file name");

If you have another reason for actually needing to know the id of the extension, I'm not sure there is a straight forward way of getting it from within the extension itself. The two ways that come to me off the top of my head are using chrome.extension.getURL("some file name") and then parsing out the extension id from that returned URL - or using chrome.management.getAll() and looping through all the installed extensions until you find yours using a match on name and then accessing the id:

Sudarshan
  • 18,140
  • 7
  • 53
  • 61
WesleyJohnson
  • 1,538
  • 1
  • 16
  • 30