'How to get over a blocked event?
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?
Solution 1:[1]
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>
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mplungjan |
| Solution 2 | Thomas |
