Simple storage not persisting data between sessions

▼魔方 西西 提交于 2019-12-11 08:58:06

问题


I'm trying to use the simplestorage from my extension, but I can't retrieve values between browser sessions. Here's the thing: From my main code, I created a value this way:

var ss = require("sdk/simple-storage");
ss.storage.foo = [{id:"bar1", properties:{a:"aaa", b:"bbb"}}]
console.log(ss.storage.foo);

This is ok, I coud see the object through the log. But then I closed the browser, commented the "foo definition" (line 2) and the console log was "undefined".

I know cfx run by default uses a fresh profile each time it runs, so simple storage won't persist from one run to the next. But I'm using

cfx -b firefox run --profiledir=$HOME/.mozilla/firefox/nightly.ext-dev

So I'm sure I'm using the same profile everytime.

What could be happening? What am I missing? Any idea is welcome! Thanks in advance!

Thanks to the answer of Notidart, I could discover that the problem was the file is saved when you close Firefox in the right way. When you just kill it through console, it's not persisting data.


回答1:


This is how simple storage works. It creates a folder in your ProfD folder which is your profile directory: https://github.com/mozilla/addon-sdk/blob/master/lib/sdk/simple-storage.js#L188

let storeFile = Cc["@mozilla.org/file/directory_service;1"].
                getService(Ci.nsIProperties).
                get("ProfD", Ci.nsIFile);
storeFile.append(JETPACK_DIR_BASENAME);
storeFile.append(jpSelf.id);
storeFile.append("simple-storage");
file.mkpath(storeFile.path);
storeFile.append("store.json");
return storeFile.path;

The exact location of the file made is in a your profile folder, in a folder named jetpack then your addon id, then a folder called simple-storage, then in a file in that folder called store.json. Example path:

ProfD/jetpack/addon-id/simple-storage/store.json

It then writes data to that file. Every time your profile folder is recreated (due to the nature of temp profile, due to jpm / cfx), your data is erased.

You should just use OS.File to create your own file to save data. OS.File is better way then nsIFile which is what simple-storage does. Save it outside that ProfD folder, so but make sure to remove it on uninstall of your addon otherwise you pollute your users computers




回答2:


Just in case someone else finds this question while using jpm, note that --profiledir is removed from jpm, so to make jpm run using the same profile directory (and thereby the same simple-storage data), you have to run it with the --profile option pointing at the profile path - not the profile name.

jpm run --profile path/to/profile



回答3:


For future readers, an alternative to @Noitidart's recommendation of using OS.File, is to use the Low-Level API io/file

You can create a file using fileIO.open(path). If the file doesn't exist, it will be created. You can read and write by including the second argument fileIO.open(path, mode).

The mode can be:

  • r - Read-only mode
  • w - Write-only Mode
  • b - Binary mode

It defaults to r. You can use this to read and write to a file (obviously the file cannot be in the ProfD folder or it will get removed each time jpm / cfx is run)



来源:https://stackoverflow.com/questions/31061310/simple-storage-not-persisting-data-between-sessions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!