电子通过中键防止多个实例

dja*_*ngo 2 node.js electron

我正在制作单实例电子应用程序。我正在使用app.makeSingleInstance,请参阅下面的示例。

中键的 SingleInstance 问题:

  1. 如果我第二次单击 app.exe,单实例工作
  2. 如果我在我的应用程序中点击一个链接,它就不起作用

我需要的:

  1. 使电子应用程序单实例并确保即使单击中键它仍然是单实例。
  2. 我不想像在某些地方那样在我的应用程序中强制禁用中间单击,我在非链接项目上有一个用例

如何重现:

  1. 使用回购:https : //github.com/electron/electron-quick-start
  2. 用我的index.html和替换现有的main.js,见下文
  3. npm install 进而 npm start

索引.html:

<!DOCTYPE html>
<html>
  <head><meta charset="UTF-8"><title>Hello World!</title></head>
  <body>
    <h1>app.makeSingleInstance()</h1>
    <a href="$">Middle Click on it</a>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

主文件

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
let mainWindow
const isSecondInstance = app.makeSingleInstance((commandLine, workingDirectory) => {
  if (myWindow) {
    if (myWindow.isMinimized()) myWindow.restore()
    myWindow.focus()
  }
})
if (isSecondInstance) {
  app.quit()
}
function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))
  mainWindow.on('closed', function () {
    mainWindow = null
  })
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Ale*_*ner 5

单击中键不会创建应用程序的新实例,而是创建BrowserWindow. 您可以a使用该auxclick事件禁用对(实际上是所有)元素的中间点击。

如果您不想将这些事件重定向到默认浏览器,您可以在主窗口的 HTML 中放置以下 JavaScript 以禁用链接元素上的中间点击:

// The following function will catch all non-left (middle and right) clicks
function handleNonLeftClick (e) {
    // e.button will be 1 for the middle mouse button.
    if (e.button === 1) {
        // Check if it is a link (a) element; if so, prevent the execution.
        if (e.target.tagName.toLowerCase() === "a") {
            e.preventDefault();
        }
    }
}

window.onload = () => {
    // Attach the listener to the whole document.
    document.addEventListener("auxclick", handleNonLeftClick);
}
Run Code Online (Sandbox Code Playgroud)

但是你也可以选择将中间点击事件重定向到你的标准浏览器,即通过 Electron 的shell模块:

// Require Electron's "shell" module
const { shell } = require("electron");

function handleNonLeftClick (e) {
    // e.button will be 1 for the middle mouse button.
    if (e.button === 1) {
        // Check if it is a link (a) element; if so, prevent the execution.
        if (e.target.tagName.toLowerCase() === "a") {
            // Prevent the default action to fire...
            e.preventDefault();

            // ...and let the OS handle the URL.
            shell.openExternal(e.target.href);
        }
    }
}

// Also attach the listener this time:
window.onload = () => { document.addEventListener("auxclick", handleNonLeftClick); }
Run Code Online (Sandbox Code Playgroud)

if (e.button === 1)如果您还想阻止对a元素的右键单击,则可以删除。