I built a Chrome extension (manifest V3) that has dynamic domain redirect functionality. In general it works as expected, even if I redirect to localhost
. However, if I attempt to redirect to localhost with a port number, e.g. localhost:4000
, it adds the rule with no errors (as is confirmed by running chrome.declarativeNetRequest.getDynamicRules()
in the service worker console), but doesn't redirect.
Here's how I add the rule in the background service worker:
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [
{
id: 1,
priority: 1,
action: {type: 'redirect', redirect: {transform: {host: 'localhost:4000'}}},
condition: {resourceTypes: ['main_frame'], requestDomains: ['example.com']}
}
]
});
I would like to know if this is expected functionality for whatever reason or is it a bug. Additionally, If anyone could help with a work around, that would be much appreciated.
Thank you.
CodePudding user response:
I'll answer my own question here, based on what @wOxxOm correctly pointed out in a comment, as it may be useful to soemone else.
When forwarding to a domain with an specified port, the port needs to be specified sepearately.
The corrected code would be as follows:
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [
{
id: 1,
priority: 1,
action: {type: 'redirect', redirect: {transform: {host: 'localhost', port: '4000'}}},
condition: {resourceTypes: ['main_frame'], requestDomains: ['example.com']}
}
]
})