@@ -39,16 +39,23 @@ export class GMContext {
3939 if ( param . listener ) {
4040 param . listener ( ) ;
4141 }
42+ if ( key === "GMdotXmlHttpRequest" ) {
43+ GMContext . apis . set ( "GM.xmlHttpRequest" , {
44+ api : descriptor . value ,
45+ param,
46+ } ) ;
47+ return ;
48+ }
4249 GMContext . apis . set ( key , {
4350 api : descriptor . value ,
4451 param,
4552 } ) ;
4653 // 兼容GM.*
47- let dot = key . replace ( "_" , "." ) ;
54+ const dot = key . replace ( "_" , "." ) ;
4855 if ( dot !== key ) {
4956 // 特殊处理GM.xmlHttpRequest
5057 if ( dot === "GM.xmlhttpRequest" ) {
51- dot = "GM.xmlHttpRequest" ;
58+ return ;
5259 }
5360 GMContext . apis . set ( dot , {
5461 api : descriptor . value ,
@@ -263,6 +270,38 @@ export default class GMApi {
263270 return this . message . syncSend ( "CAT_createBlobUrl" , blob ) ;
264271 }
265272
273+ // 用于脚本跨域请求,需要@connect domain指定允许的域名
274+ @GMContext . API ( {
275+ depend : [
276+ "CAT_fetchBlob" ,
277+ "CAT_createBlobUrl" ,
278+ "CAT_fetchDocument" ,
279+ "GM_xmlhttpRequest" ,
280+ ] ,
281+ } )
282+ GMdotXmlHttpRequest ( details : GMTypes . XHRDetails ) {
283+ let abort : any ;
284+ const ret = new Promise ( ( resolve , reject ) => {
285+ const oldOnload = details . onload ;
286+ details . onload = ( data ) => {
287+ resolve ( data ) ;
288+ oldOnload && oldOnload ( data ) ;
289+ } ;
290+ const oldOnerror = details . onerror ;
291+ details . onerror = ( data ) => {
292+ reject ( data ) ;
293+ oldOnerror && oldOnerror ( data ) ;
294+ } ;
295+ // @ts -ignore
296+ abort = this . GM_xmlhttpRequest ( details ) ;
297+ } ) ;
298+ if ( abort && abort . abort ) {
299+ // @ts -ignore
300+ ret . abort = abort . abort ;
301+ }
302+ return ret ;
303+ }
304+
266305 // 用于脚本跨域请求,需要@connect domain指定允许的域名
267306 @GMContext . API ( {
268307 depend : [ "CAT_fetchBlob" , "CAT_createBlobUrl" , "CAT_fetchDocument" ] ,
0 commit comments