在SDK中获取鼠标坐标首次右键单击 [英] Getting mouse coordinates in SDK first-time right-click
问题描述
由于JavaScript没有先创建鼠标事件侦听器,显然无法查询鼠标坐标,因为我不想要一个昂贵的鼠标悬停在每一个页面上运行的时间! (和轮询重建鼠标监听器是丑陋的,不完全准确无论如何),我希望我可以从点击事件获得鼠标坐标。不幸的是,虽然 self.on('click'
does fire:
-
- 添加一个正常的
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){/ /由于这不是第一次显示,我们在节点上(这是一个元素,而不是文本节点),虽然我们真的想要真正的clientX和clientY whe不是这个模拟基于元素的位置是
node.dispatchEvent(new MouseEvent('click',{
button:2
}));
}
//使用document.caretPositionFromPoint和x和y来确定文本的位置
有没有其他的方式来获得鼠标的实际坐标?
听众。
这个页面有一堆例子: https://developer.mozilla.org/zh-CN/docs/Mozilla/js-ctypes/Standard_OS_Libraries
<它必须在每个操作系统的基础上完成,虽然它使用JS-Ctypes。如果你担心表演,不要害怕。您可以通过 ChromeWorker
s
异步使用代码
For an SDK add-on, I am trying to detect, on right-click, the closest page anchor (as described here).
Since JavaScript apparently has no way to query the mouse coordinates without first creating a mouse event listener, and since I don't want an expensive mouseover listener running all of the time on every single page! (and polling to rebuild a mouse listener is ugly and not fully accurate anyways), I was hoping I could get the mouse coordinates from a click event. Unfortunately, although self.on('click'
does fire:
- This event is without an event object from which mouse coordinates could be obtained
- Adding a normal
window.addEventListener('click',...
listener doesn't actually get fired in the SDK content script with the first menu selection for some reason (it does fire subsequently, however).
Here is my workaround, but I want to fire on the current exact coordinates, not the coordinates of the node:
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
Is there any other way to get the actual mouse coordinates?
You can actually query the mouse coordinates without a mouse listener.
This page has a bunch of examples: https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Standard_OS_Libraries
It has to be done on per OS basis though as it uses JS-Ctypes. If you're worried about performance don't fear at all. You can use the code asynchronously via ChromeWorker
s
这篇关于在SDK中获取鼠标坐标首次右键单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!