rac*_*ck1 91 google-chrome google-chrome-extension
扩展如何发现它是第一次运行或刚刚更新,以便扩展可以执行某些特定操作?(例如,打开帮助页面或更新设置)
Alv*_*ong 166
在较新版本的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 + "!");
    }
});
Moh*_*our 68
如果要检查扩展是否已安装或更新,您可以执行以下操作:
chrome.runtime.getManifest().version
JVi*_*lla 19
幸运的是,现在有这样的事件(因为Chrome版本22,更新事件25).
对于已安装的活动:
chrome.runtime.onInstalled.addListener(function() {...});
对于OnUpdateAvailable事件:
chrome.runtime.onUpdateAvailable.addListener(function() {...});
开发人员文档中关于OnUpdateAvailable的重要摘录说:
更新可用时触发,但由于应用程序当前正在运行,因此未立即安装.如果您不执行任何操作,则下次卸载后台页面时将安装更新,如果您希望尽快安装它,则可以更快地安装chrome.runtime.reload().
简单.当扩展程序首次运行时,它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的扩展名时,扩展名为已更新.
| 归档时间: | 
 | 
| 查看次数: | 31340 次 | 
| 最近记录: |