问题
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'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.
回答1:
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.
来源:https://stackoverflow.com/questions/3340337/how-to-write-a-greasemonkey-script-to-remove-a-confim-dialog