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

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

问题描述

我正在开发可用作内部工具的 Chrome 扩展程序.其要求的行为是:

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

  1. 作为页面操作,在查看某些内网页面时启用地址栏图标.
  2. 当用户点击该图标时,识别页面上特定媒体类型(例如 .jpg)的所有文件,以及
  3. 悄悄地将它们全部保存到用户本地驱动器上的目录中.

之前有人问过这个问题,但答案是使用 NPAPI",和 NPAPI 现已废弃.

那么,目前可用的方法是什么?我看过的有:

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

  • chrome.FileSystem API --- 但这不会将文件保存在任何用户可访问的位置.相反,存储的文件隐藏在混淆名称后面一个未记录的目录.用户要求文件以其原始名称存储在可访问的目录中.
  • HTML5 下载属性,由创建一个 data: URL 并以编程方式单击它.这会为每个文件弹出一个另存为..."对话框,当单个页面上有一百个资产时,这是不可接受的.用户要求下载文件时,除了单击单个图标外,无需进一步交互.
  • Chrome 下载 API,但仅在测试版和开发版渠道中可用.用户需要此扩展程序与主流 Chrome 一起使用.
  • 通过创建一个小的 .exe 来使用 Native Messaging API只需将文件保存到磁盘,然后将 .jpg 作为 blob 传递给它.这看起来很麻烦,我什至不确定如何可靠地将大 blob 传递给这样的 EXE.
  • 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?

推荐答案

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

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.

但是,您混淆了 chrome.fileSystem带有 HTML5 文件系统 API 的 API.与 HTML 文件系统 API 不同,Chrome 的 fileSystem(应用程序)API 可以直接写入到用户的文件系统(例如 ~/Documents%USERPROFILE%Documents),由用户指定.

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.

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

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).

关于 chrome.downloads:由于您的扩展程序是在内部,您可能可以强制用户进入 beta/dev 频道以使用此 API.此 API 的唯一限制是文件将保存在用户定义的下载文件夹(的子目录)中.

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.

chrome.downloads API 现在可用于所有渠道,包括稳定分支(自 Chrome 31 起).

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

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

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