@@ -35,6 +35,23 @@ const resourceTypeMap: Record<string, DeclarativeNetRequest.ResourceType> = {
3535 CSPViolationReport : 'csp_report' ,
3636} ;
3737
38+ const debuggerAPI = { ...chrome . debugger } ;
39+ // Polyfill debugger API
40+ [ 'attach' , 'detach' , 'sendCommand' , 'getTargets' ] . forEach ( method => {
41+ // @ts -ignore
42+ debuggerAPI [ method ] = ( ...args ) =>
43+ new Promise ( ( resolve , reject ) => {
44+ // @ts -ignore
45+ chrome . debugger [ method ] ( ...args , res => {
46+ if ( chrome . runtime . lastError ) {
47+ reject ( chrome . runtime . lastError ) ;
48+ } else {
49+ resolve ( res ) ;
50+ }
51+ } ) ;
52+ } ) ;
53+ } ) ;
54+
3855class ChromeResponseModifier {
3956 private disableAll = false ;
4057 private modifyBody = false ;
@@ -63,7 +80,7 @@ class ChromeResponseModifier {
6380 }
6481
6582 private async getAttached ( ) {
66- const targets = await chrome . debugger . getTargets ( ) ;
83+ const targets = await debuggerAPI . getTargets ( ) ;
6784 const res = targets . filter ( x => Boolean ( x . tabId ) && x . attached ) ;
6885 this . attached = new Set ( res . map ( x => x . tabId ! ) ) ;
6986 this . fetchEnabled . forEach ( x => {
@@ -86,15 +103,15 @@ class ChromeResponseModifier {
86103 }
87104 try {
88105 logger . debug ( '[chrome-response-modifier] detach tab' , tabId ) ;
89- await chrome . debugger . detach ( { tabId } ) ;
106+ await debuggerAPI . detach ( { tabId } ) ;
90107 this . attached . delete ( tabId ) ;
91108 } catch ( e ) {
92109 console . error ( 'detachTab failed:' , e ) ;
93110 }
94111 }
95112
96113 private async attachTab ( tabId ?: number ) {
97- if ( typeof tabId === 'undefined' ) {
114+ if ( typeof tabId === 'undefined' || ! this . isEnabled ) {
98115 return ;
99116 }
100117 if ( this . attached . has ( tabId ) || this . pendingTabIds . has ( tabId ) ) {
@@ -103,7 +120,7 @@ class ChromeResponseModifier {
103120 try {
104121 this . pendingTabIds . add ( tabId ) ;
105122 logger . debug ( '[chrome-response-modifier] attach tab' , tabId ) ;
106- await chrome . debugger . attach ( { tabId } , '1.3' ) ;
123+ await debuggerAPI . attach ( { tabId } , '1.3' ) ;
107124 this . attached . add ( tabId ) ;
108125 } catch ( e ) {
109126 logger . debug ( '[chrome-response-modifier] attach tab failed' , tabId , e ) ;
@@ -112,7 +129,7 @@ class ChromeResponseModifier {
112129 }
113130
114131 private async enableFetch ( tabId ?: number ) {
115- if ( typeof tabId === 'undefined' ) {
132+ if ( typeof tabId === 'undefined' || ! this . isEnabled ) {
116133 return ;
117134 }
118135 await this . attachTab ( tabId ) ;
@@ -122,7 +139,7 @@ class ChromeResponseModifier {
122139 try {
123140 this . pendingTabIds . add ( tabId ) ;
124141 logger . debug ( '[chrome-response-modifier] enable fetch' , tabId ) ;
125- await chrome . debugger . sendCommand ( { tabId : tabId } , 'Fetch.enable' , {
142+ await debuggerAPI . sendCommand ( { tabId : tabId } , 'Fetch.enable' , {
126143 patterns : [
127144 {
128145 urlPattern : 'http://*' ,
@@ -149,7 +166,7 @@ class ChromeResponseModifier {
149166 return ;
150167 }
151168 try {
152- await chrome . debugger . sendCommand ( { tabId : tabId } , 'Fetch.disable' ) ;
169+ await debuggerAPI . sendCommand ( { tabId : tabId } , 'Fetch.disable' ) ;
153170 this . fetchEnabled . delete ( tabId ) ;
154171 } catch ( e ) {
155172 console . error ( 'Fetch.disable failed: ' , e ) ;
@@ -254,7 +271,7 @@ class ChromeResponseModifier {
254271 : resourceTypeMap [ resourceType ] ,
255272 } ) ;
256273 if ( ! isValidArray ( rules ) ) {
257- return chrome . debugger . sendCommand ( source , 'Fetch.continueRequest' , {
274+ return debuggerAPI . sendCommand ( source , 'Fetch.continueRequest' , {
258275 requestId,
259276 } ) ;
260277 }
@@ -263,11 +280,9 @@ class ChromeResponseModifier {
263280 let finalBody : any ;
264281 let resp : any ;
265282 if ( hasFunc ) {
266- resp = await chrome . debugger . sendCommand (
267- source ,
268- 'Fetch.getResponseBody' ,
269- { requestId } ,
270- ) ;
283+ resp = await debuggerAPI . sendCommand ( source , 'Fetch.getResponseBody' , {
284+ requestId,
285+ } ) ;
271286 }
272287 for ( const rule of rules ) {
273288 if ( ! finalBody ) {
@@ -311,7 +326,7 @@ class ChromeResponseModifier {
311326 delete newHeaders [ name ] ;
312327 }
313328 }
314- return chrome . debugger . sendCommand ( source , 'Fetch.fulfillRequest' , {
329+ return debuggerAPI . sendCommand ( source , 'Fetch.fulfillRequest' , {
315330 requestId,
316331 responseCode : 200 ,
317332 responseHeaders : finalHeaders ,
0 commit comments