user838437
user838437

Reputation: 1501

Dynamic manifest values for chrome extension

I have a chrome extension I've developed and I want to have some sort of an ID for each user.

Ideally I would place this ID in the manifest, and call it via chrome.app.getDetails().userid

Then I can use this id to get a feed every X hours and the feed is different for each userid.

If I serve the CRX file myself, I can do this by simply writing a quick PHP script that changes the manifest upon download request and inserts the id using a certain logic while also inserting the same id to a mysql table.

How can I do this while serving the extension through the Chrome Web Store?

Upvotes: 1

Views: 1828

Answers (1)

Rob W
Rob W

Reputation: 348992

Do not include user-specific values in the manifest file. A better approach is by using the extension's persistence.

One logical place to define such an id is the options page.
Another possible option is a background script, which follows the following logic:

    var userid = localStorage.getItem('userid');
    if (!userid) {
        // Create new useruid, either via a server-side script, or client-side
        // ...
        localStorage.setItem('userid', 'new user id');
    }

In this example, I used localStorage to enable persistence. You can also use the chrome.storageChrome 20+ instead, which allows users to synchronise their profiles and settings.

Instead of checking and retrieving the id at the first run, you can also implement the logic at the server's side. For instance, let's assume that the server's response format is JSON. Then, simply define the uid in the response (optionally, only if it's changed):

var userid = localStorage.getItem('userid');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/feeds/json?userid=' + userid);
xhr.onload = function() {
    var response = JSON.parse(xhr.responseText);
    if (response.userid) { // Set new uid if needed
        localStorage.setItem('userid', userid = response.userid);
    }
    // ... your application's logic ...
};
xhr.send();

The server has to implement the following:

  1. If the request (query string?) contains a userid key, check whether the userid is valid and known.
    • If the userid is invalid, generate one, and include it in the response.
  2. Your application's logic here

Which causes the response to look like (To generate JSON using PHP, use json_encode):

{"userid":"some unique user identifier", ....}

Upvotes: 2

Related Questions