对于 SDK 附加组件,我尝试右键单击时检测最近的页面 anchor (如 described here )。
因为 JavaScript 显然无法在不首先创建鼠标事件监听器的情况下查询鼠标坐标,并且因为我不希望在每个页面上一直运行昂贵的鼠标悬停监听器! (并且轮询重建鼠标监听器是丑陋的并且不完全准确),我希望我可以从单击事件中获取鼠标坐标。不幸的是,尽管 self.on('click'
确实触发了:
- 此事件没有可以获取鼠标坐标的事件对象
- 由于某种原因,添加普通的
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,但它必须在每个操作系统的基础上完成。如果您担心性能,请完全不用担心。您可以通过 ChromeWorker
s
关于javascript - SDK中首次右键获取鼠标坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24410200/