Hi,
How can I find out if my extension is being run for the first time / has just been upgraded?
Thanks!
Hi,
How can I find out if my extension is being run for the first time / has just been upgraded?
Thanks!
Simple. When the extension first runs, the localStorage
is empty. On first run, you can write a flag there to mark all consequent runs as non-first.
Example, in background.htm:
var first_run = false;
if (!localStorage['ran_before']) {
first_run = true;
localStorage['ran_before'] = '1';
}
if (first_run) alert('This is the first run!');
EDIT: To check whether the extension has just been updated, store the version instead of a simple flag on first run, then when the current extension version (get it by XmlHttpRequest
ing the manifest) doesn't equal the one stored in localStorage
, the extension has been updated.
If you want to check if the extension has been installed or updated, you can do something like this:
function onInstall() {
console.log("Extension Installed");
}
function onUpdate() {
console.log("Extension Updated");
}
function getVersion() {
var version = 'NaN';
var xhr = new XMLHttpRequest();
xhr.open('GET', chrome.extension.getURL('manifest.json'), false);
xhr.send(null);
var manifest = JSON.parse(xhr.responseText);
return manifest.version;
}
// Check if the version has changed.
var currVersion = getVersion();
var prevVersion = localStorage['version']
if (currVersion != prevVersion) {
// Check if we just installed this extension.
if (typeof prevVersion == 'undefined') {
onInstall();
} else {
onUpdate();
}
localStorage['version'] = currVersion;
}