views:

466

answers:

1

My userscript's task is fairly simple -to listen to XHR requests via an XHR bridge sort of functionality, manipulate the data received, and return it back. Everything happening transparently, of course.

I came across this reply (http://stackoverflow.com/questions/629671/how-can-i-intercept-xmlhttprequests-from-a-greasemonkey-script/629782#629782) on SOF which provides the following code-snipped:

(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener("readystatechange", function() {
console.log(this.readyState);
}, false);
open.call(this, method, url, async, user, pass); };
})(XMLHttpRequest.prototype.open);

The code works as expected when pushed via FireBug. It, however, doesn't do anything in a GreaseMonkey script. On further searches, I came across another reply(ttp://stackoverflow.com/questions/688884/how-intercept-xhr-with-greasemonkey/688909#688909) which mentions that:

Greasmonkey and Firefox 3.x doesn't currently support the "prototype"-property. Please see the following ticket for more information: http://greasemonkey.devjavu.com/ticket/164

I have two basic queries:
1. Does this apply to Fx v3.5.x as well? (Note: The ticket link on devjavu.com isn't accessible)
2. What does signature (function(){})() mean in Javascript. (Kindly bear, am not an expert in advanced JS).

+1  A: 

function(){} is anonymous function (lambda), adding () after simply executes it on the fly.
It's very handy to keep different (unique) scope for chunk of code.

(function(){
  var localVariable = 'temp';
})();
console.log(localVariable); // outputs undefined
Tomasz Durka
Strictly speaking *lambda* means using a function as an argument in a call to another function, but +1 anyways :-)
Ian Oxley
Tomasz, Thanks for the clarification.Any clue on the first question?
Jumper