5

我正在尝试编写一个 chrome 扩展,从配置文件中初始化一些参数。我想允许扩展更改这些参数并将它们保存到文件中,以便下次加载扩展时使用新配置。

我一直在阅读 chrome.filesystem api,但它需要用户的交互来选择文件。但是在这种情况下,该过程必须自动完成,无需用户进行任何操作。

由于此配置文件只能由扩展程序访问,因此它可以被沙盒化,但即使 chrome 关闭,它也必须是持久的。

我设法使用 XMLHttpRequest 读取文件,但找不到修改文件的方法。

是否可以通过 chrome 扩展来做到这一点?

4

2 回答 2

9

This is an old question but unfortunately the only response it got was wrong.

It's definitely possible to read and write files using HTML5 in Chrome, without the vague "security" issues mentioned. The HTML5 Filesystem creates a protected sandbox in which you write and read virtual files: you can think of it as files being written in file based database managed by Chrome and not accessible by either other Chrome apps & extensions or other OS based applications. The user won't be able to copy or move these files using his OS file explorer since they reside inside the web browser's file DB.

You can't read (or write) arbitrary files from (to) disk based on any given file path. If you need a file from disk you can only let the user select it himself by using chrome.fileSystem.chooseEntry()

You can however read (and write) your own files from (to) the HTML5 Filesystem.

So to answer your question: no you don't need user interaction to write your config file to the browser's file system. An alternative to files could be chrome storage, localstorage or even indexedDB to store your (persisted) config key-value pairs.

Here are a couple of useful links to start reading about it:

Toying with the HTML5 filesystem

HTML5 Rocks

HTML5 demos

于 2014-01-28T19:40:27.243 回答
1

我想在用户不知道的情况下允许 chrome 扩展写入配置文件可能是一个安全问题。您可能遇到了安全功能。一个潜在的解决方法是构建一个始终打开的桌面应用程序,并且您的 chrome 应用程序与之通信。哎呀,您可能可以使用自动热键之类的东西来做您需要做的事情(不知道所有细节)。

于 2013-01-13T13:57:14.730 回答