将node.js嵌入到Firefox扩展中并在浏览器中运行服务器 [英] Embedding node.js in a Firefox extension and running a server in-browser

查看:163
本文介绍了将node.js嵌入到Firefox扩展中并在浏览器中运行服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何在一个Firefox扩展中嵌入node.js,以便我可以运行一个持久的服务器进程(特别是 PeerServer ),只要用户启用了扩展。我唯一能够在网上找到的是这个指南 ...但是我还没有做到这些指令的工作,并需要找到一些更多的资源。

1)有没有人有任何其他资源,除了我链接到上面的文章,谈论嵌入node.js Firefox扩展?任何代码示例?

2)是否有某些原因,在Firefox扩展中运行永久服务器进程(如PeerServer)是不可能的?有什么限制的扩展,将阻止我能够做到这一点?

解决方案

你可以只有可执行文件在你的扩展名的文件夹中,并在扩展启动的可执行文件中有JS代码。在链接的资源中描述运行外部可执行文件,或者在MDN中

从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);

需要更多的逻辑来找到用户配置文件的绝对路径来获得路径应用程序启动,但它是可行的。

现在,如果你想从扩展与节点交互,你可以使用HTTP请求作为一种沟通方式。



在Firefox中嵌入节点听起来有点奇怪,尽管Firefox本身有一个JS引擎。更优雅的方法是尝试让PeerJS直接在Firefox插件上下文中运行,而不需要节点。也许更复杂,但它应该是可能的。例如,参见这个插件浏览器服务器 p>

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".

这篇关于将node.js嵌入到Firefox扩展中并在浏览器中运行服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆