How do I detect a first-run in Firefox a addon?

2020-07-10 05:56发布

I would like to know the simplest way to detect a first-run in a Firefox addon. I prefer not to use the (SQLite) Storage API as this seems way overkill for this simple usecase.

I guess my question could also be: what is the simplest way to store a flag?

2条回答
老娘就宠你
2楼-- · 2020-07-10 06:21

Maybe a better solution would be:

/**
* Check if this is the first run of the addon
*/
function checkFirstRun(){
    if(ss.storage.firstRun == undefined){
        ss.storage.firstRun = false;
        return true;
    }
    else{
        return false;
    }
}
查看更多
ゆ 、 Hurt°
3楼-- · 2020-07-10 06:38

There you go: http://mike.kaply.com/2011/02/02/running-add-on-code-at-first-run-and-upgrade/

var firstrun = Services.prefs.getBoolPref("extensions.YOUREXT.firstrun");

var curVersion = "0.0.0";

if (firstrun) {
  Services.prefs.setBoolPref("extensions.YOUREXT.firstrun", false);
  Services.prefs.setCharPref("extensions.YOUREXT.installedVersion", curVersion);
  /* Code related to firstrun */
} else {
  try {
    var installedVersion = Services.prefs.getCharPref("extensions.YOUREXT.installedVersion");
    if (curVersion > installedVersion) {
      Services.prefs.setCharPref("extensions.YOUREXT.installedVersion", curVersion);
      /* Code related to upgrade */
    }
  } catch (ex) {
    /* Code related to a reinstall */
  }
}
查看更多
登录 后发表回答