简体   繁体   English

从浏览器打开我的电子应用程序时如何获取输入

[英]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.

相关问题 如何使我的 Electron 应用程序成为打开文件的默认应用程序? - How do I make my Electron app the default for opening files? 当我从最近的选项卡中删除浏览器时,我的表单会丢弃用户输入 如何修复 - My Form Discard User Inputs When I Remove My Browser From Recent Tabs How To Fix 如何获得系统范围的代理设置并将其应用于我的电子应用程序? - How do I get system wide proxy settings and apply the same for my electron app? 如何防止应用浏览器中的Facebook打开我的网站链接? - How to prevent Facebook in app browser from opening my website links? 如何将图标放在我的 electron 应用程序中? - How do I put the icon in my electron app? 如何在不打开浏览器的情况下从提供的URL获取结果[Php代码/ AJAX] - How do i get the result from provided URL without opening the browser [Php Code/AJAX] 电子:如何与浏览器窗口通信? - Electron: How do I communicate with browser window? 如何从脚本中检查Electron应用程序的DOM? - How do I inspect an Electron app's DOM from a script? 我如何确保只能从电子窗口内部访问Electron Express应用 - How do i make sure that Electron express app is only accessible from inside electron window 如何更改 Electron 应用程序的图标 - How do I change the icon for Electron app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM