通过Chrome扩展获取鼠标坐标 [英] obtain mouse coordinates through chrome extension
问题描述
我很想知道是否有办法通过chrome扩展获取鼠标坐标,然后使用这些坐标来检查该人是否已经在该位置点击了?
获取鼠标坐标非常简单,请将其放入内容脚本:
$ p $ document.onmousemove = function(e)
{
var x = e.pageX;
var y = e.pageY;
//用x和y
来做你想要的;
实质上,我们将函数分配给 onmousemove
整个页面的事件,并将鼠标坐标从事件对象中取出( e
)。
然而,我并不完全确定你的意思是什么:
然后使用这些坐标来检查该人是否在该位置点击?
您是否想要检查用户是否点击了某个按钮?在这种情况下,您可以简单地向这个按钮(或任何其他元素)订阅事件,如下所示:
document.getElementById( onclick = function(e)
{
alert(User clicked button!);
};
记录所有鼠标点击及其位置:
document.onclick = function(e)
{
// e.target,e.srcElement和e.toElement包含单击的元素。
alert(用户点击了+ e.target.nodeName +元素。);
};
请注意,鼠标坐标在事件对象中仍然可用( e
)。
如果您在用户点击任意位置时需要坐标,则可以这样做:
document.onclick = function(e)
{
var x = e.pageX;
var y = e.pageY;
alert(用户点击位置(+ x +,+ y +))
};
I am curious to know if there is a way to get the mouse coordinates through a chrome extension and then use these coordinates to check if the person has clicked in that position ?
Getting the mouse coordinates is very simple, put this in a content script:
document.onmousemove = function(e)
{
var x = e.pageX;
var y = e.pageY;
// do what you want with x and y
};
Essentially, we are assigning a function to the onmousemove
event of the entire page, and getting the mouse coordinates out of the event object (e
).
However, I'm not entirely sure what you mean by this:
then use these coordinates to check if the person has clicked in that position ?
Do you want to check if a user clicks something like a button? In that case you can simply subscribe an event to that button (or any other element) like this:
document.getElementById("some_element").onclick = function(e)
{
alert("User clicked button!");
};
To record all mouse clicks and where they are:
document.onclick = function(e)
{
// e.target, e.srcElement and e.toElement contains the element clicked.
alert("User clicked a " + e.target.nodeName + " element.");
};
Note that the mouse coordinates are still available in the event object (e
).
If you need the coordinates when a user clicks an arbitrary location, this does the trick:
document.onclick = function(e)
{
var x = e.pageX;
var y = e.pageY;
alert("User clicked at position (" + x + "," + y + ")")
};
这篇关于通过Chrome扩展获取鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!