import { Client } from "./message" ; import { AppName , Application } from "../application" ; import { SystemConfig } from "@App/config" ; export type RequestCallback = ( body : any ) => void ; export type ErrorCallback = ( ) => void ; export interface RequestInfo extends RequestInit { url ? : string ; success ? : RequestCallback ; error ? : ErrorCallback ; json ? : boolean ; } export class HttpUtils { public static Request ( info ) { if ( Application . App . IsBackend ) { fetch ( info . url , info ) . then ( body => { if ( info . json ) { return body . json ( ) ; } else { return body . text ( ) ; } } ) . then ( body => { info . success && info . success ( body ) ; } ) . catch ( ( ) => { info . error && info . error ( ) ; } ) ; return ; } HttpUtils . crossDomainRequest ( info ) ; } private static errorCode ( ret ) { if ( ! ret . code ) { return false ; } switch ( ret . code ) { case - 1 : { Application . App . log . Info ( ret . msg ) ; break ; } case - 2 : { Application . App . log . Warn ( ret . msg ) ; break ; } case 1 : { Application . App . log . Info ( ret . msg ) ; return false ; } default : { return false ; } } return true ; } private static crossDomainRequest ( info ) { if ( window . hasOwnProperty ( 'GM_xmlhttpRequest' ) ) { ( < any > info ) . data = info . body ; ( < any > info ) . onreadystatechange = function ( response ) { if ( response . readyState == 4 ) { if ( response . status == 200 ) { if ( info . json ) { let ret = JSON . parse ( response . responseText ) ; if ( HttpUtils . errorCode ( ret ) ) { info . error && info . error ( ) ; return ; } info . success && info . success ( ret ) ; } else { info . success && info . success ( response . responseText ) ; } } else { info . error && info . error ( ) ; } } } ; ( < any > window ) . GM_xmlhttpRequest ( info ) ; } else { let client = Application . App . Client ; client . Recv ( ( data ) => { if ( data . code == 0 ) { if ( info . json ) { if ( HttpUtils . errorCode ( data . body ) ) { info . error && info . error ( ) ; return ; } } info . success && info . success ( data . body ) ; } else { info . error && info . error ( ) ; } } ) ; client . Send ( { type : "GM_xmlhttpRequest" , info : { url : info . url , method : info . method , json : info . json , body : info . body , headers : info . headers , } } ) ; } } public static HttpGet ( url , info ) { info . url = url ; this . Request ( info ) ; } public static HttpPost ( url , body , info ) { info . url = url ; info . body = body ; if ( ! info . headers ) { info . headers = { } ; } if ( ! ( < any > info . headers ) [ "Content-Type" ] ) { ( < any > info . headers ) [ "Content-Type" ] = "application/x-www-form-urlencoded" ; } info . method = "POST" ; this . Request ( info ) ; } public static SendRequest ( client , data ) { if ( ! ( data . info as RequestInfo ) ) { return ; } let info = < RequestInfo > data . info ; if ( Application . App . IsBackend ) { info . success = ( body ) => { client . Send ( { body : body , code : 0 } ) ; } ; info . error = ( ) => { client . Send ( { code : - 1 } ) ; } ; HttpUtils . Request ( info ) ; } else { let extClient = Application . App . Client ; extClient . Send ( { type : "GM_xmlhttpRequest" , info : info } ) ; extClient . Recv ( ( data ) => { client . Send ( data ) ; } ) ; } } } param doc param url constructor export function Injected ( doc , source ) { let temp = doc . createElement ( 'script' ) ; temp . setAttribute ( 'type' , 'text/javascript' ) ; temp . innerHTML = source ; temp . className = "injected-js" ; doc . documentElement . appendChild ( temp ) ; return temp ; } param doc param url constructor export function InjectedBySrc ( doc , source ) { let temp = doc . createElement ( 'script' ) ; temp . setAttribute ( 'type' , 'text/javascript' ) ; temp . src = source ; temp . className = "injected-js" ; doc . documentElement . appendChild ( temp ) ; return temp ; } export function syncGetChromeStorageLocal ( key ) { return new Promise < any > ( resolve => ( chrome . storage . local . get ( key , ( value ) => { resolve ( < any > value [ < string > key ] ) ; } ) ) ) ; } export function syncSetChromeStorageLocal ( key , value ) { let tmp = { } ; ( < any > tmp ) [ key ] = value ; return new Promise < any > ( resolve => ( chrome . storage . local . set ( tmp , ( ) => { resolve ( ) ; } ) ) ) ; } param doc export function RemoveInjected ( doc ) { let resource = doc . getElementsByClassName ( "injected-js" ) ; for ( let i = 0 ; i < resource . length ; i ++ ) { resource [ i ] . remove ( ) ; } } export function randNumber ( minNum , maxNum ) { return Math . floor ( Math . random ( ) * ( maxNum - minNum + 1 ) + minNum ) ; } param title param description param id export function createBtn ( title , description = "" , className = "" , id = "" ) { let btn = document . createElement ( 'button' ) ; btn . innerText = title ; btn . id = id ; btn . title = description ; btn . className = className ; return btn ; } param { * } url export function get ( url , success ) { let xmlhttp = createRequest ( ) ; xmlhttp . open ( "GET" , url , true ) ; xmlhttp . onreadystatechange = function ( ) { if ( this . readyState == 4 ) { if ( this . status == 200 ) { success && success ( this . responseText , ( < any > this ) . resource ) ; } else { ( < any > xmlhttp ) . errorCallback && ( < any > xmlhttp ) . errorCallback ( this ) ; } } } ; xmlhttp . send ( ) ; return xmlhttp ; } param { * } url param { * } data param { * } json export function post ( url , data , json = true , success ) { let xmlhttp = createRequest ( ) ; xmlhttp . open ( "POST" , url , true ) ; if ( json ) { xmlhttp . setRequestHeader ( "Content-Type" , "application/json" ) ; } else { xmlhttp . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ; } xmlhttp . onreadystatechange = function ( ) { if ( this . readyState == 4 ) { if ( this . status == 200 ) { success && success ( this . responseText ) ; } else { ( < any > xmlhttp ) . errorCallback && ( < any > xmlhttp ) . errorCallback ( this ) ; } } } ; xmlhttp . send ( data ) ; return xmlhttp ; } declare namespace Express { interface XMLHttpRequest { error : any ; } } /**
 * 创建http请求
 */ function createRequest ( ) { let xmlhttp ; if ( window . XMLHttpRequest ) { xmlhttp = new XMLHttpRequest ( ) ; } else { xmlhttp = new ActiveXObject ( "Microsoft.XMLHTTP" ) ; } ( < any > xmlhttp ) . error = function ( callback ) { ( < any > xmlhttp ) . errorCallback = callback ; return xmlhttp ; } ; xmlhttp . withCredentials = true ; return xmlhttp ; } export function removeHTMLTag ( html ) { let revHtml = /<.*?>/g ; html = html . replace ( revHtml , '' ) ; html = html . replace ( /(^\s+)|(\s+$)/g , '' ) ; return html ; } param { string } html export function removeHTML ( html ) { let srcReplace = /<img.*?src="(.*?)".*?>/g ; html = html . replace ( srcReplace , '$1' ) ; srcReplace = /(<iframe.+?>)\s+?(<\/iframe>)/g ; html = html . replace ( srcReplace , '$1$2' ) ; srcReplace = /<(iframe|a).*?(src|href)="(.*?)".*?>(.*?)<\/(iframe|a)>/g ; html = html . replace ( srcReplace , '$3$4' ) ; let revHtml = /<.*?>/g ; html = html . replace ( revHtml , '' ) ; html = html . replace ( /(^\s+)|(\s+$)/g , '' ) ; html = dealSymbol ( html ) ; return html . replace ( /&nbsp;/g , ' ' ) . replace ( /&quot;/g , "\"" ) . replace ( /&gt;/g , ">" ) . replace ( /&lt;/g , "<" ) . replace ( /&amp;/g , '&' ) . trim ( ) ; } param topic function dealSymbol ( topic ) { topic = topic . replace ( /，/g , ',' ) ; topic = topic . replace ( /（/g , '(' ) ; topic = topic . replace ( /）/g , ')' ) ; topic = topic . replace ( /？/g , '?' ) ; topic = topic . replace ( /：/g , ':' ) ; topic = topic . replace ( /。/g , '.' ) ; topic = topic . replace ( /[“”]/g , '"' ) ; return topic ; } param str param left param right export function substrex ( str , left , right ) { var leftPos = str . indexOf ( left ) + left . length ; var rightPos = str . indexOf ( right , leftPos ) ; return str . substring ( leftPos , rightPos ) ; } export function dealHotVersion ( hotversion ) { hotversion = hotversion . substring ( 0 , hotversion . indexOf ( "." ) + 1 ) + hotversion . substring ( hotversion . indexOf ( "." ) + 1 ) . replace ( "." , "" ) ; return Number ( hotversion ) ; } export function protocolPrompt ( content , key , keyword ? ) { keyword = keyword || "yes" ; if ( localStorage [ key ] == undefined || localStorage [ key ] != 1 ) { let msg = prompt ( content + "\n如果以后不想再弹出本对话框并且同意请在下方填写\"" + keyword + "\"" ) ; if ( msg === null ) return false ; if ( keyword != msg ) { return false ; } localStorage [ key ] = 1 ; } return true ; } export function getImageBase64 ( img , ext ) { var canvas = document . createElement ( "canvas" ) ; canvas . width = img . width ; canvas . height = img . height ; var ctx = canvas . getContext ( "2d" ) ; ctx . drawImage ( img , 0 , 0 , img . width , img . height ) ; var dataURL = canvas . toDataURL ( "image/" + ext , 0.75 ) ; canvas = null ; return dataURL ; } export function isPhone ( ) { return /Android|iPhone/i . test ( navigator . userAgent ) ; } export interface NotificationOptions { text : string ; title : string ; timeout ? : number ; } export function Noifications ( details ) { if ( window . hasOwnProperty ( "GM_notification" ) ) { ( < any > window ) . GM_notification ( details ) ; } else { let client = Application . App . Client ; client . Send ( { type : "GM_notification" , details : details , } ) ; Application . App . Client . Send ( details ) ; } } export function toBool ( val ) { if ( typeof val == "boolean" ) { return val ; } return val == "true" ; } export function boolToString ( val ) { if ( val ) { return "true" ; } return "false" ; } export function UntrustedClick ( el ) { if ( window . CAT_click != undefined ) { CAT_click ( el ) ; return true ; } let untrusted = new MouseEvent ( "click" , { "clientX" : 10086 } ) ; if ( ! untrusted . isTrusted ) { Application . App . log . Warn ( "扩展执行错误" ) ; return false ; } return el . dispatchEvent ( untrusted ) ; } export function Sleep ( timeout ? ) { return new Promise < any > ( resolve => { setTimeout ( function ( ) { resolve ( undefined ) ; } , timeout ) ; } ) ; }