Redirects working both in Chrome and Firefox

Did you manage to get this to work? I have a similar case. How can I use the declarativeNetRequest (or something else) to generate dynamic rules that work on both chrome and Firefox? I want to redirect network calls to my local server JS script when I am prototyping, but when I am not, I want to use a static link to my website. I will have a toggle button (tied to a variable in storage) to determine which URL to redirect to.
async function setRedirectRule(newRedirectUrl: string) {
let newRedirectRule: chrome.declarativeNetRequest.Rule = {
id: RULE_ID,
priority: 1,
action: {
type: chrome.declarativeNetRequest.RuleActionType.REDIRECT,
redirect: { url: newRedirectUrl }
},
condition: {
urlFilter: "*://*/*",

resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME]
}
}

try {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [RULE_ID],
addRules: [newRedirectRule]
})
const currentRules = await chrome.declarativeNetRequest.getDynamicRules()
console.log("Redirect rule updated")
console.log("Current rules:", currentRules)
} catch (error) {
console.error("Error updating redirect rule:", error)
}
}
async function setRedirectRule(newRedirectUrl: string) {
let newRedirectRule: chrome.declarativeNetRequest.Rule = {
id: RULE_ID,
priority: 1,
action: {
type: chrome.declarativeNetRequest.RuleActionType.REDIRECT,
redirect: { url: newRedirectUrl }
},
condition: {
urlFilter: "*://*/*",

resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME]
}
}

try {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [RULE_ID],
addRules: [newRedirectRule]
})
const currentRules = await chrome.declarativeNetRequest.getDynamicRules()
console.log("Redirect rule updated")
console.log("Current rules:", currentRules)
} catch (error) {
console.error("Error updating redirect rule:", error)
}
}
If I put this into the background.ts it works for chrome, every time I open a new tab it just redirects to google. But it doesn't work for firefox. This is in my package.json:
"manifest": {
"host_permissions": [
"https://*/*"
],
"permissions": [
"tabs",
"declarativeNetRequest"
]
}
"manifest": {
"host_permissions": [
"https://*/*"
],
"permissions": [
"tabs",
"declarativeNetRequest"
]
}
Arcane
Arcane•43d ago
@BozoWithTheBanjo has reached level 1. GG!