从浏览器打开应用程序时无法从深度链接获取有效负载(Electron 20)

n8ghc7c1  于 2022-12-08  发布在  Electron
关注(0)|答案(1)|浏览(242)

我正在尝试处理我的应用程序中的深度链接(electron@20.1.3,electron-builder@23.1.0)。总的来说,它工作(至少在MacOS上)很好,但当我完全关闭我的应用程序,并试图打开应用程序与深度链接应用程序打开,但没有深度链接的行为。
我使用Official Documentation中的代码来解决这个问题,但是我看不到任何代码行。
第一个

lymnna71

lymnna711#

想通了!
我的错误是我在ipcMain中添加事件侦听器的时间太晚了,因为我使用的是app.whenReady,而不是app.on("open-url")
看起来app.whenReadyapp.on("open-url")更早触发,应用程序失去了侦听app.on("open-url")和获取运行应用程序附带的事件的机会。
最终代码是这样的:

"use strict";

const {
  app,
  BrowserWindow,
  session,
  ipcMain,
  Menu,
  nativeTheme,
} = require("electron");
const { homedir } = require("os");
const { resolve, join } = require("path");
const windowStateKeeper = require("electron-window-state");
const { menuTemplate } = require("./templates");

const isMac = process.platform === "darwin";
let startUpLink = "";

if (process.defaultApp) {
  if (process.argv.length >= 2) {
    app.setAsDefaultProtocolClient("my-application", process.execPath, [
      resolve(process.argv[1])
    ]);
  }
} else {
  app.setAsDefaultProtocolClient("my-application");
}

const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
  app.quit();
} else {
  app.on("second-instance", () => {
    const mainWindow = BrowserWindow.getAllWindows()[0];
    if (mainWindow) {
      if (mainWindow.isMinimized()) mainWindow.restore();
      mainWindow.focus();
    }
  });

  app.on("open-url", async (event, url) => {
    startUpLink = url;
    event.preventDefault();
    let mainWindow = BrowserWindow.getAllWindows()[0];
    if (mainWindow) {
      mainWindow.focus();
    } else {
      await createWindow();
      mainWindow = BrowserWindow.getAllWindows()[0];
    }
    mainWindow.webContents.send("handle-deep-link", url);
  });

  // placed after open-url handler just in case and use another ready event handler
  app.on("ready", async () => {
    await createWindow();
  });
}

const createWindow = async () => {
  nativeTheme.themeSource = "light";
  const mainWindowStateKeeper = windowStateKeeper({
    defaultWidth: 1600,
    defaultHeight: 900,
    fullScreen: true
  });

  const mainWindow = new BrowserWindow({
    width: mainWindowStateKeeper.width,
    height: mainWindowStateKeeper.height,
    x: mainWindowStateKeeper.x,
    y: mainWindowStateKeeper.y,
    icon: resolve(__dirname, "./assets/icon.png"),
    webPreferences: {
      contextIsolation: false,
      nodeIntegration: true,
      devTools: process.env.NODE_ENV === "development" || DEV_TOOLS
    },
    minWidth: 1008,
    minHeight: 738,
    title: "title",
    fullscreen: mainWindowStateKeeper.isFullScreen,
    fullscreenable: true
  });

  const menu = Menu.buildFromTemplate(
    menuTemplate(isMac, !PRODUCTION_BUILD || DEV_TOOLS, mainWindow)
  );

  Menu.setApplicationMenu(menu);

  await mainWindow.webContents.session.clearCache();

  mainWindow.setBackgroundColor("#ffffff");

  await mainWindow.loadURL(
    !PRODUCTION_BUILD
      ? "http://localhost:8080/index.html"
      : "file://" + resolve(__dirname, "../vue-app/index.html")
  );

  ipcMain.on("call-open-url", () => {
    if (startUpLink) {
      BrowserWindow.getAllWindows()[0].webContents.send(
        "handle-deep-link",
        startUpLink
      );
    }
  });

  mainWindow.on("close", () => {
    startUpLink = "";
    BrowserWindow.getAllWindows().forEach(otherWindow => {
      if (mainWindow.id !== otherWindow.id) {
        otherWindow.close();
      }
    });
  });

  mainWindowStateKeeper.manage(mainWindow);
};

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

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) createWindow();
});

相关问题