2

First a little background

I was tasked with creating a 'program' that would take screenshots of web pages at given times throughout the day and save the images to a folder accessible to the user. My original train of thought was to create an extension to do the job, but I soon realized that extensions don't have access to the file system, so I turned to chrome apps which can use the fileSystem API. However, chrome apps don't have access to the functions required to take a screenshot of the current page, thus I ended up creating both: the extension takes the screenshot and sends a blob of it to the app which saves it to the file system. The process is a bit convoluted but works like a charm.

Now, the app/extension communication occurs via chrome's Message Passing API. In order for communication to take place, I need to know the id of the extension or app beforehand. I've hard-coded the ids until now, but given that those ids will will change every time the extension or app is installed, I need a better approach.

Now The Question

Thus the question is: what is the recommended way to obtain the id of a chrome app from within a chrome extension and vice versa? The plan right now is to use the chrome.management API and do the following:

In Extension

var APP_NAME = "Name of My App";
var _appId;
...
function initializeAppId() {
  // must declare the "management" permission in the manifest
  chrome.management.getAll(function(result) {
    for (var i=0; i<result.length; i++) {
      if (result[i].name == APP_NAME) {
        _appId = result[i].id;
      }
    }
  });
}

Is this the way to go about it? I would still need to hard code the name of the app, but that's not nearly as tragic as hard coding its id. Also, packaged apps don't have access to the management api, so if I went this route, I would have to do it from the extension only. Once I get the app id, I can then send a message to the app and provide the extension id which can be easily obtained from within the extension code.

How does that sound? Any suggestions?

Thanks

CJ Alpha
  • 657
  • 1
  • 6
  • 18
  • possible duplicate of [Obtaining Chrome Extension ID for development](http://stackoverflow.com/questions/23873623/obtaining-chrome-extension-id-for-development) – Xan Nov 07 '14 at 12:20
  • 1
    In short: if you generate a key you'll be using for the app (and save it, since you'll need it when publishing), you can "pin" the ID in the manifest. – Xan Nov 07 '14 at 12:21
  • So the extension/app ids will be hard coded? I was actively avoiding that. – CJ Alpha Nov 07 '14 at 12:55
  • 1
    Why would you avoid it? If/when you publish, the ID will no longer change. For security reasons it's _better_ to pin it. It only changes for unpacked development versions, and only if you don't pin it like explained above. – Xan Nov 07 '14 at 12:56
  • The alternative, using `management` API, is a very heavy hammer. – Xan Nov 07 '14 at 12:59
  • The extension/app were developed for in-house use and at the time we're not planning to publish them. I searched and searched for "app id from extension" and nothing came up suggesting how to obtain the id of one from the other. Since I wasn't planning to publish it, and I saw it changing all the time, I wanted to find a way to identify them using their name or some other method and that's why I asked the question here. – CJ Alpha Nov 07 '14 at 13:03
  • See [this answer](http://stackoverflow.com/a/26058672/934239). You can stop the ID from changing and hard-code it, which in my opinion is better. The alternative is your code in the question, yes. – Xan Nov 07 '14 at 13:06
  • Perfect, thanks. I'm not sure if this should be considered a duplicate since I was actually looking for a way to not hard code the id as opposed making it permanent as the other questions suggest. If you think it's a duplicate, you may delete it (if that's possible). Otherwise, add your comment as an answer and I'll accept it. Thanks again. – CJ Alpha Nov 07 '14 at 13:16

1 Answers1

2

Your approach is correct in the sense that it will work.

But chrome.management is a big hammer not particularly suited for the task.

You said that the IDs change. This only happens with unpacked versions, because they are not signed yet with a cryptographic key; Chrome has to assign something as an ID and it does it by hashing the path to the extension/app.

The solution is to either pack/sign the app once to obtain a suitable public key inside the CRX, or do the cryptography yourself as described here. When you specify the "key" property in the manifest, Chrome uses it to derive the ID and it stops changing.

After that, you can hard-code the ID.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206