我很想知道是否有办法通过镀铬扩展获得鼠标坐标,然后使用这些坐标来检查该人是否已点击该位置?
解决方法
获取鼠标坐标非常简单,将其放在
content script中:
- document.onmousemove = function(e)
- {
- var x = e.pageX;
- var y = e.pageY;
- // do what you want with x and y
- };
本质上,我们将一个函数分配给整个页面的onmousemove事件,并从事件对象中获取鼠标坐标(e).
但是,我不完全确定你的意思是:
then use these coordinates to check if the person has clicked in that position ?
您想检查用户是否点击了按钮之类的内容吗?在这种情况下,您可以简单地将事件订阅到该按钮(或任何其他元素),如下所示:
- document.getElementById("some_element").onclick = function(e)
- {
- alert("User clicked button!");
- };
记录所有鼠标点击及其位置:
- document.onclick = function(e)
- {
- // e.target,e.srcElement and e.toElement contains the element clicked.
- alert("User clicked a " + e.target.nodeName + " element.");
- };
请注意,事件对象(e)中的鼠标坐标仍然可用.
- document.onclick = function(e)
- {
- var x = e.pageX;
- var y = e.pageY;
- alert("User clicked at position (" + x + "," + y + ")")
- };