Firefox扩展开发 - 数据存储 - 是Firefox首选项好地方? [英] Firefox Extension Development - Data Storage - is Firefox Preferences a good place?

查看:228
本文介绍了Firefox扩展开发 - 数据存储 - 是Firefox首选项好地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Firefox附加组件,需要在本地计算机中保存一些用户数据。现在,我使用Firefox提供的文件系统功能在用户系统中创建一些文件夹和文件。

I have developed a Firefox add-on that needs to save some user-data in the local machine. Right now, I use the filesystem functionality that Firefox provides to create some folders and files in the user's system.

可移植性是一个问题。我为Windows XP设计。但是,Vista的文件夹结构是不同的(内部的应用程序数据)。 Linux是完全不同的。我做一个操作系统检查,并相应地创建/读/写文件。突然,我需要支持Mac!

Portability is a problem. I designed it for Windows XP. But, Vista's folder structure is different (inside the app data). Linux is completely different. I do an OS check and create/read/write files accordingly. Suddenly, I need to support Macs!

我想为什么不在Firefox首选项中保存数据。我将使用首选项管理器。

I thought why not save the data in the Firefox preferences. I will use the Preference Manager.

平均来说,总数据量几乎不超过100KB。但事情是,在极少数情况下,数据可能会相当大 - 在最坏的情况下几兆字节。所以,将它保存为偏好值是个好主意吗?

On an average, the total amount of data hardly goes beyond 100KB. But the thing is, in rare cases, the data could get quite big - a few megabytes in the worst case. So, is it a good idea to save it as preference values?

我知道这不是它的意思,但它会救我很多头痛。

I know that is not what it is meant for, but it will save me a lot of headaches.

即使这是一个坏主意,只是让我知道在偏好设置中存储大量数据的性能或其他问题(如果有)。

Even if it is a bad idea, just let me know about the performance or other issues (if any) of storing that much data in preferences.

感谢。

推荐答案

对于较新版本的Firefox,您可以使用 SQLite存储数据。 JavaScript中的 SQLite API不是很好,但是从Firefox 3.6开始,它变得非常稳定(异步提取数据,绑定多组数据等)。

With newer versions of Firefox you can use SQLite to store data. The SQLite API in JavaScript isn't terribly good, but from Firefox 3.6 it's starting to get pretty solid (asynch fetching of data, binding multiple sets of data, etc).

您可以使用以下代码在配置文件目录中打开/创建一个文件。它适用于所有平台上Firefox运行:

You can open/create a file in the profile directory using the following code. It works on all platforms Firefox runs on:

var file = Components.classes["@mozilla.org/file/directory_service;1"]
                     .getService(Components.interfaces.nsIProperties)
                     .get("ProfD", Components.interfaces.nsIFile);
file.append("my_db_file_name.sqlite");

这篇关于Firefox扩展开发 - 数据存储 - 是Firefox首选项好地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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