@@ -82,17 +82,26 @@ export class BackgroundGrant {
8282 reqOpt . push ( 'extraHeaders' ) ;
8383 respOpt . push ( 'extraHeaders' ) ;
8484 }
85- chrome . webRequest . onBeforeSendHeaders . addListener ( ( data ) => {
85+ const maxRedirects = new Map < string , [ number , number ] > ( ) ;
86+ // 处理重定向请求
87+ chrome . webRequest . onBeforeSendHeaders . addListener ( ( details ) => {
88+ if ( ! this . isExtensionRequest ( details ) ) {
89+ return ;
90+ }
8691 let setCookie = '' ;
8792 let cookie = '' ;
8893 let anonymous = false ;
8994 let origin = '' ;
9095 let isScriptcat = false ;
9196 const requestHeaders : chrome . webRequest . HttpHeader [ ] = [ ] ;
9297 const unsafeHeader : { [ key : string ] : string } = { } ;
93- data . requestHeaders ?. forEach ( ( val ) => {
98+ details . requestHeaders ?. forEach ( ( val ) => {
9499 const lowerCase = val . name . toLowerCase ( ) ;
95100 switch ( lowerCase ) {
101+ case 'x-cat-' + this . rand + '-max-redirects' : {
102+ maxRedirects . set ( details . requestId , [ 0 , parseInt ( val . value || '' ) ] ) ;
103+ break ;
104+ }
96105 case 'x-cat-' + this . rand + '-cookie' : {
97106 setCookie = val . value || '' ;
98107 break ;
@@ -166,16 +175,20 @@ export class BackgroundGrant {
166175 urls : [ '<all_urls>' ] ,
167176 } , reqOpt ) ;
168177 const responseHeader : { [ key : string ] : boolean } = { 'set-cookie' : true } ;
169- chrome . webRequest . onHeadersReceived . addListener ( ( details : chrome . webRequest . WebResponseHeadersDetails & { originUrl ?: string } ) => {
170- if ( ( details . initiator && chrome . extension . getURL ( '' ) . startsWith ( details . initiator ) ) ||
171- ( details . originUrl && details . originUrl . startsWith ( chrome . extension . getURL ( '' ) ) ) ) {
172- details . responseHeaders ?. forEach ( val => {
178+ chrome . webRequest . onHeadersReceived . addListener ( ( details ) => {
179+ if ( this . isExtensionRequest ( details ) ) {
180+ details . responseHeaders ?. forEach ( ( val ) => {
173181 if ( responseHeader [ val . name ] ) {
174- details . responseHeaders ?. push ( {
175- name : 'x-cat-' + this . rand + '-' + val . name ,
176- value : val . value ,
177- binaryValue : val . binaryValue ,
178- } ) ;
182+ val . name = 'x-cat-' + this . rand + '-' + val . name ;
183+ }
184+ if ( val . name . toLowerCase ( ) === 'location' ) {
185+ const nums = maxRedirects . get ( details . requestId ) ;
186+ if ( nums ) {
187+ nums [ 0 ] ++
188+ if ( nums [ 0 ] > nums [ 1 ] ) {
189+ val . name = 'x-cat-' + this . rand + '-' + val . name ;
190+ }
191+ }
179192 }
180193 } ) ;
181194 return {
@@ -185,11 +198,22 @@ export class BackgroundGrant {
185198 } , {
186199 urls : [ '<all_urls>' ] ,
187200 } , respOpt ) ;
201+ chrome . webRequest . onCompleted . addListener ( ( details ) => {
202+ if ( ! this . isExtensionRequest ( details ) ) {
203+ return ;
204+ }
205+ maxRedirects . delete ( details . requestId ) ;
206+ } , { urls : [ '<all_urls>' ] } ) ;
188207 } catch ( e ) {
189208 console . log ( e ) ;
190209 }
191210 }
192211
212+ protected isExtensionRequest ( details : chrome . webRequest . ResourceRequest & { originUrl ?: string } ) : boolean {
213+ return ( details . initiator && chrome . extension . getURL ( '' ) . startsWith ( details . initiator ) ) ||
214+ ( details . originUrl && details . originUrl . startsWith ( chrome . extension . getURL ( '' ) ) ) ? true : false ;
215+ }
216+
193217 // 单实例
194218 public static SingleInstance ( scriptMgr : ScriptManager , listener : IGrantListener , isdebug : boolean ) : BackgroundGrant {
195219 if ( ! BackgroundGrant . _singleInstance ) {
@@ -573,8 +597,7 @@ export class BackgroundGrant {
573597 post . postMessage ( grant ) ;
574598 }
575599
576- this . dealUnsafeHeader ( xhr , config . headers ) ;
577- this . dealXhrCookie ( xhr , config . cookie , config . anonymous ) ;
600+ this . dealUnsafeHeader ( config , xhr , config . headers ) ;
578601
579602 if ( config . timeout ) {
580603 xhr . timeout = config . timeout ;
@@ -1284,7 +1307,7 @@ export class BackgroundGrant {
12841307 } ) ;
12851308 }
12861309
1287- protected dealUnsafeHeader ( xhr : XMLHttpRequest , headers ?: { [ key : string ] : string } ) : { [ key : string ] : string } {
1310+ protected dealUnsafeHeader ( config : GMSend . XHRDetails , xhr : XMLHttpRequest , headers ?: { [ key : string ] : string } ) : { [ key : string ] : string } {
12881311 xhr . setRequestHeader ( 'X-Cat-' + this . rand + '-Scriptcat' , 'true' ) ;
12891312 for ( let key in headers ) {
12901313 const val = headers [ key ] ;
@@ -1306,16 +1329,16 @@ export class BackgroundGrant {
13061329 App . Log . Debug ( 'gmxhr' , ( e as Error ) . message , 'GM_xmlhttpRequest' ) ;
13071330 }
13081331 }
1309- return headers || { } ;
1310- }
1311-
1312- protected dealXhrCookie ( xhr : XMLHttpRequest , cookie ?: string , anonymous ?: boolean ) {
1313- if ( cookie ) {
1314- xhr . setRequestHeader ( 'X-Cat-' + this . rand + '-Cookie' , cookie ) ;
1332+ if ( config . maxRedirects !== undefined ) {
1333+ xhr . setRequestHeader ( 'X-Cat-' + this . rand + '-Max-redirects' , config . maxRedirects . toString ( ) ) ;
13151334 }
1316- if ( anonymous ) {
1335+ if ( config . cookie ) {
1336+ xhr . setRequestHeader ( 'X-Cat-' + this . rand + '-Cookie' , config . cookie ) ;
1337+ }
1338+ if ( config . anonymous ) {
13171339 xhr . setRequestHeader ( 'X-Cat-' + this . rand + '-Anonymous' , 'true' ) ;
13181340 }
1341+ return headers || { } ;
13191342 }
13201343
13211344 @BackgroundGrant . GMFunction ( )
@@ -1384,8 +1407,7 @@ export class BackgroundGrant {
13841407 post . postMessage ( grant ) ;
13851408 }
13861409
1387- this . dealUnsafeHeader ( xhr , config . headers ) ;
1388- this . dealXhrCookie ( xhr , config . cookie , config . anonymous ) ;
1410+ this . dealUnsafeHeader ( config , xhr , config . headers ) ;
13891411
13901412 if ( config . timeout ) {
13911413 xhr . timeout = config . timeout ;
0 commit comments