【问题标题】:electron url scheme "open-url" event电子 url 方案“open-url”事件
【发布时间】:2016-11-22 08:49:39
【问题描述】:

我在index.js 中做了以下操作;

electron.remote.app.on("open-url", function(event, url) {
  console.log("Open URL: " + url);
});

这会在 Mac OS 中触发,但不会在 Windows 中触发。在 Windows 中是否有其他事件或不同的方式来执行它?

【问题讨论】:

    标签: node.js electron nwjs


    【解决方案1】:

    From mine similar Q/A at SO: 这是关于使用 Electron 为两个平台(macOS/win32)打开应用程序并通过深度链接传递参数。


    macOS/win32 平台中具有深度链接功能的最小电子项目(单实例应用程序)'electron-deep-linking-mac-win' on GitHub.


    package.json:

    {
      "name": "electron-deeplinking-macos-win32",
      "version": "0.0.1",
      "description": "Minimal Electron application with deep inking (macOS/win32)",
      "main": "main.js",
      "scripts": {
        "start": "electron .",
        "pack": "build --dir",
        "dist": "build"
      },
      "repository": "https://github.com/oikonomopo/electron-deep-linking-osx",
      "author": "oikonomopo",
      "license": "CC0-1.0",
      "devDependencies": {
        "electron": "1.6.6",
        "electron-builder": "17.1.2"
      },
      "build": {
        "appId": "oikonomopo.electron-deeplinking-macos-win32",
        "protocols": {
          "name": "electron-deep-linking",
          "schemes": ["myapp"]
        },
        "mac": {
          "category": "public.app-category.Reference"
        },
        "win": {
        }
      }
    }
    

    main.js:

    const {app, BrowserWindow} = require('electron')
    // Module with utilities for working with file and directory paths.
    const path = require('path')
    // Module with utilities for URL resolution and parsing.
    const url = require('url')
    
    // Keep a global reference of the window object, if you don't, the window will
    // be closed automatically when the JavaScript object is garbage collected.
    let mainWindow
    
    // Deep linked url
    let deeplinkingUrl
    
    // Force Single Instance Application
    const shouldQuit = app.makeSingleInstance((argv, workingDirectory) => {
      // Someone tried to run a second instance, we should focus our window.
    
      // Protocol handler for win32
      // argv: An array of the second instance’s (command line / deep linked) arguments
      if (process.platform == 'win32') {
        // Keep only command line / deep linked arguments
        deeplinkingUrl = argv.slice(1)
      }
      logEverywhere("app.makeSingleInstance# " + deeplinkingUrl)
    
      if (mainWindow) {
        if (mainWindow.isMinimized()) mainWindow.restore()
            mainWindow.focus()
      }
    })
    if (shouldQuit) {
        app.quit()
        return
    }
    
    function createWindow () {
      // Create the browser window.
      mainWindow = new BrowserWindow({width: 800, height: 600})
    
      // and load the index.html of the app.
      mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true
      }))
    
      // Open the DevTools.
      mainWindow.webContents.openDevTools()
    
      // Protocol handler for win32
      if (process.platform == 'win32') {
        // Keep only command line / deep linked arguments
        deeplinkingUrl = process.argv.slice(1)
      }
      logEverywhere("createWindow# " + deeplinkingUrl)
    
      // Emitted when the window is closed.
      mainWindow.on('closed', function () {
        // Dereference the window object, usually you would store windows
        // in an array if your app supports multi windows, this is the time
        // when you should delete the corresponding element.
        mainWindow = null
      })
    }
    
    // This method will be called when Electron has finished
    // initialization and is ready to create browser windows.
    // Some APIs can only be used after this event occurs.
    app.on('ready', createWindow)
    
    // Quit when all windows are closed.
    app.on('window-all-closed', function () {
      // On OS X it is common for applications and their menu bar
      // to stay active until the user quits explicitly with Cmd + Q
      if (process.platform !== 'darwin') {
        app.quit()
      }
    })
    
    app.on('activate', function () {
      // On OS X it's common to re-create a window in the app when the
      // dock icon is clicked and there are no other windows open.
      if (mainWindow === null) {
        createWindow()
      }
    })
    
    // Define custom protocol handler. Deep linking works on packaged versions of the application!
    app.setAsDefaultProtocolClient('myapp')
    
    // Protocol handler for osx
    app.on('open-url', function (event, url) {
      event.preventDefault()
      deeplinkingUrl = url
      logEverywhere("open-url# " + deeplinkingUrl)
    })
    
    // Log both at dev console and at running node console instance
    function logEverywhere(s) {
        console.log(s)
        if (mainWindow && mainWindow.webContents) {
            mainWindow.webContents.executeJavaScript(`console.log("${s}")`)
        }
    }
    

    main.js 代码说明:

    • let deeplinkingUrl 我们保留提供的网址。

    • 在 macOS 平台上,这是在 'open-url' event 捕获的,我们将其设置为 deeplinkingUrl = url! (见// Protocol handler for osx

    • 在 win32 平台上,它与其他参数一起保存在 process.argv。要仅获取提供的 URL,deeplinkingUrl = argv.slice(1)。 (见// Protocol handler for win32

    • 正如@Aurélien Nicolas 提到的,在app.makeSingleInstance method 我们检查我们所在的平台并相应地设置deeplinkingUrl!如果我们在win32平台,url位于回调的argv变量,否则在macOS应该已经设置在'open-url'事件! (见// Force Single Instance Application

    【讨论】:

    • 它不适用于我的 Windows。这仅在我们在平台上安装应用程序时才有效吗?或者我可以以某种方式在开发模式下测试它?
    • @MuhammadYawarAli,如果我没记错的话,为了为您的应用程序注册协议处理程序,您需要制作将注册它的安装程序。 (使用 electron-builder 项目打包我们的应用程序)所以是的,您应该首先安装应用程序。
    • 好的,我会尝试构建一个安装程序,但它确实适用于 mac os,即使在开发模式下。
    • 我在 mac 生产版本中测试了深层链接,当应用程序关闭时,我在 open-url 事件中没有得到任何东西,但应用程序在深层链接上打开了。如果应用程序被最小化或没有重点,一切正常。
    【解决方案2】:

    这是一个仅限 mac 的功能。 最接近的选择是app.makeSingleInstance(callback)

    您可以安排以 url 作为参数启动您的应用程序:myApp my-scheme://stuff

    然后callback 会在首先启动的应用程序进程中使用 url 调用。

    【讨论】:

      猜你喜欢
      • 2010-11-03
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 2014-12-31
      • 1970-01-01
      相关资源
      最近更新 更多