Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept HTTP request body from chrome extension

Tags:

I'm aware that chrome.webRequest.onBeforeRequest allows a request to be intercepted, analyzed and blocked, but it only allows access to the request headers, and not the request body (as far as i know).

Sample use case: think intercepting form values.

It seems there is a API change proposal here suggesting exactly this.

Is there another way this could be accomplished?

Thanks.

like image 439
p3drosola Avatar asked Jul 21 '12 16:07

p3drosola


2 Answers

This functionality has been added to the API now, see the documentation.

In order to access the body you need to do the following:

chrome.webRequest.onBeforeRequest.addListener(     function(details)     {         console.log(details.requestBody);     },     {urls: ["https://myurlhere.com/*"]},     ['requestBody'] ); 
like image 200
Marmoy Avatar answered Sep 29 '22 11:09

Marmoy


Here is what I did

  1. I used the requestBody to get the post requests body
  2. I used a decoder the parse the body into a string

Here is an example

chrome.webRequest.onBeforeRequest.addListener(     function(details) {         if(details.method == "POST")         // Use this to decode the body of your post             var postedString = decodeURIComponent(String.fromCharCode.apply(null,                                       new Uint8Array(details.requestBody.raw[0].bytes)));            console.log(postedString)      },     {urls: ["<all_urls>"]},     ["blocking", "requestBody"] ); 
like image 22
Taher Elsheikh Avatar answered Sep 29 '22 11:09

Taher Elsheikh