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