简体   繁体   中英

Embedding node.js in a Firefox extension and running a server in-browser

I am trying to figure out how to embed node.js within a Firefox extension, so that I can run a persistent server process (specifically PeerServer ) from within the browser, as long as the user has the extension enabled. The only thing I've been able to find online is this guide ... but I haven't been able to make those instructions work, and need to find some more resources.

1) Does anyone have any other resources besides the article I linked to above that talk about embedding node.js in Firefox extensions? Any code examples?

2) Is there some reason that it would not be possible to run a persistent server process such as PeerServer from within a Firefox extension? Are there some kind of limitations on extensions that would prevent me from being able to do that?

You can just have the executable in a folder of your extension and have JS code in the extension launch that executable. Running an external executable is described in the resource you linked or here at MDN .

Example copied from MDN:

var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsIFile);
file.initWithPath("myapp.exe");

var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
process.init(file);

var args = ["argument1", "argument2"];
process.run(false, args, args.length);

A bit more logic is needed to find the absolute path of the user's profile to derive the path of the application to launch but it's doable.

Now if you want to interact with node from the extension you could use HTTP requests as a means of communication.

It sounds a bit strange to embed node in Firefox though when Firefox itself has a JS engine at its core. A more elegant approach would be to try to get PeerJS running directly in Firefox addon context, without node. Maybe more complicated but it should be possible. See for example this addon " Browser Server ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM