This question is almost a duplicate of "How to do an action only when there is no default value in Javascript / jQuery?" , except that as a Greasemonkey script, this is another (complex) option available to you.
From your question, it looks like you want to set a default action for nodes that don't have one, and then abort it by default if some other event handler fires for this event. There is no “retention mechanism” for this, and almost any technical attempt you are trying can cause a page to crash. That's why:
Neither javascript nor the Greasemonkey API provide any mechanism for listing events for a given node. There was an old proposal , but it was never implemented.
However, Firefox add-ons can list events through nsIEventListenerService , so chances are you can write a helper add-on for this part that interacts with Greasemonkey. Details (path) are beyond the scope of this question.
Similarly, there is no mechanism for detecting previous events in the chain of events from an Event Object if event.defaultPrevented not set. (Usually this will not happen.)
Even if you can list event listeners for node X, this will not help deal with listeners on the parent nodes. Modern JS libraries can and sometimes make any old node clickable. And / or they can install a listener, say, a document , which receives all click events, but actually does nothing, except when the original target was a specific node.
A good compromise strategy is to follow this other question and do nothing for nodes with a default action.
To process right-clicks or center-clicks, click event.which .
Thus, you can add the following snippets to your checks:
return function(e){ targt = e.target; sel = window.getSelection(); if (e.which != 1) { return; //-- Abort on anything that is not a left-click. } if (e.defaultPrevented) { return; //-- The one time we know that some other handler fired. } if (isInteractiveElement (targt) ) { // Do nothing for elements that trigger browser actions return; } ...
Where:
function isInteractiveElement (node) { var intElems = [ //-- Must be uppercase "A", "BUTTON", "INPUT", "TEXTAREA", "VIDEO", "MAP", "OBJECT" ]; if (intElems.indexOf (node.nodeName) >= 0) { return true; } else if (node.nodeName === "BODY") { return false; } return isInteractiveElement (node.parentNode); }
source share