I'm trying to disable all click events on page from my Greasemonkey script using JQuery like this:
$("*").unbind("click");
$("[onclick]").removeAttr("onclick");
Jquery is loaded into greasemonkey script using
// @require http://code.jquery.com/jquery-1.10.1.min.js
It works when I run it manually from Firefox OR Firebug Console.
But it doesn't work from Greasemonkey!
What is the problem?
How Can I do that from greasemonkey?
It works when I run it manually from Firefox OR Firebug Console.
But it doesn't work from Greasemonkey!
Since Greasemonkey usually runs your script in a wrapper function, the jQuery the page is using is not the same as the jQuery in your script. Either that or you're overwriting the jQuery function with a different one.
Because of that, .unbind()
in your script cannot remove the listeners on the page because they were added with a different jQuery function.
It works when you run it in the Firebug console because the Firebug console will use the jQuery function from the page, not your Greasemonkey script.
To solve this, just use the jQuery function from the page to unbind the listeners.
We can use unsafeWindow
to access the page's window object.
unsafeWindow.$("*")
.unbind("click")
.off("click")
.removeAttr("onclick");
Note: this will only remove event listeners added by jQuery.
To remove listeners added by .addEventListener()
, you will have to hi-jack that method to listen for calls to it, and then filter them so click events aren't added.