BozoWithTheBanjo
BozoWithTheBanjo
PD🧩 Plasmo Developers
Created by The Coding Sloth on 9/9/2023 in #👟framework
declarativeNetRequest
Thank you, I appriciate it!
15 replies
PD🧩 Plasmo Developers
Created by The Coding Sloth on 9/9/2023 in #👟framework
declarativeNetRequest
No description
15 replies
PD🧩 Plasmo Developers
Created by The Coding Sloth on 9/9/2023 in #👟framework
declarativeNetRequest
Do you maybe have an example how to do that?
15 replies
PD🧩 Plasmo Developers
Created by The Coding Sloth on 9/9/2023 in #👟framework
declarativeNetRequest
Hey, thank you for answering!
15 replies
PD🧩 Plasmo Developers
Created by The Coding Sloth on 9/9/2023 in #👟framework
declarativeNetRequest
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"
]
}
15 replies