declarativeNetRequest

I want to use the declarativeNetRequest API, and I have it working for chrome, but when I test it on firefox, its not working:
async function setRedirectRule(newRedirectUrl: string) {
let newRedirectRule = {
id: RULE_ID,
priority: 1,
action: {
type: "redirect",
redirect: { url: newRedirectUrl }
},
condition: {
urlFilter: "*://*/*",

resourceTypes: ["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 = {
id: RULE_ID,
priority: 1,
action: {
type: "redirect",
redirect: { url: newRedirectUrl }
},
condition: {
urlFilter: "*://*/*",

resourceTypes: ["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)
}
}
On chrome the new rule appears when I log currentRules, but on Firefox it's empty, not sure why it's not updating.
11 Replies
The Coding Sloth
The Coding SlothOP15mo ago
Found the solution. It's a simple host permission issue. Chrome gives you these permissions automatically when you're developing, but on firefox you have to give permission to each website.
Arcane
Arcane15mo ago
@thecodingsloth has reached level 1. GG!
BozoWithTheBanjo
Hey @The Coding Sloth Did you manage to get this to work? I have a similar case. How can I use the declarativeNetRequest 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 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"
]
}
The Coding Sloth
The Coding SlothOP8mo ago
I did figure it out. It seems that by default Firefox doesn’t give this permission automatically like chrome So you’ll need the user to allow these permissions
BozoWithTheBanjo
Hey, thank you for answering!
YAGPDB.xyz
YAGPDB.xyz8mo ago
Gave +1 Rep to @The Coding Sloth (current: #27 - 1)
BozoWithTheBanjo
Do you maybe have an example how to do that?
BozoWithTheBanjo
I figured it out, and it does work, thank you so much! 😄
No description
The Coding Sloth
The Coding SlothOP8mo ago
Happy to help👍
The Coding Sloth
The Coding SlothOP8mo ago
GitHub
GitHub - The-CodingSloth/haha-funny-leetcode-extension
Contribute to The-CodingSloth/haha-funny-leetcode-extension development by creating an account on GitHub.
BozoWithTheBanjo
Thank you, I appriciate it!
Want results from more Discord servers?
Add your server