tags:

views:

33

answers:

1

I wanted to write a very simple greasemonkey script because I hate the "are you sure?" javascript confirmation on a site I use a lot. I figured it would be pretty easy since I do use javascript a lot, this is my first greasemonkey script though. I'm just going to use it for personal use, not going to publish it or anything. After some googling I found http://wiki.greasespot.net/UnsafeWindow explaining what it seems that I want to do.

The source code for the page I want is like this

var message = "Are you sure?";
function confirmIt(message) {
    var result = confirm(message);
    return result;
}

I want to replace confirmIt(message) with just return true;

So I made a script

var oldFunction = unsafeWindow.confirmIt(message);
    unsafeWindow.confirmIt(message) = function() {
    return true;
};

I get the error "message is not defined."

I'm not sure if I'm going about this right (I'm thinking not), but I'd appreciate some guidance from someone with more experience in greasemonkey, about how to replace a javascript function on a page.

+3  A: 

You need to think of unsafeWindow.confirmIt as a variable in addition to a function (which it is). So, the way to do what you're attempting in your code would be:

var oldFunction = unsafeWindow.confirmIt;

unsafeWindow.confirmIt = function(message) {
    return true;
};

Try that.

umop
Worked! Thank you. Curious though, I don't care about accessing the variable message in my function, but what if I did want to, how would I do that if I don't seem to have access to the parameters?
Sam I Am
You do have access to the parameters. Your function is defined as taking a parameter which it refers to as "message". You could say unsafeWindow.confirmIt = function(message) {alert(message); return true;}; and it would work exactly as you'd expect.
Hellion