【问题标题】:Chrome Extension: How to List The Contents of Local DirectoryChrome 扩展:如何列出本地目录的内容
【发布时间】: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


    【解决方案1】:

    独立 Chrome 中没有内置功能。

    解析目录列表页面的替代方案:

    1. NPAPI 扩展(C++,推荐)
    2. 具有文件系统访问权限的本地服务器。 NodeJS is a good pick

    【讨论】:

    • 谢谢,但 Npapi(或任何外部软件)不适​​合我。如果没有人对此有任何想法,我需要为 chrome 的人类可读目录概述编写一个解析器。但这不是我想做的,因为目录概览源代码的结构可能会在以后的 chrome 版本中发生变化。
    • Chrome 从未公开任何目录读取功能,因为在正常情况下,文件系统 API 就足够了。任何其他与 Web 无关的功能都应通过 NPAPI 实现。我认为您可能有机会使用 Flash,但我不确定。
    • NPAPI 不再是一个选项。 Chrome 在 2015 年 9 月取消了对它的支持。来源:support.google.com/chrome/answer/6213033?hl=en
    猜你喜欢
    • 2012-02-07
    • 2015-01-16
    • 1970-01-01
    • 2014-06-03
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2021-06-15
    相关资源
    最近更新 更多