javascript - SDK中首次右键获取鼠标坐标

标签 javascript firefox-addon firefox-addon-sdk

对于 SDK 附加组件,我尝试右键单击时检测最近的页面 anchor (如 described here )。

因为 JavaScript 显然无法在不首先创建鼠标事件监听器的情况下查询鼠标坐标,并且因为我不希望在每个页面上一直运行昂贵的鼠标悬停监听器! (并且轮询重建鼠标监听器是丑陋的并且不完全准确),我希望我可以从单击事件中获取鼠标坐标。不幸的是,尽管 self.on('click' 确实触发了:

  1. 此事件没有可以获取鼠标坐标的事件对象
  2. 由于某种原因,添加普通的 window.addEventListener('click',... 监听器实际上不会在 SDK 内容脚本中在第一个菜单选择时触发(它随后会触发,然而)。

这是我的解决方法,但我想在当前的精确坐标上触发,而不是节点的坐标:

var x, y;
window.addEventListener('click', function (e) {
    if (e.button === 2) {
        x = e.clientX;
        y = e.clientY;
    }
}, true);

self.on('click', function (node) {
    if (!x) { // Since this is not showing the first time, we fire on the node (which is specifically an element, not a text node), though we really want the real clientX and clientY where the mouse is, not this simulation based on where the element is
        node.dispatchEvent(new MouseEvent('click', {
            button: 2
        }));
    }
    // Use document.caretPositionFromPoint with x and y to determine location of text

还有其他方法可以获取实际的鼠标坐标吗?

最佳答案

您实际上可以在没有鼠标监听器的情况下查询鼠标坐标。

此页面有很多示例:https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Standard_OS_Libraries

尽管它使用 JS-Ctypes,但它必须在每个操作系统的基础上完成。如果您担心性能,请完全不用担心。您可以通过 ChromeWorkers

异步使用代码

关于javascript - SDK中首次右键获取鼠标坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24410200/

相关文章:

javascript - 在 Firefox 扩展中使用 XMLHttpRequest 获取二进制数据

javascript - JavaScript 中的语法错误 : missing ; before statement

ajax - 在 Firefox Addon 页面脚本中加载 JSON 文件,包内的所有内容

javascript - 如何观看 nsIHandlerService.store(handlerInfo)

javascript - 在 window.open() 上运行 Firefox 扩展

firefox - 在附加组件和内容中使用相同的文件

javascript - 组件内部方法的调用由根来回答

javascript - 从 Chrome 扩展发送 POST 请求

php - PHP 游戏中的循环迭代

javascript - 如何在 Wordpress 主题中使用 Webpack 包含外部库