简单的存储不会在会话之间持续数据 [英] Simple storage not persisting data between sessions

查看:177
本文介绍了简单的存储不会在会话之间持续数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的扩展中使用简单存储,但是我无法在浏览器会话之间检索值。这里是这样的:从我的主代码中,我创建了一个这样的值:
$ b $ pre $ var css = require(sdk / simple-存储);
ss.storage.foo = [{id:bar1,properties:{a:aaa,b:bbb}}]
console.log(ss.storage.foo);

没关系,我通过日志查看对象。但是之后我关闭了浏览器,评论了foo definition(第2行),并且控制台日志是 undefined



我知道默认情况下运行cfx会在每次运行时使用一个新的配置文件,所以简单的存储不会从一次运行到下一次运行。但我正在使用

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

所以我确定我每次都使用相同的配置文件。



会发生什么情况?我错过了什么?任何想法是欢迎的!提前感谢!
$ b


感谢Notidart的回答,我可以发现问题是当您关闭Firefox中的文件时正确的方法。当你通过控制台杀死它时,它不会持久化数据。



解决方案

存储工作。它会在 ProfD 文件夹中创建一个文件夹,它是您的个人资料目录: 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);
返回storeFile.path;

文件的确切位置位于您的个人资料文件夹中,名为然后是名为 addon id 的文件夹,然后是一个名为 simple-storage 的文件夹,然后在名为 store.json 。示例路径:

$ p $ code $ Prof $ jetpack / addon-id / simple-storage / store.json

然后将数据写入该文件。每当您的配置文件文件夹被重新创建(由于temp配置文件的性质,由于jpm / cfx),您的数据将被删除。



您应该使用 OS.File 来创建自己的文件来保存数据。 OS.File 是更好的方法,然后是 nsIFile ,这就是简单存储所要做的。保存在 ProfD 文件夹之外,所以请确保在卸载您的插件时将其删除,否则会污染您的用户计算机。


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.

解决方案

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

这篇关于简单的存储不会在会话之间持续数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆