Safari扩展程序首先运行并更新

时间:2012-03-26 08:41:46

标签: safari installation

如何在用户安装扩展程序后第一次知道我的Safari扩展程序代码是否正在运行?

我想区分扩展程序的新安装与扩展程序的更新。

我正在寻找与this answer非常相似的内容,但对于Safari,而不是Chrome。我无法从该链接中的答案“翻译”代码到Safari。

3 个答案:

答案 0 :(得分:5)

如果您可以在没有更新检查的情况下生活,则此脚本应该有效(与Chrome相关的答案进行比较):

// In background page
function onInstall() {
  console.log('Extension installed');
}

var firstRun = typeof localStorage['extensionHasPreviouslyRun'] === 'undefined' ||
    !JSON.parse(localStorage['extensionHasPreviouslyRun']);

if (firstrun) {
  onInstall();
  localStorage['extensionHasPreviouslyRun'] = JSON.stringify(true);
}

如果您还要检查更新,则需要异步获取plist文件中的版本,如下所示:

// In background page
function onInstall() {
  console.log('Extension installed');
}

function onUpdate() {
  console.log('Extension update');
}

function requestVersion(callback) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open('GET', 'info.plist');
  xmlhttp.onload = function () {
    var infoFile = xmlhttp.responseXML;
    var keys = infoFile.getElementsByTagName('key');
    for (var i = 0; i < keys.length; i++){
      if (keys[i].firstChild.data === 'CFBundleShortVersionString'){
        var version = keys[i].nextElementSibling.firstChild.data;
        callback(version);
        break;
      }
    }
  }
  xmlhttp.send();
}

requestVersion(function(version) {
  var storedVersion = localStorage['version'];
  if (storedVersion !== version) {
    // Check if we just installed this extension.
    if (typeof storedVersion === 'undefined') {
      onInstall();
    } else {
      onUpdate();
    }
    localStorage['version'] = version;
  }
});

答案 1 :(得分:1)

我们可以从safari.extension.displayVersion

获取版本
var storedVersion = safari.extension.settings.version;
var currentVersion = safari.extension.displayVersion + '.' + safari.extension.bundleVersion;
if (typeof storedVersion === 'undefined') {
    console.log('Extension installed');
    safari.extension.settings.version = currentVersion
} else if (currentVersion != storedVersion) {
    console.log('Extension update');
    safari.extension.settings.version = currentVersion
}

不要忘记在Extension Builder中添加隐藏的设置项

答案 2 :(得分:0)

将@ Claudijo上面的答案拿到了一个小班上:

/**
 * ExtensionState
 * 
 * @abstract
 */
var ExtensionState = (function() {

    /**
     * __configFilePath
     * 
     * @access  private
     * @return  String (default: '../Info.plist')
     */
    var __configFilePath = '../Info.plist';

    /**
     * __getConfigVersion
     * 
     * @access  private
     * @param   Function callback
     * @return  void
     */
    var __getConfigVersion = function(callback) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('GET', __configFilePath);
        xmlhttp.onload = function () {
            var infoFile = xmlhttp.responseXML,
                keys = infoFile.getElementsByTagName('key');
            for (var i = 0; i < keys.length; i++){
                if (keys[i].firstChild.data === 'CFBundleShortVersionString') {
                    var version = keys[i].nextElementSibling.firstChild.data;
                    callback(version);
                    break;
                }
            }
        };
        xmlhttp.send();
    };

    /**
     * __getLocalVersion
     * 
     * @access  private
     * @return  String
     */
    var __getLocalVersion = function() {
        return localStorage['version'];
    };

    /**
     * __putLocalVersion
     * 
     * @access  private
     * @param   String version
     * @return  void
     */
    var __putLocalVersion = function(version) {
        localStorage['version'] = version;
    };

    // Public
    return {

        /**
         * installed
         * 
         * @access  public
         * @param   Function callback
         * @return  void
         */
        installed: function(callback) {
            var localVersion = __getLocalVersion();
            if (typeof localVersion === 'undefined') {
                __getConfigVersion(function(version) {
                    callback(version);
                    __putLocalVersion(version);
                });
            }
        },

        /**
         * updated
         * 
         * @access  public
         * @param   Function callback
         * @return  void
         */
        updated: function(callback) {
            var localVersion = __getLocalVersion();
            if (typeof localVersion !== 'undefined') {
                __getConfigVersion(function(version) {
                    if (localVersion !== version) {
                        callback(version);
                        __putLocalVersion(version);
                    }
                });
            }
        }
    };
})()
ExtensionState.installed(function(version) {
    console.log('(global.html): Installed');
});
ExtensionState.updated(function(version) {
    console.log('(global.html): Updated');
});