本文介绍了检测Chrome扩展程序首次运行/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can an extension find out that it is being run for the first time or has just been updated, so that the extension can perform some specific actions? (e.g. open a help page or update settings)

推荐答案

在较新版本的Chrome中(自Chrome 22以来),可以使用 chrome.runtime.onInstalled 事件,这是更清洁。

In newer versions of Chrome (since Chrome 22), you can use the chrome.runtime.onInstalled event, which is much cleaner.

示例:

Example:

// 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 + "!");
    }
});

这篇关于检测Chrome扩展程序首次运行/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!