Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load file from inside Firefox plugin

I'm working on a Firefox plugin which contains a file holding some HTML data. How do I load in this file as a string?

I can do

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

but that then tries to execute the XML file as Javascript. I just want its contents!

like image 487
Zarkonnen Avatar asked Sep 22 '10 19:09

Zarkonnen


People also ask

How do I open a file directly in Firefox?

If you were looking for a menu option, you can find it by clicking the Firefox button in the top left corner, then New Tab, then Open File. If the menu bar is displayed instead of the Firefox button, click the File menu, then Open File.

How do I load an HTML file in Firefox?

html files open in Firefox automatically (because Firefox is your default browser), double-clicking the file.

How do I import an XPI file into Firefox?

xpi file; from the Firefox File menu, select Open File..., and open the saved Extension's . xpi file. A Software Installation dialog will appear. Select Install Now to start the installation.


2 Answers

Using this function you can read files withing chrome scope.

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.

EDIT:

Just to update this because it is kinda handy !

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;
}
like image 107
BrunoLM Avatar answered Oct 26 '22 13:10

BrunoLM


For filesystem interactions in Firefox, use Mozilla XPCOM components. There are some wrappers for I/O XPCOM components such as JSLib and io.js

Using io.js it'd be something like:

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);
like image 36
racetrack Avatar answered Oct 26 '22 12:10

racetrack