Chrome Extension: How to List The Contents of Local Directory

元气小坏坏 提交于 2019-12-12 08:39:16

问题


What I want to do:

I want to create a Chrome extension for test purposes. And that extension should be able to access the directories and files on the local hard drive.

How I did It:

I requested permissions for file:///* in my manifest.json file. Then I made sure that the checkbox Allow access to file URLs was checked. Then I made a XHR for a file:

  var xhr = new XMLHttpRequest();  
  xhr.open("GET", 'file://c:/dir/file.name', true);     
  xhr.onload = function(e) {  
      console.log('response: ', xhr.response);
  }  
  xhr.send();

... and a XHR for a whole directory:

  var xhr = new XMLHttpRequest();  
  xhr.open("GET", 'file://c:/dir/', true);     
  xhr.onload = function(e) {  
      console.log('response: ', xhr.response);
  }  
  xhr.send();

... and for the total file system:

  var xhr = new XMLHttpRequest();  
  xhr.open("GET", 'file:///', true);     
  xhr.onload = function(e) {  
      console.log('response: ', xhr.response);
  }  
  xhr.send();

What happened:

  • I was able to get the right response for my file request.
  • When I requested a directory I received the source code of Chrome's default human-readable directory overview:

.

  <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>
  • And when I requested the whole filesystem, I got an error.

My Question:

I'm able to read simple files. But how can my extension get a nice machine-readable overview of a directory or of the total filesystem? Thanks.

EDIT: I know that the FileSystem API is intended to access the sandboxed filesystem://, but maybe Chrome allows extensions to also access file://. I couldn't find any documentation concerning file:// access from Chrome extensions, so I'm just able to guess.


回答1:


There's no built-in feature in stand-alone Chrome.

Alternatives to parsing the directory listing page:

  1. NPAPI extensions (C++, recommended)
  2. a local server with filesystem access. NodeJS is a good pick.


来源:https://stackoverflow.com/questions/11452758/chrome-extension-how-to-list-the-contents-of-local-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!