Skip to content Skip to sidebar Skip to footer

Greasemonkey Script To Replace Jquery Plugin Function?

I'm trying to write a Greasemonkey script for Firefox to change the behavior of some pages when they detect a window out of focus. I want the page to continue to function as if it

Solution 1:

I don't know if there is a way to do this without replacing the entire function

Yes, there is. See code below.

Also, I'm not sure if I need to add jQuery to my Greasemonkey script or not, since the page already loads it.

You don't have to. See code below.

The html (same as yours - i just added "fired from inline JS" to innerHTML)

<html lang="en">
.
.
function focusOn () {
   document.getElementById('console').innerHTML += '<div>Focus event handler fired from inline JS.</div>';
};
function focusOff () {
   document.getElementById('console').innerHTML += '<div>Blur event handler fired from inline JS.</div>';
};
.
.
</html>

The GM script

// ==UserScript==// @name           TESTE 2// @namespace      TESTE 2// @description    TESTE 2// @include        file:///*// ==/UserScript==


$ = unsafeWindow.$
$(window).off('blur')

functionfocusOff () {
    document.getElementById('console').innerHTML += '<div>Blur event handler fired from Greasemonkey.</div>';
};

$(window).on('blur', focusOff);

The result

result image

And here, why you should not use unsafeWindow: http://wiki.greasespot.net/UnsafeWindow

EDIT

About your question in the comments

Thank you. That's exactly what I was looking for. I will look into using a safe wrapper instead of unsafeWindow. If you don't mind a follow-up question: Slide Panel demo - I tried to adapt similar code to override the click function, with $('.btn-slide').unbind('click') then adding a new function, but it doesn't quite work. The event is logged, but the button still activates. $('.btn-slide').bind('click', function(event) { event.preventDefault(); console.log('button clicked'); }); How can I prevent the slide animation?

The new code:

// ==UserScript==// @name           TESTE 3// @namespace      TESTE 3// @description    TESTE 3// @include        http://www.webdesignerwall.com/demo/jquery/*// ==/UserScript==

$ = unsafeWindow.$

$(window).load(function(){
    $('.btn-slide')
        .unbind('click')
        .bind('click', function(event) {
            alert('my function here')
            console.log('button clicked')
        })
});

Post a Comment for "Greasemonkey Script To Replace Jquery Plugin Function?"