@@ -508,6 +508,24 @@ export class BackgroundGrant {
508508 return respond ;
509509 }
510510
511+ request = new Map < number , XMLHttpRequest > ( ) ;
512+ requestId = 0 ;
513+
514+ @BackgroundGrant . GMFunction ( {
515+ alias : [ 'GM_xmlhttpRequest' ]
516+ } )
517+ protected CAT_abortXhr ( grant : Grant ) : Promise < void > {
518+ return new Promise ( resolve => {
519+ const id = < number > grant . params [ 0 ] ;
520+ const xhr = this . request . get ( id ) ;
521+ if ( xhr ) {
522+ xhr . abort ( ) ;
523+ this . request . delete ( id ) ;
524+ }
525+ resolve ( ) ;
526+ } ) ;
527+ }
528+
511529 //TODO:按照tampermonkey文档实现
512530 @BackgroundGrant . GMFunction ( {
513531 confirm : ( grant : Grant , script : Script ) => {
@@ -550,6 +568,11 @@ export class BackgroundGrant {
550568 const config = < GMSend . XHRDetails > grant . params [ 0 ] ;
551569
552570 const xhr = new XMLHttpRequest ( ) ;
571+
572+ this . request . set ( ++ this . requestId , xhr ) ;
573+ grant . data = { type : 'requestId' , data : this . requestId } ;
574+ post . postMessage ( grant ) ;
575+
553576 xhr . open ( config . method || 'GET' , config . url , true , config . user || '' , config . password || '' ) ;
554577 config . overrideMimeType && xhr . overrideMimeType ( config . overrideMimeType ) ;
555578 if ( config . responseType != 'json' ) {
@@ -564,7 +587,11 @@ export class BackgroundGrant {
564587 }
565588 }
566589 grant . data = { type : event , data : respond } ;
567- post . postMessage ( grant ) ;
590+ try {
591+ post . postMessage ( grant ) ;
592+ } catch ( e ) {
593+ xhr . abort ( ) ;
594+ }
568595 }
569596 xhr . onload = ( ) => {
570597 deal ( 'onload' ) ;
@@ -682,7 +709,7 @@ export class BackgroundGrant {
682709 } ) ;
683710 }
684711 } )
685- protected GM_cookie ( grant : Grant , post : IPostMessage ) : Promise < any > {
712+ protected GM_cookie ( grant : Grant ) : Promise < any > {
686713 return new Promise ( ( resolve , reject ) => {
687714 const param = grant . params ;
688715 if ( param . length != 2 ) {
@@ -795,14 +822,14 @@ export class BackgroundGrant {
795822 active : param . active || false ,
796823 } , tab => {
797824 resolve ( { type : 'tabid' , tabId : tab . id } ) ;
798- BackgroundGrant . tabMap . set ( tab . id ! , [ grant , post ] ) ;
825+ BackgroundGrant . tabMap . set ( < number > tab . id , [ grant , post ] ) ;
799826 } ) ;
800827 } ) ;
801828 }
802829
803830 // 隐藏函数
804831 @BackgroundGrant . GMFunction ( { default : true } )
805- protected GM_closeInTab ( grant : Grant , post : IPostMessage ) : Promise < any > {
832+ protected GM_closeInTab ( grant : Grant ) : Promise < any > {
806833 return new Promise ( resolve => {
807834 chrome . tabs . remove ( < number > grant . params [ 0 ] ) ;
808835 resolve ( undefined ) ;
@@ -927,31 +954,31 @@ export class BackgroundGrant {
927954 }
928955
929956 @BackgroundGrant . GMFunction ( { default : true , background : true } )
930- protected CAT_setLastRuntime ( grant : Grant , post : IPostMessage ) : Promise < any > {
957+ protected CAT_setLastRuntime ( grant : Grant ) : Promise < any > {
931958 return new Promise ( resolve => {
932959 void this . scriptMgr . setLastRuntime ( grant . id , < number > grant . params [ 0 ] ) ;
933960 return resolve ( undefined ) ;
934961 } ) ;
935962 }
936963
937964 @BackgroundGrant . GMFunction ( { default : true , background : true } )
938- protected CAT_setRunError ( grant : Grant , post : IPostMessage ) : Promise < any > {
965+ protected CAT_setRunError ( grant : Grant ) : Promise < any > {
939966 return new Promise ( resolve => {
940967 void this . scriptMgr . setRunError ( grant . id , < string > grant . params [ 0 ] , < number > grant . params [ 1 ] ) ;
941968 return resolve ( undefined ) ;
942969 } ) ;
943970 }
944971
945972 @BackgroundGrant . GMFunction ( { default : true , background : true } )
946- protected CAT_runComplete ( grant : Grant , post : IPostMessage ) : Promise < any > {
973+ protected CAT_runComplete ( grant : Grant ) : Promise < any > {
947974 return new Promise ( resolve => {
948975 void this . scriptMgr . setRunComplete ( grant . id ) ;
949976 return resolve ( undefined ) ;
950977 } ) ;
951978 }
952979
953980 @BackgroundGrant . GMFunction ( { default : true } )
954- protected GM_log ( grant : Grant , post : IPostMessage ) : Promise < any > {
981+ protected GM_log ( grant : Grant ) : Promise < any > {
955982 return new Promise ( ( resolve , reject ) => {
956983 if ( grant . params . length == 0 ) {
957984 return reject ( 'param is null' ) ;
@@ -990,13 +1017,12 @@ export class BackgroundGrant {
9901017 } else {
9911018 model . value = value ;
9921019 }
993-
994- if ( value === undefined ) {
1020+ if ( value === undefined || value === null ) {
1021+ model . value = undefined ;
9951022 void this . valueModel . delete ( model . id ) ;
9961023 AppEvent . trigger ( ScriptValueChange , { model, tabid : grant . tabId } ) ;
9971024 return resolve ( undefined ) ;
9981025 }
999-
10001026 void this . valueModel . save ( model ) ;
10011027 AppEvent . trigger ( ScriptValueChange , { model, tabid : grant . tabId } ) ;
10021028 resolve ( undefined ) ;
@@ -1056,7 +1082,7 @@ export class BackgroundGrant {
10561082 BackgroundGrant . freedProxy ( id ) ;
10571083 }
10581084 } )
1059- protected CAT_setProxy ( grant : Grant , post : IPostMessage ) : Promise < any > {
1085+ protected CAT_setProxy ( grant : Grant ) : Promise < any > {
10601086 return new Promise ( resolve => {
10611087 BackgroundGrant . proxyRule . set ( grant . id , < CAT_Types . ProxyRule [ ] > grant . params [ 0 ] ) ;
10621088 App . Log . Debug ( 'background' , 'enable proxy' , grant . name ) ;
@@ -1073,7 +1099,7 @@ export class BackgroundGrant {
10731099 }
10741100
10751101 @BackgroundGrant . GMFunction ( { background : true } )
1076- protected CAT_clearProxy ( grant : Grant , post : IPostMessage ) : Promise < any > {
1102+ protected CAT_clearProxy ( grant : Grant ) : Promise < any > {
10771103 return new Promise ( resolve => {
10781104 BackgroundGrant . freedProxy ( grant . id ) ;
10791105 resolve ( undefined ) ;
@@ -1100,15 +1126,13 @@ export class BackgroundGrant {
11001126 y : param [ 1 ] ,
11011127 button : 'left' ,
11021128 clickCount : 1
1103- } , ( result ) => {
1129+ } , ( ) => {
11041130 chrome . debugger . sendCommand ( target , 'Input.dispatchMouseEvent' , {
11051131 type : 'mouseReleased' ,
11061132 x : param [ 0 ] ,
11071133 y : param [ 1 ] ,
11081134 button : 'left' ,
11091135 clickCount : 1
1110- } , ( result ) => {
1111- console . log ( result ) ;
11121136 } ) ;
11131137 } ) ;
11141138 } else {
@@ -1119,15 +1143,13 @@ export class BackgroundGrant {
11191143 y : param [ 1 ] ,
11201144 button : 'left' ,
11211145 clickCount : 1
1122- } , ( result ) => {
1146+ } , ( ) => {
11231147 chrome . debugger . sendCommand ( target , 'Input.dispatchMouseEvent' , {
11241148 type : 'mouseReleased' ,
11251149 x : param [ 0 ] ,
11261150 y : param [ 1 ] ,
11271151 button : 'left' ,
11281152 clickCount : 1
1129- } , ( result ) => {
1130- console . log ( result ) ;
11311153 } ) ;
11321154 } ) ;
11331155 } ) ;
@@ -1154,7 +1176,7 @@ export class BackgroundGrant {
11541176 } )
11551177 }
11561178 } )
1157- public GM_setClipboard ( grant : Grant , post : IPostMessage ) : Promise < any > {
1179+ public GM_setClipboard ( grant : Grant ) : Promise < any > {
11581180 return new Promise ( resolve => {
11591181 BackgroundGrant . clipboardData = {
11601182 type : grant . params [ 1 ] ,
0 commit comments