开发者

How do I unbind jquery event handlers in greasemonkey?

开发者 https://www.devze.com 2023-02-19 15:24 出处:网络
$(\"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
$("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开发者_如何学Go 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')});


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');
});


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


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");
});
0

精彩评论

暂无评论...
验证码 换一张
取 消