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!
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.
html files open in Firefox automatically (because Firefox is your default browser), double-clicking the file.
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.
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;
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With