declarativeNetRequest blocking One time override

101 views
Skip to first unread message

MV3 Noob

unread,
Aug 2, 2023, 6:48:40 AM8/2/23
to Chromium Extensions
Hello,
In my quest to migrate to MV3 i am trying to give the option for the user to override the blocking and can't find a solution.

The process is :
1. url is blocked by declarativeNetRequest
2. When its the mainframe, i redirect the user using the regex feature to a page explaining why it was blocked and i pass the blocked url to this page using the regex.
3. I Want to give the user the option to still go to this page, but fail to find a solution.
Thanks for your guidance.
This is what i use to block :
action: {
type: 'redirect',
redirect: {
regexSubstitution: EXT_PAGE + '#\\0'
},
},
condition: {
regexFilter: '^http.+',
requestDomains: domainslist,
resourceTypes: ["main_frame"],
},

wOxxOm

unread,
Aug 2, 2023, 11:04:43 AM8/2/23
to Chromium Extensions, MV3 Noob
You can use updateSessionRules inside EXT_PAGE script then redirect to the url, something like this:

async function onclick() {
  const tab = await chrome.tabs.getCurrent();
  const url = location.hash.slice(1);
  await chrome.declarativeNetRequest.updateSessionRules({
    removeRuleIds: [1],
    addRules: [{
      id: 1,
      condition: {
        tabIds: [tab.id],
        urlFilter: url,
      },
      action: {
        type: 'allow',
      },
      priority: 2,
    }]
  });
  location.href = url;
}

MV3 Noob

unread,
Aug 2, 2023, 11:10:42 AM8/2/23
to Chromium Extensions, wOxxOm, MV3 Noob
Thanks, very insightful as always.
With the tabid condition, it will make this override only active until this tab is closed right ?
I guess if the user wants it for a certain amount of time on all tabs,  i can store a time in the localstorage until this rule is overriden with another function.

MV3 Noob

unread,
Aug 2, 2023, 2:03:23 PM8/2/23
to Chromium Extensions, MV3 Noob, wOxxOm
I have tried but it keeps being redirect to the html blocked page :
await chrome.declarativeNetRequest.updateSessionRules({
removeRuleIds: [3],
addRules: [{
id: 3,
condition: {
tabIds: [tab.id],
urlFilter: url,
},
action: {
type: 'allow',
},
priority: 1,
}]
});
location.href = url;

Here's how i setup my blocking, the json is a list of domains (so i cannot disable individually or it would disable all the domains). I have tried setting the priority as 2 and the addRules to 1 but it did not help.
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
"id": 1,
"priority": 2,
"action": {"type": 'block'},
"condition": {"requestDomains": json, "resourceTypes":["sub_frame", "stylesheet", "script", "image", "font", "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket", "other"]
},
}],
}, )
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [{
"id": 2,
"priority": 2,
action: {
type: 'redirect',
redirect: {
regexSubstitution: EXT_PAGE + '#\\0'
},
},
condition: {
regexFilter: '^http.+',
requestDomains: json,
resourceTypes: ["main_frame"],
},
}],
}, )

wOxxOm

unread,
Aug 2, 2023, 2:38:14 PM8/2/23
to Chromium Extensions, MV3 Noob, wOxxOm
You're using a lower priority in updateSessionRules but you need to use a higher priority.

MV3 Noob

unread,
Aug 3, 2023, 3:41:22 AM8/3/23
to Chromium Extensions, wOxxOm, MV3 Noob
I have tried it both ways, same results. I tried also to remove the tab.id to make it global, but it did not make any changes.
I also tried to set the url with and without the https://, no luck.
Its weird because all your recommendations make sense and match the documentation, but i can't figure out what's wrong, is there any way to debug declarativeNetRequest ?

MV3 Noob

unread,
Aug 3, 2023, 3:46:02 AM8/3/23
to Chromium Extensions, MV3 Noob, wOxxOm
Ok i think i figured it out, i needed to specify resourceTypes: ["main_frame"]
It works now, thanks a 100'000'000 for your help !!

wOxxOm

unread,
Aug 3, 2023, 3:46:19 AM8/3/23
to Chromium Extensions, MV3 Noob, wOxxOm
What is "both ways"? The updateSessionRules priority should be bigger (i.e. 2) than that in main rules (i.e. the default 1). As for debugging, see onRuleMatchedDebug.

wOxxOm

unread,
Aug 3, 2023, 3:47:04 AM8/3/23
to Chromium Extensions, wOxxOm, MV3 Noob
> i needed to specify resourceTypes: ["main_frame"]

Looks like a bug in the API to me...

wOxxOm

unread,
Aug 3, 2023, 3:49:52 AM8/3/23
to Chromium Extensions, MV3 Noob
The documentation mentions that allowAllRequests must specify resourceTypes but nothing about allow, so it's probably an error in the documentation.
Reply all
Reply to author
Forward
0 new messages