Chrome扩展程序:如何列出本地目录的内容 [英] Chrome Extension: How to List The Contents of Local Directory

查看:173
本文介绍了Chrome扩展程序:如何列出本地目录的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做什么:



我想为测试目的创建一个Chrome扩展程序。该扩展应该能够访问本地硬盘上的目录和文件



它:

我在清单中要求 file:/// * 的权限。 json 文件。然后我确定复选框允许访问文件URL 被选中。然后我为文件创建了一个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的
    默认人类可读目录概述:




     < / li> ; HTML> 
    < head>
    < script>

    [...]

     < / script> 
    < style>

    [...]

     < / style> 
    < title id =title>< / title>
    < / head>
    < body>
    < div id =listingParsingErrorBoxi18n-values =。innerHTML:listingParsingErrorBoxText>< / div>
    < span id =parentDirTextstyle =display:nonei18n-content =parentDirText>< / span>
    < h1 id =headeri18n-content =header>< / h1>
    < table id =table>
    < tr class =header>
    < td i18n-content =headerName>< / td>
    < td class =detailsColumni18n-content =headerSize>< / td>
    < td class =detailsColumni18n-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>




    • 当我请求整个文件系统时,出现错误。 / li>


    我的问题:



    我可以读取简单的文件。但是,我的扩展如何获得机器可读的目录概览或整个文件系统?谢谢。


    $ b 编辑: 我知道FileSystem API旨在访问沙盒 filesystem:// ,但也许Chrome允许扩展也访问 file:// 。我找不到任何有关 file:// 从Chrome扩展程序访问的文档,所以我只能猜测。

    解决方案

    Chrome独立版没有内置功能。



    解析目录列表页面的替代方法:
    $ b $ ol

  • NPAPI扩展(C ++,推荐)带有文件系统访问的本地服务器
  • NodeJS是一个很好的选择。 li>


    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.

    解决方案

    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.

    这篇关于Chrome扩展程序:如何列出本地目录的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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