Skip to content

Commit 1f516ac

Browse files
committed
Fixed contextual policies MV3 implementation (issue #394).
1 parent a4e82a5 commit 1f516ac

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/bg/DNRPolicy.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,34 @@
209209
if (!tabs?.length) {
210210
return rules;
211211
}
212-
for (const [siteKey, perms] of ctxSettings) {
213-
const tabIds = tabs.filter(tab => perms.contextual.match(tab.url)).map(tab => tab.id);
214-
if (!tabIds.length) continue;
212+
for (const [siteKey,] of ctxSettings) {
213+
const ctxTabs = tabs.map(tab =>
214+
({id: tab.id,
215+
settings: policy.get(siteKey, tab.url),
216+
}))
217+
.filter(({settings}) => settings.contextMatch);
218+
const caps2Tabs = new Map();
219+
for(const {id, settings} of ctxTabs) {
220+
const capsKey = JSON.stringify([...settings.perms.capabilities]);
221+
caps2Tabs.get(capsKey)?.push(id) || caps2Tabs.set(capsKey, [id]);
222+
}
215223
const urlFilter = toUrlFilter(siteKey);
216-
forBlockAllow(perms.capabilities, (type, resourceTypes) => {
217-
rules.push({
218-
id: TAB_BASE + rules.length,
219-
priority: CTX_PRIORITY,
220-
action: {
221-
type,
222-
},
223-
condition: {
224-
tabIds,
225-
urlFilter,
226-
resourceTypes,
227-
}
224+
for (const [capsKey, tabIds] of [...caps2Tabs]) {
225+
forBlockAllow(new Set(JSON.parse(capsKey)), (type, resourceTypes) => {
226+
rules.push({
227+
id: TAB_BASE + rules.length,
228+
priority: CTX_PRIORITY,
229+
action: {
230+
type,
231+
},
232+
condition: {
233+
tabIds,
234+
urlFilter,
235+
resourceTypes,
236+
}
237+
});
228238
});
229-
});
239+
}
230240
}
231241
if (!cascade) {
232242
return rules;

0 commit comments

Comments
 (0)