WRT building a Firefox Add-on.
Is it possible to get the element under the mouse via some XPCOM or javascript method? (non-js-ctypes please as that requires OS specificity)
I want to detect what is under the mouse when user presses Ctrl + Shift + M.
Right now I'm adding a mouseover
listener to the document when the user presses this hotkey, so I can get the element under the mouse when he moves it, but not the element that was under the mouse exactly when he pressed the hotkey bination.
WRT building a Firefox Add-on.
Is it possible to get the element under the mouse via some XPCOM or javascript method? (non-js-ctypes please as that requires OS specificity)
I want to detect what is under the mouse when user presses Ctrl + Shift + M.
Right now I'm adding a mouseover
listener to the document when the user presses this hotkey, so I can get the element under the mouse when he moves it, but not the element that was under the mouse exactly when he pressed the hotkey bination.
-
i would suggest having a listener on the
mousemove
event to track the mouse. then using the mouse's coordinates, find the element beneath it. this is annoying but possible. see more here: stackoverflow./questions/7790725/… – user428517 Commented Jul 2, 2014 at 18:30 -
The issue with
elementFromPoint
is I have to addmousemove
and the element will not be available until user makes movement of mouse after pressing hotkey. I basically use the 2nd method in this solution: stackoverflow./a/4711224/3791822 – Blagoh Commented Jul 2, 2014 at 18:34 -
1
listen on
mousemove
ALL the time, storing the position somewhere. then when the key bination is pressed, use the last recorded mouse position. – user428517 Commented Jul 2, 2014 at 18:36 -
Oh yes that's an idea, I had that too but forgot to mention it so up vote for that. A persistent
mousemove
is not so good for performance especially from the add-on scope so I was hoping to avoid that please. – Blagoh Commented Jul 2, 2014 at 18:43 - then you're probably out of luck. are you sure it hurts performance that much? i've done things like this before with little issue. a simple handler that updates xy values in an array is not very taxing. that's all it would need to do. – user428517 Commented Jul 2, 2014 at 18:47
1 Answer
Reset to default 15I just looked through the source for code that gets (or stores and makes available) the cursor position. I didn't find anything one could use (from Javascript, XPCOM or not). I might have missed something... MXR is your friend.
However, if you want to avoid mousemove
(and this is a good idea in general), you can just look for the innermost hovered element, e.g. like so.
function getInnermostHovered() {
var n = document.querySelector(":hover");
var nn;
while (n) {
nn = n;
n = nn.querySelector(":hover");
}
return nn;
}
(fiddle demoing the principle)
While this is what I'd consider a hack, it seems to work good enough most of the time, but will fail if the element has mouse events disabled via pointer-events
. There could be other issues I didn't think of...
Of course, this can return nothing when the document has no hovered element (e.g. the mouse is not actually within the document).