I have a web application that my client uses for the cash registry. What I need to do is to create a local file as the cash register's software needs to read from that file in order to print.
Until now i was using this code:
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filePath);
Unfortunately with the latest version of firefox this isn't working anymore so I was told that i need and add-on to create the file.I've tried to develop an add-on(don't know if succesfully) and i have main.js looking like this :
var FileManager =
{
Write:
function (File, Text)
{
if (!File) return;
const unicodeConverter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
unicodeConverter.charset = "UTF-8";
Text = unicodeConverter.ConvertFromUnicode(Text);
const os = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
os.init(File, 0x02 | 0x08 | 0x20, 0700, 0);
os.write(Text, Text.length);
os.close();
},
Read:
function (File)
{
if (!File) return;
var res;
const is = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
const sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
.createInstance(Components.interfaces.nsIScriptableInputStream);
is.init(File, 0x01, 0400, null);
sis.init(is);
res = sis.read(sis.available());
is.close();
return res;
},
};
Any ideas how should I use main.js?Where I find it after the add-on is installed? I need to use something like this : FileManager.Write(path,text).
Sorry about the super-late reply.
If I understand your question correctly, you have a P.O.S application that runs in Firefox talking to some sort of local webserver via HTTP. The client-side JavaScript of your application needs to be able to read & write files from the local filesystem of the browser's PC.
If that's correct, then you can do so as follows. You'll need to create a Firefox addon, the simpliest kind of which is called a "bootstrapped" (or "restartless") addon.
A restartless addon consists of two files:
To build the addon, simply place both files inside the top-level (no folders!) of a ZIP file with the file extension
.xpi
. To install the addon, navigate toabout:addons
then from the tools menu, clickInstall from file
, find your XPI, open it, then after a short delay chooseInstall
.In
install.rdf
put something like this:You need to implement two mandatory JavaScript functions in the
bootstrap.js
:startup()
- called when you install the addon, and when your browser starts up.shutdown()
- called when you uninstall the addon, and when your browser shuts down.You should call the rest of your 'privileged' code in
startup()
. For hygiene, you can (and probably should) also implementinstall()
anduninstall()
functions.Start by implementing the following code in
bootstrap.js
:Basically, that calls
WindowListener.setupBrowserUI()
for each current & future window of your web-browser.WindowListener
is defined as follows:That sets up an event listener for the
OpenWindow
event, and in turn installs an event listener forload
events in theTabBrowser
of each ChromeWindow. Theload
event handler is defined as:That targets the correct pages (by checking the
window.location.href
and callsinstall_my_privileged_methods
on theirwindow
object, which is defined as:I didn't test this code. It's a straigh-forward translation of what you wrote above... I'll assume that works!
That add two special methods,
WriteFile
&ReadFile
, to the chosenwindow
objects. In your web application's (unprivileged) JavaScript code use them like this:Finally,
install_privileged_method
is defined as: