Chrome Extension: How to List The Contents of Loca

2019-04-07 11:11发布

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条回答
霸刀☆藐视天下
2楼-- · 2019-04-07 11:43

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.
查看更多
登录 后发表回答