如何在Node.js中注册url协议处理程序 [英] How to register a url protocol handler in Node.js

查看:259
本文介绍了如何在Node.js中注册url协议处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个命令行节点模块,并希望能够通过网站上的链接启动它。

I am developing a command line node module and would like to be able to launch it via links on a website.

我想注册一个自定义协议 my-module:// 这样链接的格式如下: my-module:// action:some-action 然后单击它们将启动节点包。

I want to register a custom protocol my-module:// such that links would have the following format: my-module://action:some-action and clicking on them would start the node package.

如果没有节点API(我确定不会有),那么是否有我可以通过调用系统命令从节点做到这一点吗?

If there isn't a node API for this (I'm sure there won't be) then is there a way I can do it from node by invoking system commands?

它必须适用于Windows,Linux和MacOS。

It must work on Windows, Linux, and MacOS.

推荐答案

这是一个有趣的想法。我认为目前没有跨平台node.js解决方案。我确实遇到过这个人要求同样的事情:

Its an interesting idea. I don't think there is currently a cross platform node.js solution out there. I did come across this thread of people asking for the same thing:

https://github.com/rogerwang/node-webkit/issues/951

Electron现在支持 app.setAsDefaultProtocolClient API(对于macOS和Windows,自v0.37.4起。)

Electron now supports it with the app.setAsDefaultProtocolClient API (since v0.37.4) for macOS and Windows.

编写库来执行此操作并不是非常困难。

It wouldn't be terribly difficult to write the library to do this.

Windows

在Windows端,您必须将应用程序注册为处理该URI方案的应用程序。

On the windows side you'd have to register the app as the application that handles that URI scheme.

您需要为您的应用程序设置一个注册表项:

You'll need to set up a registry entry for your application:


HKEY_CLASSES_ROOT
   alert
      (Default) = "URL:Alert Protocol"
      URL Protocol = ""
      DefaultIcon
         (Default) = "alert.exe,1"
      shell
         open
            command
               (Default) = "C:\Program Files\Alert\alert.exe" "%1"

然后,当您的应用程序由Windows运行时,您应该能够在<$ c中看到参数$ C> process.argv [] 。确保启动shell以运行节点,而不仅仅是直接运行应用程序。

Then, when your application is run by windows, you should be able to see the arguments in process.argv[]. Make sure that you launch a shell to run node, not just your application directly.

原始MSDN文章

请注意,这需要管理员权限并在系统范围内设置处理程序。要按用户执行此操作,您可以使用 HKEY_CURRENT_USER \Software \Classes 作为Electron的实现做到了。

Note this requires administrator privileges and sets the handler system-wide. To do it per user, you can use HKEY_CURRENT_USER\Software\Classes instead, as the Electron's implementation does it.

Apple:

github评论中引用的OS X文章实际上是针对iOS的。我将查看以下编程指南,了解有关注册应用程序以处理URL方案的信息:

The cited "OS X" article in the github comment is actually for iOS. I'd look at the following programming guide for info on registering an application to handle a URL scheme:

Apple Dev Documentation

总之,你需要创建一个启动服务并用 CFBundleURLTypes 填充.plist文件,这个字段是一个数组并且应该只填充协议名称,即 http

In summary, you'll need to create a launch service and populate the .plist file with CFBundleURLTypes, this field is an array and should be populated with just the protocol name i.e. http

以下超级用户问题有一个更好的解决方案,但是是按用户设置。

The following Super User Question has a better solution, but is a per user setting.

你找的文件是〜/ Library / Preferences / com.apple.LaunchServices.plist。

"The file you seek is ~/Library/Preferences/com.apple.LaunchServices.plist.

它持有一个名为LSHandlers的数组,以及定义LSHandlerURLScheme c的Dictionary子类用LSHandlerRole相应地修改。

It holds an array called LSHandlers, and the Dictionary children that define an LSHandlerURLScheme can be modified accordingly with the LSHandlerRole."

Linux:

从什么我可以说,有几种方法可以在Linux中实现这一点(惊喜?)

From what I can tell, there are several ways to accomplish this in Linux (surprise?)

Gnome有一个工具可以让你注册一个url处理程序 w3档案

Gnome has a tool that will let you register a url handler w3 archives

gconftool-2 -t string -s /desktop/gnome/url-handlers/tel/command "bin/vonage-call %s"
gconftool-2 -s /desktop/gnome/url-handlers/tel/needs_terminal false -t bool
gconftool-2 -t bool -s /desktop/gnome/url-handlers/tel/enabled true

一些较轻的管理器看起来像是允许你创建假的mime类型和将它们注册为URI协议处理程序。

Some of the lighter weight managers look like they allow you to create fake mime types and register them as URI Protocol handlers.

为各种方案的URI创建伪mime类型,如下所示:
application / x-xdg-protocol-
支持特定URI的应用程序协议可以将伪mime类型添加到其桌面条目文件中的MimeType键。因此,通过查看mimeinfo.cache文件,可以很容易地找到支持URI方案的系统上安装的所有应用程序。再次使用defaults.list文件为speficied URI类型指定默认程序。 wiki.lxde.org

"Fake mime-types are created for URIs with various scheme like this: application/x-xdg-protocol- Applications supporting specific URI protocol can add the fake mime-type to their MimeType key in their desktop entry files. So it's easy to find out all applications installed on the system supporting a URI scheme by looking in mimeinfo.cache file. Again defaults.list file can be used to specify a default program for speficied URI type." wiki.lxde.org

KDE还支持自己处理URL协议处理程序的方法:

KDE also supports their own method of handling URL Protocol Handlers:

创建文件: $ KDEDIR / share / services / your.protocol 并用相关数据填充:

Create a file: $KDEDIR/share/services/your.protocol and populate it with relevant data:


[Protocol]
exec=/path/to/player "%u"
protocol=lastfm
input=none
output=none
helper=true
listing=
reading=false
writing=false
makedir=false
deleting=false

来自 last.fm论坛

希望有所帮助。

这篇关于如何在Node.js中注册url协议处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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