2

I am trying to write a chrome extension that disables event listeners for all elements (mouseover, click, ...) I am not trying to rewrite noscript, this is just a setup step that i need.

I have tried $("body *).unbind() and .unbind("mouseover click") and .off() and .off("mouseover click") none worked.

What am i doing wrong?

PS: it would also be fine to just disable all javascript code (coming from the page itself) from running on the page and only allow my extension injected code

WindowsMaker
  • 3,132
  • 7
  • 29
  • 46
  • You can't disable JavaScript event listeners with jQuery event handling...they're 2 totally different things. – Ian Nov 20 '13 at 00:51
  • I believe that the listeners on the page are created using jquery also. But I would like to remove all event listeners – WindowsMaker Nov 20 '13 at 00:53
  • Well it seems to work as you expect...are you sure the events aren't bound **after** your code? And that your code is actually running? http://jsfiddle.net/zArE4/ – Ian Nov 20 '13 at 00:56
  • The standard `.removeEventListener` takes the function listener type (click) and the original function listening to the event. If you don't have both of these available you cannot remove the event set up in pure javascript – megawac Nov 20 '13 at 00:56
  • @Ian : since i am writing an extension i can choose when my code is executed (when i click on the extension icon) so my code should run AFTER. and I am pretty sure it is running :) – WindowsMaker Nov 20 '13 at 00:59
  • @megawac: if that is the case then how do those chrome plugins that disable javascript execution work? I can just disable all javascript codes coming from the page itself and only allow my own code – WindowsMaker Nov 20 '13 at 01:00

3 Answers3

3

This is not a complete example, as I won't do all of your work for you, but might lead you in the right direction:

function preventAll(){
  var dom = document.getElementsByTagName('*');
  var km = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseover', 'mouseout', 'mouseup', 'mouseenter', 'mouseleave', 'keydown', 'keypress', 'keyup'];
  for(var i=0,l=dom.length; i<l; i++){
    for(var n=0,c=km.length; n<c; n++){
      dom[i]['on'+km[n]] = function(e){
        e = e || event;
        e.preventDefault();
        return false;
      }
    }
  }
  var fr = frames;
  for(var i=0,l=fr.length; i<l; i++){
    // cancell frames events here
  }
}
StackSlave
  • 10,613
  • 2
  • 18
  • 35
  • 1
    Thanks! but what is a frame? – WindowsMaker Nov 20 '13 at 01:33
  • 1
    A `frame` is a `frame` or an `iframe`. For the full list of DOM Events, look here http://www.w3schools.com/jsref/dom_obj_event.asp . There are a lot more. Also, when dealing with the `forms` Object, you will want to loop over that and the `elements` Object, which is a property of each `forms` property. Like I said, I'll help, but I'm not going to do this for you. Good Luck! – StackSlave Nov 20 '13 at 01:34
1

It's not possible to intercept or list all previously-chained events in Javascript. However, it does look like Chrome plugins specifically (as opposed to the DOM in general) have an API for manipulating how Javascript works.

Community
  • 1
  • 1
Jeff Sisson
  • 1,616
  • 11
  • 22
1

If you have events made by jQuery as you say, it keeps around the data that you can parse out. You can see how the excellent Visual Events does it, as a more general solution (i.e. outside Chrome extensions).

Amadan
  • 191,408
  • 23
  • 240
  • 301