Chrome扩展程序如何将许多文件保存到用户指定的目录中? [英] How can a Chrome extension save many files to a user-specified directory?

查看:201
本文介绍了Chrome扩展程序如何将许多文件保存到用户指定的目录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Chrome扩展程序以用作内部工具。它的所需行为是:


  1. 作为页面操作,在查看特定Intranet页面时启用地址栏图标。 $ b
  2. 当用户点击图标时,识别页面上某种媒体类型(比如.jpg)的所有文件,并且
  3. 默默地将它们全部保存到一个目录中用户的本地驱动器。

此问题已在之前问过,但答案是使用NPAPI NPAPI现在已被废弃

那么,目前可用的方法是什么做到这一点?我看过的是:


  • chrome.FileSystem API ---但这不会将文件保存在任何用户可访问的位置。相反,存储的文件隐藏在混淆名称后面一个无证的目录。用户要求文件以原始名称存储在可访问的目录中。

  • HTML5下载属性,方法是创建一个data:URL并以编程方式点击它。这会弹出每个文件的另存为...对话框,当单个页面上有一百个资源时,这是不可接受的。用户要求文件在没有进一步交互的情况下进行下载,除了单击图标之外。

  • Chrome下载API ,但仅适用于测试版和开发渠道。用户需要使用主流Chrome浏览器的扩展程序。

  • 使用 Native Messaging API 通过创建一个简单的将一个文件保存到磁盘的小型.exe文件,然后将.jpg作为blob传递给它。这似乎非常麻烦,我甚至不知道如何可靠地将大块blob传递给EXE这样的。



是否有另一种方法可以试试?

解决方案

你做了很多研究。事实上,没有任何插件或扩展,常规网页无法写入用户的文件系统。另外, HTML5 Filesystem API 仅提供对虚拟文件系统的访问已经观察到。



然而,你很迷惑 chrome.fileSystem API与HTML5 FileSystem API。与HTML FileSystem API不同,Chrome的 fileSystem (app)API可以直接将写入用户的文件系统(例如〜/ Documents 或%USERPROFILE%\Documents ),由用户指定。



API仅适用于Chrome 应用,不适用于扩展程序。这不是问题,尤其是在您开发内部工具时,因为您可以安装应用程序和扩展程序,并使用消息传递以在扩展(页面操作)和应用程序(文件系统访问)之间进行通信( example )。






关于 chrome.downloads :由于您的扩展名是内部的,您可能会强制用户登录beta / dev频道以使用此API。这个API的唯一限制是这些文件将被保存在用户定义的Downloads文件夹的子目录中。



编辑: chrome.downloads 现在所有渠道都可以使用API​​,包括稳定分支(自Chrome 31以来)。


I'm working on a Chrome extension to be used as an internal tool. Its required behavior is:

  1. As a page action, enable an address bar icon when looking at certain intranet pages.
  2. when the user clicks the icon, identify all files of a certain media type (say, .jpg) on the page, and
  3. silently save them all to a directory on the user's local drive.

This question has been asked before, but the answer then was "use NPAPI", and NPAPI is now derelict.

So, what is the currently available way to achieve this? The ones I've looked at are:

  • The chrome.FileSystem API --- but this does not save files in any user-accessible location. Instead the stored files are hidden behind obfuscated names in an undocumented directory. User requires that the files be stored under their original names in an accessible directory.
  • The HTML5 download attribute, by creating a data: URL and programmatically clicking it. This pops up a "save as..." dialog for each file, which is unacceptable when there are a hundred assets on a single page. User requires that the files be downloaded without further interaction beyond the single icon click.
  • The Chrome Download API, but that is only available in the beta and dev channels. User requires this extension work with mainstream Chrome.
  • Use the Native Messaging API by creating a small .exe that simply saves a file to disk, and then pass the .jpg as a blob to it. This seems very cumbersome and I am not even sure how to reliably pass large blobs to EXEs like that.

Is there another approach I can try?

解决方案

You've done quite a lot of research. Indeed, regular web pages cannot write to the user's filesystem without any plugins or extensions. Also, the HTML5 Filesystem API only provides access to a virtual filesystem, as you've observed.

However, you are confusing the chrome.fileSystem API with the HTML5 FileSystem API. Unlike the HTML FileSystem API, Chrome's fileSystem (app) API can directly write to the user's filesystem (e.g. ~/Documents or %USERPROFILE%\Documents), specified by the user.

This API is only available to Chrome apps, not extensions. This is not a problem, especially since you're developing an internal tool, because you can install the app and extension, and use message passing to communicate between the extension (page action) and app (file system access) (example).


About chrome.downloads: Since your extension is internal, you can probably force users to get on the beta/dev channel in order to use this API. The only limitation of this API is that the files will be saved in (a subdirectory of) the user-defined Downloads folder.

EDIT: The chrome.downloads API is now avaiable in all channels, including the stable branch (since Chrome 31).

这篇关于Chrome扩展程序如何将许多文件保存到用户指定的目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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