You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron冷启动时Deep Linking无法传递链接属性问题

深度链接冷启动失效问题修复(Win10 + Electron 21.3.1)

问题概述

Win10环境下基于Electron 21.3.1开发的应用,通过example://example.com触发深度链接时:

  • 应用已运行状态下,跳转功能正常
  • 冷启动应用时,无法传递深度链接参数,默认加载预定义的deeplinkingUrl值

问题根源

  1. 协议名拼写错误:代码中多处将example误写为exmaple,导致系统协议关联不匹配
  2. 冷启动参数未解析:Win10冷启动时,深度链接参数会传入process.argv,但原代码中相关解析逻辑被注释,未执行
  3. 窗口加载逻辑冗余:createWindow中重复调用loadURL,可能覆盖正确的参数值

修复方案

步骤1:修正协议名拼写

将所有exmaple替换为example,包括协议注册、参数匹配等场景。

步骤2:冷启动时解析参数

在createWindow函数中恢复Win10参数解析逻辑,确保冷启动时能获取到深度链接。

步骤3:优化窗口加载逻辑

确保窗口仅在参数确定后执行一次loadURL,避免重复加载覆盖正确参数。

修复后的完整代码

const { app, BrowserWindow } = require('electron')
const path = require('path')

let mainWindow
let deeplinkingUrl = "https://example.com/"

const gotTheLock = app.requestSingleInstanceLock()

if (!gotTheLock) {
    app.quit()
} else {
    app.on('second-instance', (event, argv, cwd) => {
        if (process.platform === 'win32') {
            // 精准匹配深度链接参数
            const argUrl = argv.find(arg => arg.startsWith('example://'))
            if (argUrl) {
                deeplinkingUrl = argUrl.replace("example", "https")
            }
        }
        logEverywhere("#second-instance", deeplinkingUrl)
        
        if (mainWindow) {
            mainWindow.loadURL(deeplinkingUrl)
            if (mainWindow.isMinimized()) mainWindow.restore()
            mainWindow.focus()
        }
    })
}

function createWindow() {
    mainWindow = new BrowserWindow({ 
        width: 800, 
        height: 600, 
        icon: path.join(__dirname, '/icon.png'), 
    })

    // 冷启动解析Win10参数
    if (process.platform === 'win32') {
        const argUrl = process.argv.find(arg => arg.startsWith('example://'))
        if (argUrl) {
            deeplinkingUrl = argUrl.replace("example", "https")
        }
    }

    logEverywhere("#createWindow", deeplinkingUrl)
    mainWindow.loadURL(deeplinkingUrl)
    mainWindow.webContents.openDevTools()

    mainWindow.on('closed', function () {
        mainWindow = null
    })
}

app.on('ready', () => {
    app.setAsDefaultProtocolClient('example')
    createWindow()
})

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

app.on('activate', function () {
    if (mainWindow === null) {
        createWindow()
    }
})

// macOS深度链接处理
app.on('open-url', function (event, url) {
    event.preventDefault()
    deeplinkingUrl = url.replace("example", "https")
    logEverywhere("#open-url", deeplinkingUrl)
    
    if (mainWindow) {
        mainWindow.loadURL(deeplinkingUrl)
        mainWindow.focus()
    }
})

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

额外优化说明

  • 使用find方法精准匹配深度链接参数,避免依赖固定索引导致的错误
  • 将协议注册移至ready事件内,确保Electron初始化完成后再关联系统协议
  • 统一Windows和macOS的链接替换逻辑,保持代码一致性

内容的提问来源于stack exchange,提问作者Dominika Jadowska

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:55:15