代码之家  ›  专栏  ›  技术社区  ›  Zarkonnen

如何从Firefox插件内部加载文件

  •  10
  • Zarkonnen  · 技术社区  · 14 年前

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

    我能做到

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

    但这会尝试以javascript的形式执行XML文件。我只想要里面的东西!

    7 回复  |  直到 8 年前
        1
  •  6
  •   racetrack    14 年前

    JSLib io.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);
    
        2
  •  10
  •   Community Egal    7 年前

    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;
    }
    
        4
  •  3
  •   Greck    11 年前

    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
  •  0
  •   Kranu    14 年前

    Components.utils.import

        6
  •  0
  •   iammyr    10 年前
        7
  •  0
  •   Stephan    9 年前