[英]How do i get inputs when opening my electron app from the browser
So Basically i want to call所以基本上我想打电话
masslinker://123/456
and it's supposed to goto show id 123 episode number 456. How do i get those inputs?它应该显示 id 123 第 456 集。我如何获得这些输入?
I Currently have this line of code我目前有这行代码
app.setAsDefaultProtocolClient('masslinker');
Citing from the corresponding docs :引用相应的文档:
Once registered, all links with your-protocol:// will be opened with the current executable.
注册后,所有与 your-protocol:// 的链接都将使用当前可执行文件打开。 The whole link, including protocol, will be passed to your application as a parameter.
整个链接,包括协议,将作为参数传递给您的应用程序。
That means that a new instance of your app will be launched whenever a URL of that type is requested.这意味着只要请求该类型的 URL,就会启动应用程序的新实例。
You should therefore check for arguments on app startup and if there is one, you can handle it:因此,您应该检查应用程序启动时的参数,如果有,您可以处理它:
if (process.argv.length >= 3) {
const url_to_open = process.argv[2];
console.log("Received: " + url_to_open);
// should print:
// Received: masslinker://123/456
// now take URL apart using string operations ..
}
The exact number of parameters may change depending on how you launch your app and/or when you go into production.参数的确切数量可能会根据您启动应用程序的方式和/或投入生产的时间而变化。 You may work around that by checking if the last argument matches your protocol prefix.
您可以通过检查最后一个参数是否与您的协议前缀匹配来解决这个问题。
Note that if you don't take further measures, this will open a new instance of your app for each URL you open.请注意,如果您不采取进一步措施,这将为您打开的每个网址打开一个新的应用实例。 You can counteract that using
app.requestSingleInstanceLock()
and the second-instance
event.您可以使用
app.requestSingleInstanceLock()
和second-instance
事件来抵消。 The event handler will get as second parameter all the command line parameters of the new instance, so that you can handle them in the first instance.事件处理程序将获取新实例的所有命令行参数作为第二个参数,以便您可以在第一个实例中处理它们。
Further note that setting up a protocol handler seems to be quite dependent on the operating system and may be part of the installation procedure of your app.进一步注意,设置协议处理程序似乎非常依赖于操作系统,并且可能是应用程序安装过程的一部分。 However, once the handler is set up, processing incoming calls should work as above regardless of the operating system.
但是,一旦设置了处理程序,无论操作系统如何,处理传入呼叫都应按上述方式工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.