- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 ElectronJS 项目,我在这个项目中使用协议(protocol)(深层链接)。它适用于 MacOS 和 Windows,但在 Linux 上我无法理解如何创建此协议(protocol)。
我已经查看了 ElectronJS 文档以及网络上的问题等,但我无法弄清楚如何在 Linux 上初始化协议(protocol)。正如我在 MacOS 和 Windows 上取得的成功一样,我想要的只是实现一种在深层链接中与应用程序交互的协议(protocol)。
适用于 MacOS 和 Windows 的代码:
// main.ts
// –– B ––– PROTOCOL HANDLER –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
ProtocolUtils.setDefaultProtocolClient();
// eslint-disable-next-line default-case
switch (process.platform) {
case 'darwin':
ProtocolUtils.setProtocolHandlerOSX();
break;
case 'win32':
ProtocolUtils.setProtocolHandlerWin32();
break;
}
// –– E ––– PROTOCOL HANDLER –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// protocol.ts
export abstract class ProtocolUtils {
/**
* @description Create default protocole for call this app.
* Ex : in your browser => myapp://test
*/
public static setDefaultProtocolClient(): void {
if (!app.isDefaultProtocolClient('myapp')) {
// Define custom protocol handler.
// Deep linking works on packaged versions of the application!
app.setAsDefaultProtocolClient('myapp');
}
}
/**
* @description Create logic (WIN32) for open url from protocol
*/
public static setProtocolHandlerWin32(): void {
// Force Single Instance Application on win32
const gotTheLock = app.requestSingleInstanceLock();
app.on('second-instance', (e: Electron.Event, argv: string[]) => {
// Someone tried to run a second instance, we should focus our window.
if (MainWindow.mainWindow) {
if (MainWindow.mainWindow.isMinimized()) MainWindow.mainWindow.restore();
MainWindow.mainWindow.focus();
} else {
MainWindow.openMainWindow(); // Open main windows
}
app.whenReady().then(() => {
MainWindow.mainWindow.loadURL(this._getDeepLinkUrlForWin32(argv)); // Load URL in WebApp
});
});
if (gotTheLock) {
app.whenReady().then(() => {
MainWindow.openMainWindow(); // Open main windows
MainWindow.mainWindow.loadURL(this._getDeepLinkUrlForWin32()); // Load URL in WebApp
});
} else {
app.quit();
}
}
/**
* @description Create logic (OSX) for open url from protocol
*/
public static setProtocolHandlerOSX(): void {
app.on('open-url', (event: Electron.Event, url: string) => {
event.preventDefault();
app.whenReady().then(() => {
MainWindow.openMainWindow(); // Open main windows
MainWindow.mainWindow.loadURL(this._getUrlToLoad(url)); // Load URL in WebApp
});
});
}
/**
* @description Format url to load in mainWindow
*/
private static _getUrlToLoad(url: string): string {
// Ex: url = myapp://deep-link/test?params1=paramValue
// Ex: Split for remove myapp:// and get deep-link/test?params1=paramValue
const urlSplitted = url.split('//');
// Generate URL to load in WebApp.
// Ex: file://path/index.html#deep-link/test?params1=paramValue
const urlToLoad = format({
pathname: Env.BUILDED_WEBAPP_INDEX_PATH,
protocol: 'file:',
slashes: true,
hash: `#${urlSplitted[1]}`,
});
return urlToLoad;
}
/**
* @description Resolve deep link url for windows from process argv
*/
private static _getDeepLinkUrlForWin32(argv?: string[]): string {
let url: string;
const newArgv: string[] = !isNil(argv) ? argv : process.argv;
// Protocol handler for win32
// argv: An array of the second instance’s (command line / deep linked) arguments
if (process.platform === 'win32') {
// Get url form precess.argv
newArgv.forEach((arg) => {
if (/myapp:\/\//.test(arg)) {
url = arg;
}
});
if (!isNil(url)) {
return this._getUrlToLoad(url); // Load URL in WebApp
} else if (!isNil(argv) && isNil(url)) {
throw new Error('URL is undefined');
}
}
}
}
我不担心 macOS 和 windows,但在 linux 上,即使有以下行,该协议(protocol)也不存在:ProtocolUtils.setDefaultProtocolClient();
负责创建 myapp://
协议(protocol)...
当我运行这个命令时:xdg-open myapp://deep-link/test?toto=titi
一个错误告诉我这个协议(protocol)不存在
如果有人有一个例子让我在 Linux 上配置或者可以帮助我?
谢谢
最佳答案
好的,我已经找到解决方案了!
首先,我们删除了 electron-forge 并将其替换为 electron-builder(参见 doc)。
然后在阅读了大量关于 Linux 深度链接的文档之后,文档示例:
我的解决方案是:
# electron-builder.yml
appId: com.myapp.myapp
productName: myapp
directories:
output: out
linux:
icon: src/assets/icons/app/icon@256x256.png
category: Utility
mimeTypes: [x-scheme-handler/myapp] # Define MimeType
desktop: # Define desktop elem
exec: myapp %u # Define Exec
target:
- target: deb
arch:
- x64
所以我在这里用我的协议(protocol)名称定义了 MimeType myapp
可以给出:
myapp://toto?foo=bar
并且在我的桌面文件中用 myapp %u
定义 Exec 因为 %u
=> 一个 URL。本地文件可以作为 file: URL 或文件路径传递。 (cf doc )
为了完成我的 main.ts
和 protocol.utils.ts
:
// main.ts
// –– B ––– PROTOCOL HANDLER –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
ProtocolUtils.setDefaultProtocolClient();
switch (process.platform) {
case 'darwin':
ProtocolUtils.setProtocolHandlerOSX();
break;
case 'linux':
case 'win32':
ProtocolUtils.setProtocolHandlerWindowsLinux();
break;
default:
throw new Error('Process platform is undefined');
}
// –– E ––– PROTOCOL HANDLER –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
// protocol.utils.ts
export abstract class ProtocolUtils {
public static setDefaultProtocolClient(): void {
if (!app.isDefaultProtocolClient('myapp')) {
// Define custom protocol handler.
// Deep linking works on packaged versions of the application!
app.setAsDefaultProtocolClient('myapp');
}
}
/**
* @description Create logic (WIN32 and Linux) for open url from protocol
*/
public static setProtocolHandlerWindowsLinux(): void {
// Force Single Instance Application
const gotTheLock = app.requestSingleInstanceLock();
app.on('second-instance', (e: Electron.Event, argv: string[]) => {
// Someone tried to run a second instance, we should focus our window.
if (MainWindow.mainWindow) {
if (MainWindow.mainWindow.isMinimized()) MainWindow.mainWindow.restore();
MainWindow.mainWindow.focus();
} else {
// Open main windows
MainWindow.openMainWindow();
}
app.whenReady().then(() => {
MainWindow.mainWindow.loadURL(this._getDeepLinkUrl(argv));
});
});
if (gotTheLock) {
app.whenReady().then(() => {
// Open main windows
MainWindow.openMainWindow();
MainWindow.mainWindow.loadURL(this._getDeepLinkUrl());
});
} else {
app.quit();
}
}
/**
* @description Create logic (OSX) for open url from protocol
*/
public static setProtocolHandlerOSX(): void {
app.on('open-url', (event: Electron.Event, url: string) => {
event.preventDefault();
app.whenReady().then(() => {
if (!isNil(url)) {
// Open main windows
MainWindow.openMainWindow();
MainWindow.mainWindow.loadURL(this._getUrlToLoad(url));
} else {
this._logInMainWindow({ s: 'URL is undefined', isError: true });
throw new Error('URL is undefined');
}
});
});
}
/**
* @description Format url to load in mainWindow
*/
private static _getUrlToLoad(url: string): string {
// Ex: url = myapp://deep-link/test?params1=paramValue
// Ex: Split for remove myapp:// and get deep-link/test?params1=paramValue
const splittedUrl = url.split('//');
// Generate URL to load in WebApp.
// Ex: file://path/index.html#deep-link/test?params1=paramValue
const urlToLoad = format({
pathname: Env.BUILDED_APP_INDEX_PATH,
protocol: 'file:',
slashes: true,
hash: `#${splittedUrl[1]}`,
});
return urlToLoad;
}
/**
* @description Resolve deep link url for Win32 or Linux from process argv
* @param argv: An array of the second instance’s (command line / deep linked) arguments
*/
private static _getDeepLinkUrl(argv?: string[]): string {
let url: string;
const newArgv: string[] = !isNil(argv) ? argv : process.argv;
// Protocol handler
if (process.platform === 'win32' || process.platform === 'linux') {
// Get url form precess.argv
newArgv.forEach((arg) => {
if (/myapp:\/\//.test(arg)) {
url = arg;
}
});
if (!isNil(url)) {
return this._getUrlToLoad(url);
} else if (!isNil(argv) && isNil(url)) {
this._logInMainWindow({ s: 'URL is undefined', isError: true });
throw new Error('URL is undefined');
}
}
}
这是工作 :D
关于linux - Electron - 如何在 Linux 上创建深层链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65808445/
我是一名优秀的程序员,十分优秀!