1+ const NOCSP_RULES_DOMAIN_MAX_COUNT = 32768 ;
2+ const NOCSP_RULES_URLFILTER_MAX_COUNT = 512 ;
3+
14export const isValidDNRUrlFilter = ( text : string ) => {
25 // https://developer.chrome.com/docs/extensions/reference/api/declarativeNetRequest?hl=en#property-RuleCondition-urlFilter
36
@@ -45,7 +48,19 @@ export const convertDomainToDNRUrlFilter = (text: string) => {
4548 return ret ;
4649} ;
4750
48- export const createNoCSPRules = ( urlFilters : string [ ] ) => {
51+ export const createNoCSPRules = ( domains : string [ ] , urlFilters : string [ ] ) => {
52+ // domains = max 32768 domains
53+ // urlFilters = max 512
54+ if ( domains . length > NOCSP_RULES_DOMAIN_MAX_COUNT ) {
55+ throw new Error (
56+ `createNoCSPRules: The number of domains = ${ domains . length } exceeding ${ NOCSP_RULES_DOMAIN_MAX_COUNT } `
57+ ) ;
58+ }
59+ if ( urlFilters . length > NOCSP_RULES_URLFILTER_MAX_COUNT ) {
60+ throw new Error (
61+ `createNoCSPRules: The number of urlFilters = ${ urlFilters . length } exceeding ${ NOCSP_RULES_URLFILTER_MAX_COUNT } `
62+ ) ;
63+ }
4964 const REMOVE_HEADERS = [
5065 `content-security-policy` ,
5166 `content-security-policy-report-only` ,
@@ -54,12 +69,9 @@ export const createNoCSPRules = (urlFilters: string[]) => {
5469 `x-frame-options` ,
5570 ] ;
5671 const { RuleActionType, HeaderOperation, ResourceType } = chrome . declarativeNetRequest ;
57- if ( urlFilters . length > 512 ) {
58- throw new Error ( `Too many URL patterns (${ urlFilters . length } ). Max is 512.` ) ;
59- }
6072 const rules : chrome . declarativeNetRequest . Rule [ ] = urlFilters . map ( ( urlFilter , index ) => {
6173 return {
62- id : 2001 + index ,
74+ id : 2002 + index ,
6375 action : {
6476 type : RuleActionType . MODIFY_HEADERS ,
6577 responseHeaders : REMOVE_HEADERS . map ( ( header ) => ( {
@@ -73,5 +85,43 @@ export const createNoCSPRules = (urlFilters: string[]) => {
7385 } ,
7486 } satisfies chrome . declarativeNetRequest . Rule ;
7587 } ) ;
88+ const requestDomains = domains
89+ . map ( ( s ) => {
90+ try {
91+ const u = new URL ( `https://${ s } /` ) ; // 取编码后的 hostname
92+ return u . hostname ;
93+ } catch {
94+ // ingored
95+ }
96+ } )
97+ . filter ( Boolean ) as string [ ] ; // 去除错误或空字串
98+ if ( domains . length > 0 ) {
99+ rules . push ( {
100+ id : 2001 ,
101+ action : {
102+ type : RuleActionType . MODIFY_HEADERS ,
103+ responseHeaders : REMOVE_HEADERS . map ( ( header ) => ( {
104+ operation : HeaderOperation . REMOVE ,
105+ header,
106+ } ) ) ,
107+ } ,
108+ condition : {
109+ requestDomains : requestDomains ,
110+ resourceTypes : [ ResourceType . MAIN_FRAME , ResourceType . SUB_FRAME ] ,
111+ } ,
112+ } satisfies chrome . declarativeNetRequest . Rule ) ;
113+ }
76114 return rules ;
77115} ;
116+
117+ export const removeDynamicRulesInRange = async ( minId : number , maxId : number ) => {
118+ const existingRules = await chrome . declarativeNetRequest . getDynamicRules ( ) ;
119+
120+ const idsToRemove = existingRules . filter ( ( rule ) => rule . id >= minId && rule . id <= maxId ) . map ( ( rule ) => rule . id ) ;
121+
122+ if ( idsToRemove . length === 0 ) return ;
123+
124+ await chrome . declarativeNetRequest . updateDynamicRules ( {
125+ removeRuleIds : idsToRemove ,
126+ } ) ;
127+ } ;
0 commit comments