首次在SDK中获取鼠标坐标右键单击

时间:2014-06-25 13:33:43

标签: javascript firefox-addon firefox-addon-sdk

对于SDK插件,我试图在右键单击时检测到最近的页面锚点(described here)。

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

  1. 此事件没有可从中获取鼠标坐标的事件对象
  2. 由于某些原因,添加正常的window.addEventListener('click',...侦听器实际上并没有在第一个菜单选择的SDK内容脚本中被触发(但它随后会触发)。
  3. 这是我的解决方法,但我想触发当前的精确坐标,而不是节点的坐标:

    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
    

    有没有其他方法可以获得实际的鼠标坐标?

1 个答案:

答案 0 :(得分:1)

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

此页面包含大量示例:https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Standard_OS_Libraries

它必须在每个操作系统的基础上完成,但因为它使用JS-Ctypes。如果你担心表演,就不要害怕。您可以通过ChromeWorker s

异步使用代码
相关问题