【问题标题】:How to load file from inside Firefox plugin如何从 Firefox 插件内部加载文件
【发布时间】:2010-09-22 19:13:03
【问题描述】:

我正在开发一个 Firefox 插件,其中包含一个包含一些 HTML 数据的文件。如何将此文件作为字符串加载?

我可以的

var contents = Components.utils.import("resource://stuff.html");

但随后会尝试将 XML 文件作为 Javascript 执行。我只想要它的内容!

【问题讨论】:

    标签: javascript firefox firefox-addon


    【解决方案1】:

    使用此功能,您可以读取具有 chrome 范围的文件。

    function Read(file)
    {
        var ioService=Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
        var scriptableStream=Components
            .classes["@mozilla.org/scriptableinputstream;1"]
            .getService(Components.interfaces.nsIScriptableInputStream);
    
        var channel=ioService.newChannel(file,null,null);
        var input=channel.open();
        scriptableStream.init(input);
        var str=scriptableStream.read(input.available());
        scriptableStream.close();
        input.close();
        return str;
    }
    
    var contents = Read("chrome://yourplugin/stuff.html");
    

    Example loading CSS content and injecting on a page.

    编辑:

    只是为了更新它,因为它有点方便!

    let { Cc, Ci } = require('chrome');
    function read(file){
        var ioService = Cc["@mozilla.org/network/io-service;1"]
            .getService(Ci.nsIIOService);
        var scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"]
            .getService(Ci.nsIScriptableInputStream);
    
        var channel = ioService.newChannel2(file, null, null, null, null, null, null, null);
        var input = channel.open();
        scriptableStream.init(input);
        var str = scriptableStream.read(input.available());
    
        scriptableStream.close();
        input.close();
        return str;
    }
    

    【讨论】:

    • 感谢您的回答,这个回答非常有用,而且是正确的 - 但我最终通过赛道的回答找到了我的解决方案,所以我给了他们赏金。
    • 这对我来说是最好的答案
    【解决方案2】:

    对于 Firefox 中的文件系统交互,请使用 Mozilla XPCOM 组件。有一些 I/O XPCOM 组件的包装器,例如 JSLibio.js

    使用io.js 会是这样的:

    var file = DirIO.get("ProfD"); // Will get you profile directory
    file.append("extensions"); // extensions subfolder of profile directory
    file.append("{1234567E-12D1-4AFD-9480-FD321BEBD20D}"); // subfolder of your extension (that's your extension ID) of extensions directory
    // append another subfolder here if your stuff.xml isn't right in extension dir
    file.append("stuff.xml");
    var fileContents = FileIO.read(file);
    var domParser = new DOMParser();
    var dom = domParser.parseFromString(fileContents, "text/xml");
    // print the name of the root element or error message
    dump(dom.documentElement.nodeName == "parsererror" ? "error while parsing" : dom.documentElement.nodeName);
    

    【讨论】:

    • 哪些 XPCOM 组件?我真的无法掌握 Firefox 文档,它似乎没有可用组件的索引。另外,如果我在 FileIO.open 中指定一个相对路径,它的根会在哪里?
    • @Zarkonnen 更新了示例代码。您需要 Mozilla I/O XPCOM 组件 (@mozilla.org/file/*)
    【解决方案3】:

    我认为这些链接会很有帮助... 这些告诉了如何实现 Json 以及一些关于 firefox 接口的东西

    http://www.json.org/js.html

    https://developer.mozilla.org/en/JSON

    希望对你有帮助:)

    【讨论】:

      【解决方案4】:

      使用可以从任何方案(chrome://、resource://、http://、...)读取的 XPCOM 的异步解决方案:

      const Cc = Components.classes;
      const Ci = Components.interfaces;
      const nsIIOService = Cc["@mozilla.org/network/io-service;1"]
                           .getService(Ci.nsIIOService);
      function get_url_async(_url, /* function(data) */ _callback_success, /* function(status) */ _callback_fail)
      {
          var channel=nsIIOService.newChannel(_url,null,null);
          channel.asyncOpen(
              {
                  buffer:null,
                  onStartRequest: function(/*in nsIRequest*/ aRequest, /*in nsISupports*/ aContext)
                  {
                      this.buffer = "";
                  },
                  onStopRequest: function(/*in nsIRequest*/ aRequest, /*in nsISupports*/ aContext, /*in nsresult*/ aStatusCode)
                  {
                      if(aStatusCode === Cr.NS_OK)
                          _callback_success(this.buffer);
                      else
                          _callback_fail(aStatusCode);
                  },
                  onDataAvailable: function(/*in nsIRequest*/ aRequest, /*in nsISupports*/ aContext, /*in nsIInputStream*/ aInputStream, /*in unsigned long*/ aOffset, /*in unsigned long*/ aCount)
                  {
                      var scriptable_in_stream = Cc["@mozilla.org/scriptableinputstream;1"]
                                                 .createInstance(Ci.nsIScriptableInputStream);
                      scriptable_in_stream.init(aInputStream);
                      this.buffer += scriptable_in_stream.read(aCount);
                      scriptable_in_stream.close();
                  }
              }, 
              /* context */ null
          );
      }
      

      用法:

      get_url_async(
          "resource://stuff.html", 
          function success(html)
          {
              // actions with html
          },
          function fail(status)
          {
              dump("Cannot get resource://stuff.html status code:"+status);
          }
      );
      

      【讨论】:

        【解决方案5】:

        Components.utils.import 用于 Javascript 代码模块。

        如果要使用该命令,则必须将数据存储为 JSON。

        JSON 有点类似于 XML,因为它是为数据设计的,但 JSON 很容易与 Javascript 代码集成。

        【讨论】:

          【解决方案6】:

          对我来说,BunoLM 的解决方案有效(感谢一百万!)但仅在将文件 url 传递给读取函数时使用“资源”协议之后,原因 explained elsewhere

          我在这里添加它是因为我花了一段时间才弄清楚这一点,所以希望它能帮助其他人;)

          【讨论】:

            【解决方案7】:

            我想你正在寻找nsILocalFile

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-18
              • 2014-04-27
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多