-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathdnr.ts
More file actions
77 lines (66 loc) · 2.18 KB
/
dnr.ts
File metadata and controls
77 lines (66 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
export const isValidDNRUrlFilter = (text: string) => {
// https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest?hl=en#property-RuleCondition-urlFilter
const domainNameAnchor = text.startsWith("||");
const leftAnchor = !domainNameAnchor && text.startsWith("|");
const rightAnchor = text.endsWith("|");
try {
let s = text;
if (domainNameAnchor) s = s.slice(2);
if (leftAnchor) s = s.slice(1);
if (rightAnchor) s = s.slice(0, -1);
const t = s.replace(/\*/g, "").replace(/^/g, "_");
// eslint-disable-next-line no-control-regex
if (/[^\x00-\xFF]/.test(t)) return false;
new URL(t);
return true;
} catch {
return false;
}
};
export const convertDomainToDNRUrlFilter = (text: string) => {
// will match its domain and subdomain
let ret;
text = text.toLowerCase();
try {
if (text.startsWith("http") && /^http[s*]?:\/\//.test(text)) {
const u = new URL(`${text.replace("http*://", "http-wildcard://")}`);
ret = `|${u.origin.replace("http-wildcard://", "http://")}`;
} else {
const u = new URL(`https://${text}/`);
ret = `||${u.hostname}`;
}
} catch {
throw new Error("invalid domain");
}
return ret;
};
export const createNoCSPRules = (urlFilters: string[]) => {
const REMOVE_HEADERS = [
`content-security-policy`,
`content-security-policy-report-only`,
`x-webkit-csp`,
`x-content-security-policy`,
`x-frame-options`,
];
const { RuleActionType, HeaderOperation, ResourceType } = chrome.declarativeNetRequest;
if (urlFilters.length > 512) {
throw new Error(`Too many URL patterns (${urlFilters.length}). Max is 512.`);
}
const rules: chrome.declarativeNetRequest.Rule[] = urlFilters.map((urlFilter, index) => {
return {
id: 2001 + index,
action: {
type: RuleActionType.MODIFY_HEADERS,
responseHeaders: REMOVE_HEADERS.map((header) => ({
operation: HeaderOperation.REMOVE,
header,
})),
},
condition: {
urlFilter: urlFilter,
resourceTypes: [ResourceType.MAIN_FRAME, ResourceType.SUB_FRAME],
},
} satisfies chrome.declarativeNetRequest.Rule;
});
return rules;
};