0

I am trying to get the formData of a POST request with a chrome extension. I am using the following manifest.json file and background.js file. However, when I console.log the requestBody I cannot see the formData which was sent by the website. How can I get the form elements?

manifest.json

{
  "manifest_version": 2,
  "name": "Form Data",
  "description": "Looks at some form data.",
  "version": "0.9",
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "webRequest",
    "webRequestBlocking",
    "<all_urls>"
  ]
}
background.js

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        if(details.method == "POST")
            console.log(JSON.stringify(details));
    },
    {urls: ["<all_urls>"]},
    ["requestBody"]
);

Example Response:

{"frameId":0,"initiator":"https://example.com","method":"POST","parentFrameId":-1,"requestBody":{"raw":[{"bytes":{}}]},"requestId":"13910","tabId":430,"timeStamp":1645826234368.5469,"type":"xmlhttprequest","url":"https://www.example.com/api/v4/login"}
Aiden Pearce
  • 243
  • 2
  • 13
  • JSON.stringify doesn't work with this data so remove it and inspect the variable interactively by setting a breakpoint in [background.js devtools](/a/10258029). – wOxxOm Feb 25 '22 at 22:53
  • I think it is not the issue here. Because in the example request, `requestBody` is empty. It should not be empty, there should be `username` and `passsword` data in it. I have tried to `console.log(details.requestBody.formData)` and it does not work too. – Aiden Pearce Feb 26 '22 at 11:02
  • Inspect the request object entirely. Maybe the data is inside `raw`. – wOxxOm Feb 26 '22 at 17:59
  • Nope the problem is some kinda security protection mechanism. Chrome does not let you get the formData element since it will include username and passwords and critical information in it. – Aiden Pearce Feb 28 '22 at 13:18
  • 1
    Extensions can get this data via a content script anyway so hiding this data in webRequest is useless. Did you inspect the object in devtools? Your console log shows there's `bytes`, which cannot be printed, you need to inspect/use it directly. – wOxxOm Feb 28 '22 at 13:20

0 Answers0