0

I'm developing an JavaScript app which needs to trigger mousemove event, and I am using tampermonkey to "embed" it on websites to test if it works on them. On one website, the mousemove event is simply blocked (I don't know why) and even console.log within this event doesn't show. It seems like this event has never triggered (the website ignores).

Is it possible to "override" events and block them? If so, how can I override this action and enable my specific mousemove event?

This script won't work:

document.addEventListener("mousemove", function(e) {
    e = e || window.event;
    e.stopPropagation();
    console.log("[HELLO] Is it moving?!");
}, false);

(the result will be... nothing. this won't be logged)

Update They set window.onmousemove as null. Is there any way to revert this?

  • also if you're running your code on random sites it is possible that for whatever reason the js on the site is removing event handlers on an interval or something causing your mouse move not to fire (first answer is much more likely) – bturner1273 Apr 12 '22 at 13:11

2 Answers2

0

If some other event handler called stopPropagation() on the event, it won't be propagated up to the document level during the "bubble" phase.

However, you can make your event handler be called sooner, by running it in the "capture" phase. Simply change the third argument from false to true. This will solve most issues, except if some other event handler on the document was added before yours, and calls stopImmediatePropagation on the event. But this is rare.

See Bubbling and capturing explained for more background information.

Thomas
  • 174,939
  • 50
  • 355
  • 478
0

If you can run your code before theirs, you can save the function

window.onmousemove = function()  {
  console.log("moving",new Date())
};
window.myMove = window.onmousemove;
document.getElementById("x").addEventListener("click",function() {
  window.onmousemove=null
})

document.getElementById("y").addEventListener("click",function() {
  window.onmousemove=myMove;
})
<button type="button" id="x">Turn off mousemove</button>
<button type="button" id="y">Turn mousemove back on</button>
mplungjan
  • 169,008
  • 28
  • 173
  • 236