Reputation: 1501
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
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.storage
Chrome 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:
userid
key, check whether the userid is valid and known.
userid
is invalid, generate one, and include it in the response.Which causes the response to look like (To generate JSON using PHP, use json_encode
):
{"userid":"some unique user identifier", ....}
Upvotes: 2