-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 How can I intercept XMLHttpRequests from a Greasemonkey script? on SO -- which provides the following code-snippet:

(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: how intercept xhr with greasemonkey -- 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).
Community
  • 1
  • 1
Jumper
  • 129
  • 3
  • 9
  • Further to Tomasz's answer (to Q#2), here's a pointer for others: http://www.hunlock.com/blogs/Functional_Javascript#quickIDX5 – Jumper Dec 08 '09 at 17:37

1 Answers1

0

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
  • 586
  • 2
  • 9