Q:

Chrome extension run_at every post request it makes?

I have figured out how to run a script on every page load when it is done explicitly by user but I want to run my script each time it makes a post or get request in its back end on its own to the database or ad server implicitly. [For example on gmail if we keep our eyes on requests (maybe firebug - console - all) we will see that after certain time a POST request is getting fired from the browser on its own. ]
Is there any way I can do that?
Actually I am writing my first extension so it clearly states I don't know much about it.

A:

You should use webRequest module in your extension. After specifing proper permissions in the manifest, for example:
"permissions": [
    "webRequest",
    "*://*/*"
  ],
"background": {
    "scripts": ["background.js"]
 },

you can register in your background page ("background.js" in the example) any required handlers, such as onBeforeRequest, onBeforeSendHeaders, onHeadersReceived, onCompleted, and others. I think the names are self-explaining, but you can consult with abovementioned documentation.
Depending from your requirements, you can define event handlers which prevent requests, modify headers, just read and somehow analyse http-headers.
Example for reading http headers and possibly changing them:
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details)
{
  console.log(details.url);
  if(details.method == 'POST')
  {
    // do some stuff
    for(var i = 0; i < details.requestHeaders.length; ++i)
    {
      // log or change some headers
      // details.requestHeaders[i].name
      // details.requestHeaders[i].value
    }
  }

  return {requestHeaders: details.requestHeaders};
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]);

