Javascript Windows-Electron使用协议(带参数)到';做点什么';在当前的active electron应用程序中
更新如下 注意:此应用程序仅适用于windows,因此它排除了“打开url”等 我的应用程序使用了协议,它成功地打开了electron应用程序,但是我要寻找的是,如果应用程序打开,用户单击带有参数的协议链接(我的-app://go/to/this/section )它将打开当前应用程序并运行一个函数以转到链接 此时实际发生的是应用程序打开了另一个版本的自身,而没有传递参数 我在app.js中有了Javascript Windows-Electron使用协议(带参数)到';做点什么';在当前的active electron应用程序中,javascript,electron,Javascript,Electron,更新如下 注意:此应用程序仅适用于windows,因此它排除了“打开url”等 我的应用程序使用了协议,它成功地打开了electron应用程序,但是我要寻找的是,如果应用程序打开,用户单击带有参数的协议链接(我的-app://go/to/this/section )它将打开当前应用程序并运行一个函数以转到链接 此时实际发生的是应用程序打开了另一个版本的自身,而没有传递参数 我在app.js中有了setAsDefaultProtocolClient函数来尝试捕获协议的任何实例,但这似乎不起作用 c
setAsDefaultProtocolClient
函数来尝试捕获协议的任何实例,但这似乎不起作用
const {remote} = window.require('electron');
remote.app.setAsDefaultProtocolClient( 'my-app' );
我的主进程中也有makeSingleInstance
功能,一次只允许应用程序的一个实例运行,但是在关闭第二个客户端时,它似乎没有发送我通过协议传递的参数
// Make sure there is only 1 instance of the app running at a time
const isSecondInstance = app.makeSingleInstance((commandLine, workingDirectory) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
// If this is a second instance of the app close it
if (isSecondInstance) {
app.quit();
}
更新: 现在,我有了第二个窗口,将其协议发布到应用程序的原始实例,但是我无法记录参数 每当我登录
process.argv
并使用“我的-app://section/29 它只是安装目录的目录,没有任何参数
我已将以下内容添加到makeSingleInstance
if (process.platform == 'win32') {
deeplinkingUrl = process;
// Keep only command line / deep linked arguments
logEverywhere( process );
}
以下是logEverywhere
,仅供参考:
function logEverywhere(s) {
console.log(s);
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.executeJavaScript(`console.log("${s}")`);
}
}
在
makeSingleInstance
函数中,它有一个参数commandLine
,该参数包含第二个实例的命令行参数数组
其中将包括协议url
所以你可以这样写:
var isSecondInstance = exports.app.makeSingleInstance(function (commandLine, workingDirectory) {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
var protocolLink = commandLine[1];
// Run function to go to link.
}
});
if (isSecondInstance) {
app.quit();
}
其中,
protocolink
变量是用户最初单击的链接。新版本的Electron改变了其工作方式makeSingleInstance
已弃用。改用requestSingleInstanceLock
。这是一个样本
import { app, ipcMain as ipc } from 'electron'
import { find, compact } from 'lodash'
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
// mainWindow is window created with `new BrowserWindow(params)`
if (mainWindow) {
// And use ipc, or promiseIpc to send the command line
// to the renderer to handle it however we want
const cmd = find(commandLine, cmd => {
return cmd.indexOf('app-protocol-name') > -1
})
let actualCommands = cmd.replace('app-protocol-name://', '')
actualCommands = compact(actualCommands.split('/'))
ipc.send('NEW_COMMAND_LINE', mainWindow, actualCommands)
mainWindow.show()
mainWindow.restore()
mainWindow.focus()
}
})
}
命令行
有时是由10个或更多元素组成的数组。因此,您必须在其中搜索实际包含协议的元素。然后适当地分割和处理参数。
这段代码不会处理斜杠、引号等内容,但这只是一个开始谢谢,这让我更进一步了