node.js - Electron 通过中键单击防止多个实例

标签 node.js electron

我正在尝试创建单个实例 Electron应用。 我正在使用app.makeSingleInstance ,请参阅下面的示例。

中键点击的 SingleInstance 问题:

  1. 如果我第二次点击 app.exe,单实例就可以工作
  2. 如果我在应用程序内的链接上单击鼠标中键,则不起作用

我需要什么:

  1. 使 Electron 应用程序成为单一实例,并确保即使单击中键,它仍保持单一实例。
  2. 我不想在我的应用程序中强制禁用中键点击,因为在某些地方,我有一个在非链接项目上使用它们的用例

如何重现:

  1. 使用存储库:https://github.com/electron/electron-quick-start
  2. 用我的 index.htmlmain.js 替换现有的,见下文
  3. npm install 然后npm start

index.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>

ma​​in.js

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()
  }
})

enter image description here

最佳答案

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

如果您不想将这些事件重定向到默认浏览器,则可以在主窗口的 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);
}

但是您也可以选择将中键单击事​​件重定向到标准浏览器,即通过 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); }

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

关于node.js - Electron 通过中键单击防止多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49164924/

相关文章:

javascript - 从数组数组中获取唯一值

sql-server - 无法使用node js连接到mysql windows身份验证

electron - 在 Gitlab CI 上运行测试时 ChromeDriver 出现问题

electron - Electron Web蓝牙API requestDevice()错误

node.js - 使用 serverless-mocha-plugin 测试动态端点

node.js - 验证 hapi server.log 正在使用预期消息进行调用

node.js - Node : Check if a file is locked without locking the file

reactjs - 使用按钮在Main和Renderer之间进行通信

node.js - 为什么 Electron 快速启动应用程序无法在我的 Ubuntu 实例上正确启动?

reactjs - 编写带有 Electron react 样板的 Electron 应用构建的自动化测试用例