114

扩展如何发现它是第一次运行还是刚刚更新,以便扩展可以执行一些特定的操作?(例如打开帮助页面或更新设置)

4

4 回答 4

192

在较新版本的 Chrome(自 Chrome 22 起)中,您可以使用chrome.runtime.onInstalled更简洁的事件。

例子:

// 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 + "!");
    }
});
于 2013-02-19T12:37:02.480 回答
83

更新答案以反映清单的 v3:

Chromium 现在有一组chrome.runtime API,允许您获取扩展的版本。

要获取当前版本:

chrome.runtime.getManifest().version

要在第一次安装扩展、扩展更新到新版本以及 Chromium 更新到新版本时进行监听,您可以使用该onInstalled事件。

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;
  }
于 2010-03-08T14:03:57.120 回答
20

Fortunately, there are now events for this (since Chrome version 22, and 25 for update events).

For an installed event:

chrome.runtime.onInstalled.addListener(function() {...});

For an OnUpdateAvailable event:

chrome.runtime.onUpdateAvailable.addListener(function() {...});

An important excerpt about OnUpdateAvailable from the developer docs says:

Fired when an update is available, but isn't installed immediately because the app is currently running. If you do nothing, the update will be installed the next time the background page gets unloaded, if you want it to be installed sooner you can explicitly call chrome.runtime.reload().

于 2014-08-17T21:19:44.013 回答
9

简单的。扩展程序首次运行时,localStorage为空。在第一次运行时,您可以在此处编写一个标志以将所有后续运行标记为非第一次运行。

例如,在 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!');

编辑:要检查扩展是否刚刚更新,请在第一次运行时存储版本而不是简单标志,然后当当前扩展版本(通过XmlHttpRequest清单获取它)不等于存储的版本时localStorage,扩展有已更新。

于 2010-03-08T07:16:19.583 回答