【发布时间】:2012-07-12 13:20:19
【问题描述】:
我想做什么:
我想为测试目的创建一个 Chrome 扩展程序。该扩展程序应该能够访问本地硬盘驱动器上的目录和文件。
我是怎么做到的:
我在manifest.json 文件中请求了file:///* 的权限。然后我确保选中了复选框Allow access to file URLs。然后我为一个文件做了一个 XHR:
var xhr = new XMLHttpRequest();
xhr.open("GET", 'file://c:/dir/file.name', true);
xhr.onload = function(e) {
console.log('response: ', xhr.response);
}
xhr.send();
... 以及整个目录的 XHR:
var xhr = new XMLHttpRequest();
xhr.open("GET", 'file://c:/dir/', true);
xhr.onload = function(e) {
console.log('response: ', xhr.response);
}
xhr.send();
...对于total文件系统:
var xhr = new XMLHttpRequest();
xhr.open("GET", 'file:///', true);
xhr.onload = function(e) {
console.log('response: ', xhr.response);
}
xhr.send();
发生了什么:
- 我能够为我的文件请求获得正确的响应。
- 当我请求一个目录时,我收到了 Chrome 的源代码 默认人类可读目录概览:
.
<html>
<head>
<script>
[...]
</script>
<style>
[...]
</style>
<title id="title"></title>
</head>
<body>
<div id="listingParsingErrorBox" i18n-values=".innerHTML:listingParsingErrorBoxText"></div>
<span id="parentDirText" style="display:none" i18n-content="parentDirText"></span>
<h1 id="header" i18n-content="header"></h1>
<table id="table">
<tr class="header">
<td i18n-content="headerName"></td>
<td class="detailsColumn" i18n-content="headerSize"></td>
<td class="detailsColumn" i18n-content="headerDateModified"></td>
</tr>
</table>
</body>
</html>
[...]
<script>start("C:\\dir\\");</script>
<script>addRow("..","..",1,"0 B","11/11/11 11:11:11 AM");</script>
<script>addRow("file.name","file.name",0,"4.9 kB","11/11/11 11:11:11 PM");</script>
- 当我请求整个文件系统时,出现错误。
我的问题:
我能够读取简单的文件。但是我的扩展如何才能获得一个很好的目录的机器可读概览或整个文件系统?谢谢。
编辑: 我知道 FileSystem API 旨在访问沙盒化的 filesystem://,但也许 Chrome 允许扩展程序也访问 file://。我找不到任何有关从 Chrome 扩展程序访问 file:// 的文档,所以我只能猜测。
【问题讨论】:
标签: google-chrome google-chrome-extension filesystems google-chrome-os