my-module://
,使链接具有以下格式:my-module://action:some-action
,点击它们将启动node包。 如果没有针对此的Node API(我确定不会有),那么我是否可以通过调用系统命令从Node中完成呢? 它必须在Windows,Linux和MacOS上工作。my-module://
,使链接具有以下格式:my-module://action:some-action
,点击它们将启动node包。 如果没有针对此的Node API(我确定不会有),那么我是否可以通过调用系统命令从Node中完成呢? 它必须在Windows,Linux和MacOS上工作。这是一个有趣的想法。我认为目前还没有跨平台的node.js解决方案。我找到了一些人询问同样问题的帖子:
https://github.com/rogerwang/node-webkit/issues/951
app.setAsDefaultProtocolClient
API(自v0.37.4起)在macOS和Windows上设置默认协议客户端。
编写此库不会很困难。
Windows:
在Windows方面,您需要将应用程序注册为处理该URI方案的应用程序。
您需要为应用程序设置注册表条目:
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运行您的应用程序时,您应该能够在process.argv[]
中看到参数。确保启动一个shell来运行node,而不仅仅是直接运行您的应用程序。
请注意,这需要管理员权限并在系统范围内设置处理程序。要按用户进行设置,您可以使用HKEY_CURRENT_USER\Software\Classes
,因为Electron的实现这样做。
Apple:
在github评论中引用的“OS X”文章实际上是针对iOS的。我建议查看以下编程指南,了解如何注册应用程序以处理URL方案:
简而言之,您需要创建一个启动服务并使用CFBundleURLTypes
填充.plist文件,此字段是数组,应仅填充协议名称,即http
。
以下Super User问题提供了更好的解决方案,但是它是每个用户的设置。
“您要查找的文件是〜/ Library / Preferences / com.apple.LaunchServices.plist。”
它保存了一个名为LSHandlers的数组,定义LSHandlerURLScheme的Dictionary子元素可以相应地进行修改,以便与LSHandlerRole配合使用。
Linux:
据我所知,在Linux中有几种方法可以完成这个任务(惊喜?)
Gnome有一个工具,可以让您注册一个url处理程序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
以下是我在Mac OS上使用NW.js应用程序的方法:
Open the app /Applications/Utilities/Script Editor
type the following code in the editor
on open location this_URL
do shell script "/Applications/X.app/Contents/MacOS/x '" & this_URL & "'"
end open location
Replace X by the name of your App.
Save the script as an Application Bundle anywhere
Go to the script, right click then 'Show Package Contents' then edit Contents/info.plist
Add these lines at the end of the file, just before </dict></plist>
:
<key>CFBundleIdentifier</key>
<string>com.mycompany.AppleScript.AppName</string> <!-- edit here -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>AppName</string> <!-- edit here -->
<key>CFBundleURLSchemes</key>
<array>
<string>myurlscheme</string> <!-- your url scheme here -->
</array>
</dict>
</array>
You can now open a link starting with myurlscheme: and see your app is opening!
看起来这个模块已经永久改变了注册流程:
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
console.log('Registering...');
// Registers the Protocol
ProtocolRegistry.register({
protocol: 'testproto', // sets protocol for your command , testproto://**
command: `node ${path.join(__dirname, './tester.js')} $_URL_`, // this will be executed with a extra argument %url from which it was initiated
override: true, // Use this with caution as it will destroy all previous Registrations on this protocol
terminal: true, // Use this to run your command inside a terminal
script: false
}).then(async () => {
console.log('Successfully registered');
});
有一个适用于此目的的npm模块。
链接:https://www.npmjs.com/package/protocol-registry
所以在nodejs中,您只需要运行以下代码即可:
首先安装它
npm i protocol-registry
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
console.log('Registering...');
// Registers the Protocol
ProtocolRegistry.register({
protocol: 'testproto', // set your app for testproto://**
command: `node ${path.join(__dirname, './index.js')}`, // this will be executed with a extra argument %url from which it was initiated
}).then(async () => {
console.log('Successfully registered');
});
node yourapp/index.js testproto://test