app.on('open-url") 深度链接后未在 linux 上调用事件

app.on('open-url") event not being called on linux after deep linking to it

我希望能够打开一个深度 link 并采用该协议 url 并使用它登录。我已经成功地让电子应用程序从深层 link 打开,但我似乎无法从深层 link 获取 url 或参数。我遵循了 https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app 的教程,但无法在 ubuntu 上运行它。在本教程中,当您从深度 link 打开时,它会打开一个对话框,但不会调用 open-url 侦听器。让 open-url 在 linux.

上工作是否缺少某些东西

这是我的 index.ts

import {app, BrowserWindow, ipcMain, powerMonitor, Menu, Tray, contextBridge, ipcRenderer, shell, dialog} from 'electron'
import path from "path"
import pushReceiver from "electron-fcm-push-receiver"

let mainWindow: BrowserWindow | null;
let deeplinkingUrl: any = null

try {
    if (process.defaultApp) {
        if (process.argv.length >= 2) {
            app.setAsDefaultProtocolClient('proto', process.execPath, [path.resolve(process.argv[1])])
        }
    } else {
        app.setAsDefaultProtocolClient('electron-fiddle')
    }
} catch(e) {
    logEverywhere(e);
}

const hasLock = app.requestSingleInstanceLock()

if (!hasLock) {
    app.quit()
} else {
    app.on('second-instance', (ev,argv) => {
        logEverywhere(ev)
        logEverywhere("Second instance was opened")

        if (mainWindow) {
            if (mainWindow.isMinimized()) mainWindow.restore()
            mainWindow.focus()
        }

        if (process.platform == 'win32') {
            // Keep only command line / deep linked arguments
            deeplinkingUrl = argv.slice(1)
        }

        logEverywhere(argv.slice(3))
        //mainWindow?.loadURL(argv.slice(3))
        logEverywhere('app.makeSingleInstance# ' + deeplinkingUrl)

    })

    app.on('open-url', (event, url) => {
        event.preventDefault()
        deeplinkingUrl = url
        dialog.showErrorBox('Welcome Back', `You arrived from: ${url}`)

        logEverywhere("testing" +url)
    })

    app.whenReady().then(() => {
        const w = new BrowserWindow({
            width: 1366,
            height: 768,
            webPreferences: {
                preload: path.join(__dirname, './preload.js'),
                nodeIntegration: true,
                enableRemoteModule: true
            } as any,
        });

        w.removeMenu();

        try {
            w.loadURL("https:local-host");
            mainWindow = w;
            pushReceiver.setup(w.webContents);
            logEverywhere("Loaded Why test");
            w.on('closed', function () {
                mainWindow = null;
            });

            const tray = new Tray(path.join(__dirname, "icon.png"), "asd546sdf-7dea-4b73-964b-6s5dfscsasdf");
            tray.setToolTip("Test Site");
            tray.setContextMenu(Menu.buildFromTemplate([
                {
                    label: "Open",
                    click: () => {
                        w.show()
                    }
                },
                {
                    label: 'Dev Console',
                    click: () => {
                        w.webContents.openDevTools()
                    },
                },
                {
                    label: "Reload",
                    click: () => {
                        w.webContents.reloadIgnoringCache()
                    }
                },
                {
                    label: "Quit",
                    click: () => {
                        app.quit()
                    }
                }
            ]));
            tray.setTitle("Test Site");
            tray.on("click", ev => {
                w.show()
            });

            // w.webContents.send("asdf", 2)
        } catch (e) {
            console.error(e);
        }
    });

    (global as any).goToForeground = () => mainWindow?.show()

    app.on('window-all-closed', function () {
        if (process.platform !== 'darwin') {
            app.quit();
        }
    });

    ipcMain.on('app_version', (event: Event) => {
        (event as any).sender.send('app_version', {version: app.getVersion()});
    });

    ipcMain.on('goToForeground', (event) => {
        console.log("Showing")
        mainWindow?.show()
    })

    powerMonitor.on('shutdown', (ev: Event) => {
        ev.preventDefault()
        app.quit()
    })
}

function logEverywhere(s: any) {
    console.log(s)
    if (mainWindow && mainWindow.webContents) {
        mainWindow.webContents.executeJavaScript(`console.log("${s}")`)
    }
}

// Handle window controls via IPC
ipcMain.on('shell:open', () => {
    const pageDirectory = __dirname.replace('app.asar', 'app.asar.unpacked')
    const pagePath = path.join('file://', pageDirectory, 'index.html')
    shell.openExternal(pagePath)
})

这是我的 package.json 文件

{
  "name": "test-electron",
  "version": "1.0.0",
  "description": "Electron wrapper",
  "main": "dist/index.js",
  "scripts": {
    "start": "tsc && electron-forge start",
    "package": "electron-forge package",
    "make": "electron-forge make",
    "publish": "electron-forge publish",
    "lint": "eslint --ext .ts ."
  },
  "author": "Test",
  "license": "ISC",
  "config": {
    "forge": {
      "packagerConfig": {
        "protocols": [
          {
            "name": "electron-fiddle",
            "schemes": ["electron-fiddle"]
          }
        ]
      },
      "makers": [
        {
          "name": "@electron-forge/maker-squirrel",
          "config": {
            "name": "my_new_app"
          }
        },
        {
          "name": "@electron-forge/maker-zip",
          "platforms": [
            "darwin"
          ]
        },
        {
          "name": "@electron-forge/maker-deb",
          "config": {
            "mimeType": ["x-scheme-handler/electron-fiddle"]
          }
        }
      ]
    }
  },
  "devDependencies": {
    "@electron-forge/cli": "^6.0.0-beta.63",
    "@electron-forge/maker-deb": "^6.0.0-beta.63",
    "@electron-forge/maker-rpm": "^6.0.0-beta.63",
    "@electron-forge/maker-squirrel": "^6.0.0-beta.63",
    "@electron-forge/maker-zip": "^6.0.0-beta.63",
    "@typescript-eslint/eslint-plugin": "^4.33.0",
    "@typescript-eslint/parser": "^4.33.0",
    "electron": "^17.1.1",
    "electron-fcm-push-receiver": "^2.1.7",
    "typescript": "^4.6.2"
  },
  "dependencies": {
    "electron-squirrel-startup": "^1.0.0",
    "electron-updater": "^4.6.5",
    "@electron-forge/cli": "^6.0.0-beta.63",
    "@electron-forge/maker-deb": "^6.0.0-beta.63",
    "@electron-forge/maker-rpm": "^6.0.0-beta.63",
    "@electron-forge/maker-squirrel": "^6.0.0-beta.63",
    "@electron-forge/maker-zip": "^6.0.0-beta.63",
    "@typescript-eslint/eslint-plugin": "^4.33.0",
    "@typescript-eslint/parser": "^4.33.0",
    "electron": "^17.1.1",
    "electron-fcm-push-receiver": "^2.1.7",
    "typescript": "^4.6.2"
  }
}

找出问题所在。该文档具有相同的 linux 和 mac 但 linux 不同或至少在 debian 上是这样。文档说要放入 app.on('open-url', (event, url) 事件,但这对 linux 不起作用,或者至少对我不起作用。

显然或者我发现你必须从操作系统调用它打开时的参数中获取它。所以我所做的是在准备好事件完成时在应用程序中做的:

app.whenReady().then(() => {                
    const argv = process.argv 
    const lastArg = argv[argv.length-1]
});

所以如果你没有应用程序运行如果应用程序已经运行那么你需要从使用中获取参数 link像这样的二审事件:

app.on('second-instance', (ev:Event, argv: string[], workingDirectory:string, additionalData) => {
    const lastArg = argv[argv.length-1]
    if (lastArg.startsWith("protocal://")) {
        onDeepLink(argv[argv.length-1])
    }

我认为这也是它与 windows 一起工作的方式,但我需要为此做更多测试。