Hi,
I have a manifest v2 Chrome extension that uses blocking chrome.webRequest.onHeadersReceived to rewrite part of the Location header on responses from a matching url.
The use case is to intercept the redirection from an externally hosted login page (aws cognito hosted ui), and to modify the url host to localhost during development.
The redirect url has a query param appended (?code=xxxx) - code parameter is used in authorization_code grant type flow in order to acquire the tokens.
For this reason, I need to change the host in the Location header while preserving the query params.
I have not been able to find a way to do this using the new manifest v3 chrome.declarativeNetRequest.
Would anyone know if there is a way to do the same thing as the code below but in manifest v3?
Thanks
const config = {filter: 'https://example.com/*', rewrite: 'http://localhost:5137'};
chrome.webRequest.onHeadersReceived.addListener(
rewriteLocationHost,
{
urls: [config.filter]
},
['blocking', 'responseHeaders', 'extraHeaders']
)
function rewriteLocationHost(data) {
if (data.statusCode === 301 || data.statusCode === 302) {
const header = data.responseHeaders.find(v => v.name.toLowerCase() === 'location')
if (header) {
header.value = changeHost(header.value, config.rewrite)
}
}
return {responseHeaders: data.responseHeaders}
}
function changeHost(url, newHost) {
const u = new URL(url)
const i = url.indexOf(u.host) + u.host.length + 1
const newUrl = newHost + url.substring(i)
return newUrl
}