1-
1+ // resource load use no-cors mode default
2+ // html load must use cors
23export const $fetch = ( url : string , init : RequestInit = { } ) => {
34 return fetch ( url , Object . assign ( { mode : 'no-cors' } , init ) )
4- . then ( r => {
5- console . log ( r )
6- return r . text ( )
7- } )
5+ . then ( r => r . text ( ) )
86}
97
8+ // like './images/xxx' or '../images'
109export const isRelativeURL = ( path : string ) => {
1110 return path . startsWith ( './' ) || path . startsWith ( '..' )
1211}
1312
13+
1414export const makeResourceReg = ( str : string ) : RegExp => {
1515 return {
1616 script : / \< s c r i p t \s + \S ? s r c \= \" ( [ ^ " ] * ) \" / g,
1717 style : / \< l i n k \s + \S ? \s ? h r e f \= \" ( [ ^ " ] * .c s s ) \" / ,
1818 } [ str ]
1919}
20+
21+ // find scripts and styles in html string
2022export const filterResources = ( source : string , type : string ) : string [ ] => {
2123 const reg : RegExp = makeResourceReg ( type ) , arr : string [ ] = [ ]
2224 let result : string [ ] , num = 10
@@ -27,3 +29,17 @@ export const filterResources = (source: string, type: string): string[] => {
2729 }
2830 return arr
2931}
32+
33+ export const listenImageLoad = ( images : HTMLImageElement [ ] , done : ( url : string ) => void )
34+ : void => {
35+ const isCompleted = ( imgs : HTMLImageElement [ ] ) => ! imgs . length
36+ const timer : number = window . setInterval ( ( ) => {
37+ images = images . map ( img => {
38+ if ( ! img . complete ) return img
39+ done ( img . src )
40+ return null
41+ } )
42+ . filter ( v => ! ! v )
43+ isCompleted ( images ) && clearInterval ( timer )
44+ } , 300 )
45+ }
0 commit comments