Google Chrome 扩展 - 如何访问和存储本地文件以供以后使用
我目前正在编写我的第一个 google chrome 扩展。简单地说,我想在扩展弹出窗口中添加一个文件浏览器,浏览图像并将该图像插入当前打开的选项卡中。
此外,我想将选定的文件存储在扩展中以供以后重用。
第一种方法是使用 FileAPI 加载文件内容并将以 base64 编码的文件内容存储在 localStorage 中。不幸的是,由于本地存储的空间限制,在一些图像之后就停止工作了。
更好的方法是获取所选文件的绝对文件名,并在每次需要时重新读取该文件 - 不幸的是,我无法获取包括路径的完整文件,只是获取了不带路径的文件名。
另一种方法可能是 WebSQL,但这似乎很复杂 - 有人可以确认我们是否也有配额吗?
有人事先做过类似的事情吗?我很高兴得到任何帮助。
I'm currently writing my first google chrome extension. Simplified, I want to add a file browser in the extensions popup, browser an image and insert that image in the currently opened tab.
Additionally I want to store the selected file in the extension for later reuse.
First approach was an , loading of file content with FileAPI and storing the file content base64 encoded in localStorage. Unfortunately that stopped working after a few images due to space limitation of localStorage.
Better approach would be to get the absolute filename of the chosen file and reread the file everytime necessary - unfortunately I was unable to get the complete file including path, just got the filename without path.
Another approach could be WebSQL, but this seems to be complicated - can anybody confirm if or if not we have a quota there as well?
Does anybody made anything like that beforehand? I would be glad to get any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您需要对缓存文件执行的操作,您可以简单地使用 FileIO API 将二进制文件保存到扩展的沙箱环境中。仅将文件路径存储到原始文件可能行不通,因为 API 只能在其沙箱中读取 [除非您自己通过输入选择文件]。
如有必要,您可以添加“unlimitedStorage”权限(更多信息请参见开发者文档 )到您的 manifest.json 以覆盖存储限制。
这仅适用于文件 I/O(临时/持久)和 Web SQL(仅临时)。由于它是同步实现的,localStorage 的最大长度约为 2700000 个字符(因为它存储为 UTF-16),设置此权限不会增加该字符数。
使用 File API 和 Chrome 13+,您可以请求一定数量的配额(请参阅 这里和此处查看完整示例)。此功能已被引入以适用于 Web 应用程序,并且无需设置 unlimitedStorage 权限即可使用。 (但是,如果设置了权限,则当前不会要求用户允许存储请求)
在我的实验中,这似乎是持久存储大量数据的唯一方法。
Depending on what you need to do with the cached files, you could simply use the FileIO API to save the binary to your extension's sandbox environment. Just storing the filepath to the original file probably won't work as the API can only read within it's sandbox [unless your choosing the files yourself through an input].
If necessary, you can add the "unlimitedStorage" Permission (more at the developer documentation )to your manifest.json in order to override the storage limitations.
This will only apply to the File I/O (temporary/persistent) and Web SQL (temporary only). Due to it's synchronous implementation, localStorage has a maximum of ~ 2700000 characters (as it is stored as UTF-16), which won't be increased by setting this permission.
With the File API and Chrome 13+, you can request a certain amount of quota (see here and here for full example). This has been introduced to work for Web apps and works without setting the unlimitedStorage permission. (However, if the permission is set, the user is currently not asked to allow the storage request)
In my experiments, this seems to be the only way to persistently store large amounts of data.