@@ -422,19 +422,22 @@ export class BackgroundGrant {
422422 if ( api == undefined ) {
423423 return resolve ( undefined ) ;
424424 }
425- api . apply ( this , [ grant , postMessage ] ) . then ( ( result : any ) => {
426- if ( grant . value == 'CAT_runComplete' || ( grant . value == 'CAT_setRunError' && grant . params [ 0 ] ) ) {
427- //执行完毕,释放资源
428- BackgroundGrant . freedCallback . forEach ( v => {
429- v ( grant . id , grant . tabId ) ;
430- } ) ;
431- }
432- resolve ( result ) ;
433- } ) . catch ( ( e : string ) => {
434- grant . error = 'GM_ERROR' ;
435- grant . errorMsg = e ;
436- resolve ( grant ) ;
437- } ) ;
425+ const ret = api . apply ( this , [ grant , postMessage ] ) ;
426+ if ( ret instanceof Promise ) {
427+ ret . then ( ( result : any ) => {
428+ if ( grant . value == 'CAT_runComplete' || ( grant . value == 'CAT_setRunError' && grant . params [ 0 ] ) ) {
429+ //后台脚本执行完毕,释放资源
430+ BackgroundGrant . freedCallback . forEach ( v => {
431+ v ( grant . id , grant . tabId ) ;
432+ } ) ;
433+ }
434+ resolve ( result ) ;
435+ } ) . catch ( ( e : string ) => {
436+ grant . error = 'GM_ERROR' ;
437+ grant . errorMsg = e ;
438+ resolve ( grant ) ;
439+ } ) ;
440+ }
438441 } ) ;
439442 } ) ;
440443
@@ -592,6 +595,15 @@ export class BackgroundGrant {
592595 } ) ;
593596 xhr . send ( data ) ;
594597 }
598+ } else if ( config . dataType == 'Blob' ) {
599+ const handler = async ( ) => {
600+ if ( ! config . data ) {
601+ return reject ( 'data is null' ) ;
602+ }
603+ const resp = await ( await fetch ( < string > config . data ) ) . blob ( ) ;
604+ xhr . send ( resp ) ;
605+ }
606+ void handler ( ) ;
595607 } else {
596608 xhr . send ( < string > config . data ) ;
597609 }
@@ -741,7 +753,7 @@ export class BackgroundGrant {
741753 @BackgroundGrant . GMFunction ( {
742754 listener : ( ) => {
743755 chrome . tabs . onRemoved . addListener ( tabId => {
744- const tab = BackgroundGrant . tabMap . get ( tabId ) ;
756+ const tab = < [ Grant , IPostMessage ] > BackgroundGrant . tabMap . get ( tabId ) ;
745757 if ( tab ) {
746758 tab [ 0 ] . data = { type : 'close' }
747759 tab [ 1 ] . postMessage ( tab [ 0 ] ) ;
@@ -775,36 +787,45 @@ export class BackgroundGrant {
775787
776788 @BackgroundGrant . GMFunction ( {
777789 listener : ( ) => {
778- chrome . notifications . onClosed . addListener ( async ( id , user ) => {
779- const ret = await App . Cache . get ( 'GM_notification:' + id ) ;
780- if ( ret ) {
781- const [ grant , post ] = ret ;
782- grant . data = { type : 'done' , id : id , user : user } ;
783- post . postMessage ( grant ) ;
784- App . Cache . del ( 'GM_notification:' + id ) ;
790+ chrome . notifications . onClosed . addListener ( ( id , user ) => {
791+ const handler = async ( ) => {
792+ const ret = await App . Cache . get ( 'GM_notification:' + id ) ;
793+ if ( ret ) {
794+ const [ grant , post ] = < [ Grant , IPostMessage ] > ret ;
795+ grant . data = { type : 'done' , id : id , user : user } ;
796+ post . postMessage ( grant ) ;
797+ void App . Cache . del ( 'GM_notification:' + id ) ;
798+ }
785799 }
800+ void handler ( ) ;
786801 } ) ;
787- chrome . notifications . onClicked . addListener ( async ( id ) => {
788- const ret = await App . Cache . get ( 'GM_notification:' + id ) ;
789- if ( ret ) {
790- const [ grant , post ] = ret ;
791- grant . data = { type : 'click' , id : id , index : undefined } ;
792- post . postMessage ( grant ) ;
793- grant . data = { type : 'done' , id : id , user : true } ;
794- post . postMessage ( grant ) ;
795- App . Cache . del ( 'GM_notification:' + id ) ;
802+ chrome . notifications . onClicked . addListener ( ( id ) => {
803+ const handler = async ( ) => {
804+ const ret = await App . Cache . get ( 'GM_notification:' + id ) ;
805+ if ( ret ) {
806+ const [ grant , post ] = < [ Grant , IPostMessage ] > ret ;
807+ grant . data = { type : 'click' , id : id , index : undefined } ;
808+ post . postMessage ( grant ) ;
809+ grant . data = { type : 'done' , id : id , user : true } ;
810+ post . postMessage ( grant ) ;
811+ void App . Cache . del ( 'GM_notification:' + id ) ;
812+ }
796813 }
814+ void handler ( ) ;
797815 } ) ;
798- chrome . notifications . onButtonClicked . addListener ( async ( id , buttonIndex ) => {
799- const ret = await App . Cache . get ( 'GM_notification:' + id ) ;
800- if ( ret ) {
801- const [ grant , post ] = ret ;
802- grant . data = { type : 'click' , id : id , index : buttonIndex } ;
803- post . postMessage ( grant ) ;
804- grant . data = { type : 'done' , id : id , user : true } ;
805- post . postMessage ( grant ) ;
806- App . Cache . del ( 'GM_notification:' + id ) ;
816+ chrome . notifications . onButtonClicked . addListener ( ( id , buttonIndex ) => {
817+ const handler = async ( ) => {
818+ const ret = await App . Cache . get ( 'GM_notification:' + id ) ;
819+ if ( ret ) {
820+ const [ grant , post ] = < [ Grant , IPostMessage ] > ret ;
821+ grant . data = { type : 'click' , id : id , index : buttonIndex } ;
822+ post . postMessage ( grant ) ;
823+ grant . data = { type : 'done' , id : id , user : true } ;
824+ post . postMessage ( grant ) ;
825+ void App . Cache . del ( 'GM_notification:' + id ) ;
826+ }
807827 }
828+ void handler ( ) ;
808829 } ) ;
809830 }
810831 } )
@@ -827,15 +848,15 @@ export class BackgroundGrant {
827848 }
828849
829850 chrome . notifications . create ( options , ( notificationId ) => {
830- App . Cache . set ( 'GM_notification:' + notificationId , [ grant , post ] ) ;
851+ void App . Cache . set ( 'GM_notification:' + notificationId , [ grant , post ] ) ;
831852 grant . data = { type : 'create' , id : notificationId } ;
832853 post . postMessage ( grant ) ;
833854 if ( details . timeout ) {
834855 setTimeout ( ( ) => {
835856 chrome . notifications . clear ( notificationId ) ;
836857 grant . data = { type : 'done' , id : notificationId , user : false } ;
837858 post . postMessage ( grant ) ;
838- App . Cache . del ( 'GM_notification:' + notificationId ) ;
859+ void App . Cache . del ( 'GM_notification:' + notificationId ) ;
839860 } , details . timeout ) ;
840861 }
841862 } ) ;
@@ -845,18 +866,17 @@ export class BackgroundGrant {
845866
846867 @BackgroundGrant . GMFunction ( )
847868 protected GM_closeNotification ( grant : Grant ) : Promise < any > {
848- return new Promise ( async resolve => {
849- chrome . notifications . clear ( grant . params [ 0 ] ) ;
850-
851- const ret = await App . Cache . get ( 'GM_notification:' + grant . params [ 0 ] ) ;
852- if ( ret ) {
853- const [ grant , post ] = ret ;
854- grant . data = { type : 'done' , id : grant . params [ 0 ] , user : false } ;
855- post . postMessage ( grant ) ;
856- App . Cache . del ( 'GM_notification:' + grant . params [ 0 ] ) ;
857- }
858-
859- return resolve ( undefined ) ;
869+ return new Promise ( resolve => {
870+ chrome . notifications . clear ( < string > grant . params [ 0 ] ) ;
871+ void App . Cache . get ( 'GM_notification:' + < string > grant . params [ 0 ] ) . then ( ret => {
872+ if ( ret ) {
873+ const [ grant , post ] = < [ Grant , IPostMessage ] > ret ;
874+ grant . data = { type : 'done' , id : grant . params [ 0 ] , user : false } ;
875+ post . postMessage ( grant ) ;
876+ void App . Cache . del ( 'GM_notification:' + < string > grant . params [ 0 ] ) ;
877+ }
878+ return resolve ( undefined ) ;
879+ } ) ;
860880 } ) ;
861881 }
862882
0 commit comments