My goal is to intercept a fetch request, change it's response body and fulfill the request using a chrome extension. I am using the following code, but the request get's not fulfilled.
chrome.debugger.onEvent.addListener(async (source, method, params: any) => {
if (method == "Fetch.requestPaused") {
const requestId = params.requestId
const request = params.request
if (params.request.url.includes("myPage.html")) {
console.log("Intercepting request " + requestId)
chrome.debugger.sendCommand(
{ tabId: tabId },
"Fetch.getResponseBody",
{
requestId: requestId,
},
(response: any) => {
const responseCode = params.responseCode
const responseHeaders = params.responseHeaders
if (response.base64Encoded) {
let responseBody = atob(response.body)
// modify response body string
chrome.debugger.sendCommand({ tabId: tabId }, "Fetch.fulfillRequest", {
requestId: requestId,
responseCody: responseCode,
responseHeaders: responseHeaders,
body: btoa(responseBody),
})
} else {
chrome.debugger.sendCommand({ tabId: tabId }, "Fetch.continueRequest", { requestId: requestId })
}
},
)
} else {
chrome.debugger.sendCommand({ tabId: tabId }, "Fetch.continueRequest", { requestId: requestId })
}
}
})
The interception, reading, modifying and encoding the body works fine, but the request won't get fulfilled.