【问题标题】:How to Enable Right-Click In NightmareJS?如何在 NightmareJS 中启用右键单击?
【发布时间】:2017-05-05 12:30:15
【问题描述】:

我目前正在使用不同的视口和代理在 NightmareJS 中进行测试。我正在自动访问具有许多复杂脚本和样式的站点。我无法充分复制代理并在单独的测试浏览器中查看,以便该站点为我显示与我使用 Nightmare 访问时相同的内容。因此,我需要能够在 Nightmare 的 Electron 窗口中右键单击元素,以便检查 DOM 中的特定元素。

问题是在电子窗口中右键单击被禁用。我找到了this answer,它描述了在渲染过程中添加代码:

// Importing this adds a right-click menu with 'Inspect Element' option
const remote = require('remote')
const Menu = remote.require('menu')
const MenuItem = remote.require('menu-item')

let rightClickPosition = null

const menu = new Menu()
const menuItem = new MenuItem({
  label: 'Inspect Element',
  click: () => {
    remote.getCurrentWindow().inspectElement(rightClickPosition.x, rightClickPosition.y)
  }
})
menu.append(menuItem)

window.addEventListener('contextmenu', (e) => {
  e.preventDefault()
  rightClickPosition = {x: e.x, y: e.y}
  menu.popup(remote.getCurrentWindow())
}, false)

注意:此代码可能需要按照 referenced answer 中的说明进行更改,但在使用 Nightmare 时我应该在哪里添加此类代码?

主脚本与 Electron 渲染过程脚本 AFAIK 不同。至于那种解决方案,this 可能会更好,但是如何调用呢?

【问题讨论】:

标签: javascript node.js dom electron nightmare


【解决方案1】:

目前没有可用于nightmare右键的api,参考issue:

https://github.com/segmentio/nightmare/issues/346

一种可能的解决方案是直接绕过 nighmarejs 使用 Electron。

示例(未测试):

webpage.sendEvent('contextmenu')

【讨论】:

  • NightmareJS 封装了 Electron,而不是 Phantom。不过我会接受答案。有一个包裹 Phantom 的叉子,但我不会使用它,因为与 Electron 相比,Phantom 非常慢。
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多