Consider this simple js file called xhr.js:

-----------------------------------
(function(){

var _XMLHttpRequest = XMLHttpRequest;
window.XMLHttpRequest = function()
{
  console.log("XHR!");
  return new _XMLHttpRequest();
};

})();
-----------------------------------

The idea is that in your content script you inject/load this script file at
the page so "window.XMLHttpRequest" will point to the page's XMLHttpRequest
object and not your extention's sandboxed XMLHttpRequest object.

The complicated part is the communication between the page context and the
sandboxed contexts (the contexts of content scripts and/or background
pages).

Firebug Lite makes this easier because the whole application is running
inside the page, so there are only few cases in which we actually need to
communicate with the sandbox contexts, which happens when we're listening
for context menu (right-click menu) actions.

So, you need to:

1) create a content script which will load on all pages
2) inside the content script, you need to inject/load the xhr.js file inside
the page's context
3) the xhr.js file listens for XHR calls and logs them to the console

You can see how Firebug Lite loads itself into the page here:
http://code.google.com/p/fbug/source/browse/lite/branches/firebug1.4/build/chrome-extension/contentScript.js#76

You can see part of the code that handles the communication with sandboxed
contexts here:
http://code.google.com/p/fbug/source/browse/lite/branches/firebug1.4/build/chrome-extension/googleChrome.js

The other part of the communication to handle context-menus is located here:
http://code.google.com/p/fbug/source/browse/lite/branches/firebug1.4/build/chrome-extension/background.html#206

An example of the communication from Firebug Lite to Content Script is
located here:
http://code.google.com/p/fbug/source/browse/lite/branches/firebug1.4/build/chrome-extension/firebug-lite.js#11017

regards,

Pedro Simonetti.

2011/10/10 aslan <[email protected]>

> Thank You for your attention but i'm new to javascript and maybe i
> don't understand sth.
>
> > > var XMLHttpRequestWrapper = function(activeXObject){
> > >    console.log("xhr!!!!!!!!!!!");
> > >    new XMLHttpRequest();
> > > }
> >
> > This code is incomplete and won't work properly because the wrapper does
> not
> > have the open(), send() and other methods. But I suppose you already know
> > that and this is just a sample code to demonstrate what you are trying to
> > do.
>
> I now it's incomplete but I want to do a simple thing here for the
> beggining- I want to console.log("xhr"); on every xmlhttprequest on
> page (even without finishing this request)
>
> > > var _XMLHttpRequest = XMLHttpRequest;
> > > window.XMLHttpRequest = function()
> > >    {
> > >        return new XMLHttpRequestWrapper();
> > >    };
> > > -------------------------------
> > > it only fires on my own xhrs not on those from website.
> >
> > Chrome extension runs in a sandboxed environment, which means that if you
> > execute the wrapper in the sandbox only XHR calls made inside the sandbox
> > will be tracked. That's the behavior you are seeing.
> >
> > If you want to listen for XHR calls made by the page, you need to inject
> a
> > script at the page and then use a "Content Script" to communicate with
> it:
> >
> > http://code.google.com/chrome/extensions/content_scripts.html#host-pa...
>
> I've red this but they communicate with this page (http://foo.com/
> example.html) involving in it's code (maybe I misunderstood). And how
> can I communicate with all pages outside sandbox only with content
> sript and background page? Firebug lite for chrome does that for every
> page. It monitors xhrs on everywhere! I still don't understand how.
> Please help me.
>
> Luke
>
> --
> You received this message because you are subscribed to the Google Groups
> "Firebug" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/firebug?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Firebug" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/firebug?hl=en.

Reply via email to