var first_run = false;
if (!localStorage['ran_before']) {
first_run = true;
localStorage['ran_before'] = '1';
}
if (first_run) alert('This is the first run!');
chrome.runtime.onInstalled.addListener((details) => {
const currentVersion = chrome.runtime.getManifest().version
const previousVersion = details.previousVersion
const reason = details.reason
console.log(`Previous Version: ${previousVersion }`)
console.log(`Current Version: ${currentVersion }`)
switch (reason) {
case 'install':
console.log('New User installed the extension.')
break;
case 'update':
console.log('User has updated their extension.')
break;
case 'chrome_update':
case 'shared_module_update':
default:
console.log('Other install events within the browser')
break;
}
})
仅此而已!
旧答案,在2011年之前
如果你想检查扩展是否已经安装或更新,你可以这样做:
function onInstall() {
console.log("Extension Installed");
}
function onUpdate() {
console.log("Extension Updated");
}
function getVersion() {
var details = chrome.app.getDetails();
return details.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;
}
// Check whether new version is installed
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install"){
console.log("This is a first install!");
}else if(details.reason == "update"){
var thisVersion = chrome.runtime.getManifest().version;
console.log("Updated from " + details.previousVersion + " to " + thisVersion + "!");
}
});