I’m testing the webRequest API for extension. Specifically, I want to modify the data in the request body before sending it to the server:
//manifest.json
{
"description": "Demonstrating webRequests",
"manifest_version": 2,
"name": "webRequest-demo",
"version": "1.0",
"permissions": ["webRequest", "webRequestBlocking", "*://localhost/*"],
"background": {
"scripts": ["background.js"]
}
}
//background.js
function changebody(requestDetails) {
if(requestDetails.method=="POST") {
console.log("requestDetails:", requestDetails);
requestDetails.requestBody.formData["textarea1"]=["hello world"];
}
return requestDetails;
}
browser.webRequest.onBeforeRequest.addListener(
changebody,
{ urls: ["*://localhost/*"]},
["blocking","requestBody"],
);
<html>
<head><title>test</title>
</head>
<body>
<form action="test.php" method="post">
<textarea id="textarea1" name="textarea1">test</textarea>
<input type="submit">
</form>
</body>
</html>
I think the console log would display the data before the modification, i.e., “test”, but it actually displays the modified data “hello world”, why?