2
$("p").click(function(){alert('clicked')});

$("p").unbind('click');

Within greasemonkey the click event does not unbind. I believe this is caused by greasemonkey's security model wrapping the associated event object from the first line in XPCNativeWrapper, causing the second line to be unable to "find" it. However, I can't seem to find a workaround. Any suggestions?

UPDATE: Something like the following does work in greasemonkey. So I'm still thinking it's an XPCNativeWrapper issue that I can't find a way to resolve.

$("p").click(function(){alert('clicked'); $(this).unbind('click')});
James
  • 21
  • 3

3 Answers3

1

I think jQuery click events are added after calling unbind() function. This is why the following code works.

$("p").click(function(){alert('clicked'); $(this).unbind('click')});

Some time elapses in your function until you click the alert button and unbind() function works after you click OK.

Here is a solution I have tested:

function removeClick() {
    $("p").unbind('click');
}

var initTimeout = setTimeout(function() { removeClick(); }, 1000);

If you don't like using timeouts or intervals in a greasemonkey script, you can add mouseover event to your p elements to remove the click.

$("p").mouseover(function(){
    $(this).unbind('click');
});
sevenkul
  • 966
  • 1
  • 8
  • 14
  • Please note that the mouseover method is not safe when the form is submitted using the keyboard. – Rapti Jan 22 '15 at 23:22
0

Do it this way:

window.addEventListener('load', function ()
{
    jQuery = unsafeWindow['jQuery'];
    jQuery(document).unbind("contextmenu");
    jQuery(document).unbind("keypress");
    jQuery(document).unbind("selectstart");
    jQuery(document).unbind("mousedown");
});
Xdg
  • 1,735
  • 2
  • 27
  • 42
0

How are you loading the JQuery? May not be loaded properly: http://joanpiedra.com/jquery/greasemonkey/

EDIT Or edit the JQuery code as described here: http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error

Sycren
  • 539
  • 2
  • 5
  • 15
  • I'm embedding it directly into my greasemonkey script. – James Mar 25 '11 at 19:06
  • what is the version of jquery ? you should read this: http://wiki.greasespot.net/Third-Party_Libraries#jQuery – w35l3y Mar 26 '11 at 23:04
  • 1.5.1. Although I did try 1.3.2 at one point while troubleshooting this and got the same results. – James Mar 27 '11 at 15:55
  • The solution referenced by @Sycren does work, but I'm curious if there's a better way, i.e. one that doesn't require using unsafeWindow. Especially since [greasespot](http://wiki.greasespot.net/UnsafeWindow) clearly states: **USE OF UNSAFEWINDOW IS INSECURE, AND IT SHOULD BE AVOIDED WHENEVER POSSIBLE.** – James Mar 28 '11 at 19:15
  • Out of interest, what are you writing a script for? – Sycren Mar 29 '11 at 17:39
  • Perhaps this may be more of what you want: http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error However you would then need to host the edited jquery code rather than use the one hosted by google. – Sycren Mar 29 '11 at 17:44