const abstractImagesFromHTML = ( str ) => { let imgReg = /\<img(.*?)>/g ; let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i ; let result = [ ] ; let imgs = str . match ( imgReg ) ; if ( imgs && imgs . length > 0 ) { imgs . map ( img => { let _img = img . match ( srcReg ) ; if ( _img && _img [ 1 ] ) result . push ( _img [ 1 ] ) ; } ) ; } return result ; } ; function randomString ( len ) { len = len || 32 ; var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' ; var maxPos = $chars . length ; var pwd = '' ; for ( let i = 0 ; i < len ; i ++ ) { pwd += $chars . charAt ( Math . floor ( Math . random ( ) * maxPos ) ) ; } return pwd ; } function music163 ( html ) { let re = /(http:\/\/music\.163\.com|https:\/\/music\.163\.com|music\.163\.com)\/(.*?)(?=\s|http|https|\)|\>|\]|\}|\<|$)/gi ; let musics = html . match ( re ) ; if ( musics && musics . length > 0 ) { musics . map ( str => { let type = - 1 , id ; if ( str . indexOf ( '/song?' ) != - 1 ) { type = 2 ; } else if ( str . indexOf ( '/playlist?' ) != - 1 ) { type = 0 ; } else if ( str . indexOf ( '/album?' ) != - 1 ) { type = 1 ; } else if ( str . indexOf ( '/dj?' ) != - 1 ) { type = 3 ; } else if ( str . indexOf ( '/radio/?' ) != - 1 ) { type = 4 ; } try { str . split ( '?' ) [ 1 ] . split ( '&' ) . map ( s => { let arr = s . split ( '=' ) ; if ( arr [ 0 ] == 'id' ) id = arr [ 1 ] ; } ) ; } catch ( err ) { console . log ( err ) ; } if ( type != - 1 && id ) { if ( type == 2 ) { let url = `//music.163.com/outchain/player?type=2&id=${ id }&auto=0&height=66` ; html = html . replace ( str , `<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="${ url }"></iframe>` ) ; } else if ( type == 3 ) { let url = `//music.163.com/outchain/player?type=3&id=${ id }&auto=1&height=66` ; html = html . replace ( str , `<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="${ url }"></iframe>` ) ; } else if ( type == 4 ) { let url = `//music.163.com/outchain/player?type=4&id=${ id }&auto=1&height=430` ; html = html . replace ( str , `<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=450 src="${ url }"></iframe>` ) ; } else { let url = `//music.163.com/outchain/player?type=${ type }&id=${ id }&height=430` ; html = html . replace ( str , `<iframe type="music" src="${ url }" height="430"></iframe>` ) ; } } } ) ; } return html ; } /**
 * http://v.youku.com/v_show/id_XMzkyMzA5MDg0MA==.html?spm=a2hww.11359951.m_26673_c_32078.5~5!3~5!3~5~5~A
* <iframe height=498 width=510 src='http://player.youku.com/embed/XMzkyMzA5MDg0MA==' frameborder=0 'allowfullscreen'></iframe>
 */ function youku ( html ) { let re = /(http:\/\/v\.youku\.com|https:\/\/v\.youku\.com|v\.youku\.com)\/v\_show\/id\_(.*?)(?=\s|http|https|\)|\>|\]|\}|\<|$)/gi ; let arr = html . match ( re ) ; if ( ! arr || arr . length == 0 ) return html ; arr . map ( str => { let id ; try { id = str . split ( 'v.youku.com/v_show/id_' ) [ 1 ] . split ( '.' ) [ 0 ] ; id = id . split ( '?' ) [ 0 ] ; } catch ( err ) { console . log ( err ) ; } if ( id ) { let url = `//player.youku.com/embed/${ id }` ; html = html . replace ( str , `<iframe width='100%' src='${ url }' frameborder=0 'allowfullscreen'></iframe>` ) ; } } ) ; return html ; } function bilibili ( html ) { let re = /(https:\/\/www\.bilibili\.com|https:\/\/bilibili\.com|http:\/\/www.bilibili\.com|http:\/\/bilibili\.com|www\.bilibili\.com|bilibili\.com)\/video\/av(.*?)(?=\s|http|https|\)|\>|\]|\}|\<|$)/gi ; let arr = html . match ( re ) ; if ( ! arr || arr . length == 0 ) return html ; arr . map ( str => { let id ; try { id = str . split ( 'bilibili.com/video/av' ) [ 1 ] . split ( '/' ) [ 0 ] ; id = id . split ( '?' ) [ 0 ] ; } catch ( err ) { console . log ( err ) ; } if ( id ) { let url = `//player.bilibili.com/player.html?aid=${ id }` ; html = html . replace ( str , `<iframe src="${ url }" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>` ) ; } } ) ; return html ; } function acfun ( html ) { let re = /(https:\/\/www\.acfun\.cn|https:\/\/acfun\.cn|http:\/\/www.acfun\.cn|http:\/\/acfun\.cn|www\.acfun\.cn|acfun\.cn)\/v\/(.*?)(?=\s|http|https|\)|\>|\]|\}|\<|$)/gi ; let arr = html . match ( re ) ; if ( ! arr || arr . length == 0 ) return html ; arr . map ( str => { let id ; try { id = str . split ( 'acfun.cn/v/' ) [ 1 ] . split ( '/' ) [ 0 ] ; id = id . split ( '?' ) [ 0 ] ; } catch ( err ) { console . log ( err ) ; } if ( id ) { let url = `https://www.acfun.cn/player/${ id }` ; html = html . replace ( str , `<iframe src="${ url }" id="ACFlashPlayer-re"  scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>` ) ; } } ) ; return html ; } /**
 * https://www.youtube.com/watch?v=c_WCKfQCQuk
 * <iframe width="560" height="315" src="https://www.youtube.com/embed/c_WCKfQCQuk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
 */ function youtube ( html ) { let re = /(https:\/\/www\.youtube\.com|https:\/\/youtube\.com|http:\/\/www.youtube\.com|http:\/\/youtube\.com|www\.youtube\.com|youtube\.com)\/watch\?v\=(.*?)(?=\s|http|https|\)|\>|\]|\}|\<|$)/gi ; let arr = html . match ( re ) ; if ( ! arr || arr . length == 0 ) return html ; arr . map ( str => { let id ; try { str . split ( '?' ) [ 1 ] . split ( '&' ) . map ( item => { let arr = item . split ( '=' ) ; if ( arr && arr [ 0 ] == 'v' ) { id = arr [ 1 ] ; } } ) ; } catch ( err ) { console . log ( err ) ; } if ( id ) { let url = `//www.youtube.com/embed/${ id }` ; html = html . replace ( str , `<iframe width="100%" style="background:#f9f9f9;" src="${ url }" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>` ) ; } } ) ; return html ; } /**
 * https://v.qq.com/x/cover/l19x9qoetxc5rh8/h0028rgy2x5.html(可以匹配)
 * https://v.qq.com/x/cover/i5w51tl7vbl5mid.html(不能匹配)
 * https://v.qq.com/x/page/e0887umbbap.html
 * <iframe frameborder="0" src="https://v.qq.com/txp/iframe/player.html?vid=h0028rgy2x5" allowFullScreen="true"></iframe>
 */ function vqq ( html ) { let re = /(https:\/\/v\.|http:\/\/v\.|v\.)qq\.com\/x\/(page|cover)\/(.*?)(?=\s|http|https|\)|\>|\]|\}|\<|$)/gi ; let arr = html . match ( re ) ; if ( ! arr || arr . length == 0 ) return html ; arr . map ( ( str ) => { let id ; try { let urlArr = str . split ( '?' ) [ 0 ] . split ( '.' ) ; urlArr = urlArr [ urlArr . length - 2 ] . split ( '/' ) ; id = str . split ( '?' ) [ 0 ] . split ( '/' ) . pop ( ) . split ( '.' ) [ 0 ] ; } catch ( err ) { console . log ( err ) ; } if ( id ) { let url = `//v.qq.com/txp/iframe/player.html?vid=${ id }` ; html = html . replace ( str , `<iframe frameborder="0" src="${ url }" allowFullScreen="true"></iframe>` ) ; } } ) ; return html ; } const link = ( str ) => { if ( ! str ) return '' ; str = str . replace ( '&nbsp;' , ' ' ) ; let imgReg = /(<a(.*?)>(.*?)<\/a>|<img(.*?)>|<ifram(.*?)>)/gi ; let aList = [ ] ; let arr = str . match ( imgReg ) ; if ( arr && arr . length > 0 ) { str . match ( imgReg ) . map ( ( item ) => { let id = '#' + randomString ( 18 ) + '#' ; aList . push ( { id , value : item } ) ; str = str . replace ( item , id ) ; } ) ; } let linkReg = /(http:\/\/>http:\/\/|http:\/\/|https:\/\/|www\.|magnet\:\?xt\=)(.*?)(?=\s|http|https|\)|\>|\]|\}|\<|\"|\'|$)/gi ; let links = str . match ( linkReg ) ; if ( links && links . length > 0 ) { links = links . sort ( ( a , b ) => { return b . length - a . length ; } ) ; let _links = [ ] ; links . map ( ( item ) => { let id = '#' + randomString ( 18 ) + '#' ; _links . push ( { id , value : item } ) ; str = str . replace ( item , id ) ; } ) ; _links . map ( item => { let href = item . value . indexOf ( 'http' ) == - 1 ? 'http:' + item . value : item . value ; str = str . replace ( item . id , `<a href=${ href } target="_blank" rel="nofollow">${ item . value }</a>` ) ; } ) ; } if ( aList . length > 0 ) { aList . map ( item => { str = str . replace ( item . id , item . value ) ; } ) ; } return str ; } ; const image = ( html ) => { let imgReg = /\<img(.*?)>/g ; let imgs = html . match ( imgReg ) ; let allImage = abstractImagesFromHTML ( html ) ; allImage . map ( ( item , index ) => { allImage [ index ] = item . split ( '?' ) [ 0 ] ; } ) ; allImage = "['" + allImage . join ( "','" ) + "']" ; if ( imgs && imgs . length > 0 ) { imgs . map ( ( img , index ) => { let _img = img ; _img = _img . replace ( /\?/g , "\\?" ) ; html = html . replace ( new RegExp ( _img , "gm" ) , '<span onclick=\"webPictureViewer(' + allImage + ',' + index + ');\">' + img + '</span>' ) ; } ) ; } return html ; } ; const trimHtml = function ( html ) : string { let imgReg = /\<pre>(.*?)<\/pre>/g ; let preArr = html . match ( imgReg ) ; let aList = [ ] ; if ( preArr && preArr . length > 0 ) { preArr . map ( ( item ) => { let id = '#' + randomString ( 18 ) + '#' ; aList . push ( { id , value : item } ) ; html = html . replace ( item , id ) ; } ) ; } let arr = html . split ( '<p><br></p>' ) ; let trim = function ( arr ) { var newArr = [ ] ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( newArr . indexOf ( arr [ i ] ) == - 1 ) { newArr . push ( arr [ i ] ) ; } } if ( newArr . length > 0 && ! newArr [ 0 ] ) { newArr . splice ( 0 , 1 ) ; } if ( newArr . length > 0 && ! newArr [ newArr . length - 1 ] ) { newArr . splice ( newArr . length - 1 , 1 ) ; } return newArr ; } ; arr = trim ( arr ) ; html = arr . join ( '<p><br></p>' ) ; if ( aList . length > 0 ) { aList . map ( ( item ) => { html = html . replace ( item . id , item . value ) ; } ) ; } return html ; } ; export default ( html ) => { if ( ! html ) return '' ; html = html . replace ( /([\r\n])/g , "<br>" ) ; html = music163 ( html ) ; html = youku ( html ) ; html = bilibili ( html ) ; html = acfun ( html ) ; html = youtube ( html ) ; html = vqq ( html ) ; html = link ( html ) ; html = image ( html ) ; return html ; } ;