@@ -12,13 +12,15 @@ const loader: ILoader = {
1212 isReady : false ,
1313 readys : [ ] ,
1414 scriptPath : '' ,
15+ head : undefined ,
1516
1617 init : function ( ) {
1718 /** --- 文档装载完毕后需要执行的函数 --- */
1819 let run = async ( ) : Promise < void > => {
20+ this . head = document . getElementsByTagName ( 'head' ) [ 0 ] ;
1921 // --- 判断 fetch 是否存在 ---
2022 if ( typeof fetch !== 'function' ) {
21- await this . loadScript ( document . getElementsByTagName ( 'head' ) [ 0 ] , 'https://cdn.jsdelivr.net/npm/[email protected] /fetch.min.js' ) ; 23+ await this . loadScript ( 'https://cdn.jsdelivr.net/npm/[email protected] /fetch.min.js' ) ; 2224 }
2325 this . isReady = true ;
2426 for ( let func of this . readys ) {
@@ -541,8 +543,17 @@ const loader: ILoader = {
541543 return list ;
542544 } ,
543545
544- loadScript : function ( el : HTMLElement , url : string ) : Promise < boolean > {
545- return new Promise ( function ( resolve ) {
546+ loadScript : function ( url : string , el ?: HTMLElement ) : Promise < boolean > {
547+ return new Promise ( ( resolve ) => {
548+ if ( ! el ) {
549+ if ( this . head ) {
550+ el = this . head ;
551+ }
552+ else {
553+ el = document . getElementsByTagName ( 'head' ) [ 0 ] ;
554+ this . head = el ;
555+ }
556+ }
546557 let script = document . createElement ( 'script' ) ;
547558 script . addEventListener ( 'load' , function ( ) {
548559 resolve ( true ) ;
@@ -555,13 +566,14 @@ const loader: ILoader = {
555566 } ) ;
556567 } ,
557568
558- loadScripts : function ( el : HTMLElement , urls : string [ ] , opt : {
569+ loadScripts : function ( urls : string [ ] , opt : {
559570 'loaded' ?: ( url : string , state : number ) => void ;
571+ 'el' ?: HTMLElement ;
560572 } = { } ) : Promise < void > {
561573 return new Promise ( ( resolve ) => {
562574 let count = 0 ;
563575 for ( let url of urls ) {
564- this . loadScript ( el , url ) . then ( function ( res ) {
576+ this . loadScript ( url , opt . el ) . then ( function ( res ) {
565577 ++ count ;
566578 if ( res ) {
567579 opt . loaded ?.( url , 1 ) ;
0 commit comments