Skip to content

Commit 6e55658

Browse files
committed
More reliable implementation of DNR-based unrestricted tabs.
1 parent 05e62a5 commit 6e55658

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/bg/DNRPolicy.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@
3434

3535
let _lastPolicy;
3636

37+
const dnrTypes = Object.values(browser.declarativeNetRequest.ResourceType);
3738
const resourceTypesMap = {};
38-
{
39-
const dnrTypes = Object.values(browser.declarativeNetRequest.ResourceType);
40-
41-
for(const [key, value] of Object.entries(RequestGuard.policyTypesMap)) {
42-
if (!(value && dnrTypes.includes(key))) continue;
43-
const mapping = resourceTypesMap[value] ||= [];
44-
mapping.push(key);
45-
}
39+
for(const [key, value] of Object.entries(RequestGuard.policyTypesMap)) {
40+
if (!(value && dnrTypes.includes(key))) continue;
41+
const mapping = resourceTypesMap[value] ||= [];
42+
mapping.push(key);
4643
}
4744

4845
const ResourceTypeFor = {
@@ -184,17 +181,30 @@
184181

185182
async function addTabRules(rules = []) {
186183
if (ns.unrestrictedTabs.size) {
184+
const tabIds = [...ns.unrestrictedTabs];
185+
tabIds.push(browser.tabs.TAB_ID_NONE); // for service workers
187186
rules.push({
188187
id: TAB_BASE,
189188
priority: TAB_PRIORITY,
190189
action: {
191190
type: "allowAllRequests",
192191
},
193192
condition: {
194-
tabIds: [...ns.unrestrictedTabs],
193+
tabIds,
195194
resourceTypes: ["main_frame", "sub_frame"],
196195
}
197196
});
197+
rules.push({
198+
id: TAB_BASE + 1,
199+
priority: TAB_PRIORITY,
200+
action: {
201+
type: "allow",
202+
},
203+
condition: {
204+
tabIds,
205+
resourceTypes: dnrTypes,
206+
}
207+
});
198208
}
199209
await addCtxRules(rules);
200210
return rules;
@@ -223,6 +233,9 @@
223233
const urlFilter = toUrlFilter(siteKey);
224234
for (const [capsKey, tabIds] of [...caps2Tabs]) {
225235
forBlockAllow(new Set(JSON.parse(capsKey)), (type, resourceTypes) => {
236+
if (type == "allow" && resourceTypes.includes("script")) {
237+
tabIds.push(browser.tabs.TAB_ID_NONE); // for service workers
238+
}
226239
rules.push({
227240
id: TAB_BASE + rules.length,
228241
priority: CTX_PRIORITY,

0 commit comments

Comments
 (0)