Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added content/.frame.js.swp
Binary file not shown.
35 changes: 35 additions & 0 deletions content/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,39 @@ var CommandRunHandler = function() {
this.isPrefix = function(string,prefix) {
return (prefix === string.substring(0,prefix.length));
}

/**
* Reads a file from a directory and returns its contents.
* Useful for reading command output.
*
* The variable dir should have one of the values listed here:
* https://developer.mozilla.org/en-US/Add-ons/Code_snippets/File_I_O
* e.g. "Home" or "TmpD".
*
* WARNING: Do not use on very large files, as your browser may freeze.
*/
this.readFile = function (dir, filename, encoding = "UTF-8") {
Components.utils.import("resource://gre/modules/FileUtils.jsm");

var file = FileUtils.getFile(dir, [filename]);
var data = "";

var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
createInstance(Components.interfaces.nsIConverterInputStream);
fstream.init(file, -1, 0, 0);
cstream.init(fstream, encoding, 0, 0);

let str = {};
let read = 0;
do {
read = cstream.readString(0xffffffff, str);
data += str.value;
} while (read != 0);

cstream.close();

return data;
}
}