55
66function loadWidget ( config ) {
77 let { waifuPath, apiPath, cdnPath } = config ;
8- let useCDN = false ;
8+ let useCDN = false , modelList ;
99 if ( typeof cdnPath === "string" ) {
1010 useCDN = true ;
1111 if ( ! cdnPath . endsWith ( "/" ) ) cdnPath += "/" ;
@@ -100,10 +100,13 @@ function loadWidget(config) {
100100 }
101101 showMessage ( text , 7000 , 8 ) ;
102102 } ) ( ) ;
103+ function randomSelection ( obj ) {
104+ return Array . isArray ( obj ) ? obj [ Math . floor ( Math . random ( ) * obj . length ) ] : obj ;
105+ }
103106 // 检测用户活动状态,并在空闲时显示消息
104107 var userAction = false ,
105- userActionTimer = null ,
106- messageTimer = null ,
108+ userActionTimer ,
109+ messageTimer ,
107110 messageArray = [ "好久不见,日子过得好快呢……" , "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~" , "嗨~快来逗我玩吧!" , "拿小拳拳锤你胸口!" , "记得把小家加入 Adblock 白名单哦!" ] ;
108111 window . addEventListener ( "mousemove" , ( ) => userAction = true ) ;
109112 window . addEventListener ( "keydown" , ( ) => userAction = true ) ;
@@ -114,7 +117,7 @@ function loadWidget(config) {
114117 userActionTimer = null ;
115118 } else if ( ! userActionTimer ) {
116119 userActionTimer = setInterval ( ( ) => {
117- showMessage ( messageArray [ Math . floor ( Math . random ( ) * messageArray . length ) ] , 6000 , 9 ) ;
120+ showMessage ( randomSelection ( messageArray ) , 6000 , 9 ) ;
118121 } , 20000 ) ;
119122 }
120123 } , 1000 ) ;
@@ -139,7 +142,7 @@ function loadWidget(config) {
139142 clearTimeout ( messageTimer ) ;
140143 messageTimer = null ;
141144 }
142- if ( Array . isArray ( text ) ) text = text [ Math . floor ( Math . random ( ) * text . length ) ] ;
145+ text = randomSelection ( text ) ;
143146 sessionStorage . setItem ( "waifu-text" , priority ) ;
144147 var tips = document . getElementById ( "waifu-tips" ) ;
145148 tips . innerHTML = text ;
@@ -166,15 +169,15 @@ function loadWidget(config) {
166169 result . mouseover . forEach ( tips => {
167170 window . addEventListener ( "mouseover" , event => {
168171 if ( ! event . target . matches ( tips . selector ) ) return ;
169- var text = Array . isArray ( tips . text ) ? tips . text [ Math . floor ( Math . random ( ) * tips . text . length ) ] : tips . text ;
172+ var text = randomSelection ( tips . text ) ;
170173 text = text . replace ( "{text}" , event . target . innerText ) ;
171174 showMessage ( text , 4000 , 8 ) ;
172175 } ) ;
173176 } ) ;
174177 result . click . forEach ( tips => {
175178 window . addEventListener ( "click" , event => {
176179 if ( ! event . target . matches ( tips . selector ) ) return ;
177- var text = Array . isArray ( tips . text ) ? tips . text [ Math . floor ( Math . random ( ) * tips . text . length ) ] : tips . text ;
180+ var text = randomSelection ( tips . text ) ;
178181 text = text . replace ( "{text}" , event . target . innerText ) ;
179182 showMessage ( text , 4000 , 8 ) ;
180183 } ) ;
@@ -184,7 +187,7 @@ function loadWidget(config) {
184187 after = tips . date . split ( "-" ) [ 0 ] ,
185188 before = tips . date . split ( "-" ) [ 1 ] || after ;
186189 if ( ( after . split ( "/" ) [ 0 ] <= now . getMonth ( ) + 1 && now . getMonth ( ) + 1 <= before . split ( "/" ) [ 0 ] ) && ( after . split ( "/" ) [ 1 ] <= now . getDate ( ) && now . getDate ( ) <= before . split ( "/" ) [ 1 ] ) ) {
187- var text = Array . isArray ( tips . text ) ? tips . text [ Math . floor ( Math . random ( ) * tips . text . length ) ] : tips . text ;
190+ var text = randomSelection ( tips . text ) ;
188191 text = text . replace ( "{year}" , now . getFullYear ( ) ) ;
189192 //showMessage(text, 7000, true);
190193 messageArray . push ( text ) ;
@@ -193,34 +196,58 @@ function loadWidget(config) {
193196 } ) ;
194197 } ) ( ) ;
195198
196- function loadModel ( modelId , modelTexturesId ) {
199+ async function loadModelList ( ) {
200+ let response = await fetch ( `${ cdnPath } model_list.json` ) ;
201+ let result = await response . json ( ) ;
202+ modelList = result ;
203+ }
204+
205+ async function loadModel ( modelId , modelTexturesId , message ) {
197206 localStorage . setItem ( "modelId" , modelId ) ;
198- if ( modelTexturesId === undefined ) modelTexturesId = 0 ;
199207 localStorage . setItem ( "modelTexturesId" , modelTexturesId ) ;
200- loadlive2d ( "live2d" , `${ apiPath } get/?id=${ modelId } -${ modelTexturesId } ` , console . log ( `Live2D 模型 ${ modelId } -${ modelTexturesId } 加载完成` ) ) ;
208+ showMessage ( message , 4000 , 10 ) ;
209+ if ( useCDN ) {
210+ if ( ! modelList ) await loadModelList ( ) ;
211+ let target = randomSelection ( modelList . models [ modelId ] ) ;
212+ loadlive2d ( "live2d" , `${ cdnPath } model/${ target } /index.json` ) ;
213+ } else {
214+ loadlive2d ( "live2d" , `${ apiPath } get/?id=${ modelId } -${ modelTexturesId } ` ) ;
215+ console . log ( `Live2D 模型 ${ modelId } -${ modelTexturesId } 加载完成` ) ;
216+ }
201217 }
202218
203- function loadRandModel ( ) {
219+ async function loadRandModel ( ) {
204220 var modelId = localStorage . getItem ( "modelId" ) ,
205221 modelTexturesId = localStorage . getItem ( "modelTexturesId" ) ;
206- // 可选 "rand"(随机), "switch"(顺序)
207- fetch ( `${ apiPath } rand_textures/?id=${ modelId } -${ modelTexturesId } ` )
208- . then ( response => response . json ( ) )
209- . then ( result => {
210- if ( result . textures . id == 1 && ( modelTexturesId == 1 || modelTexturesId == 0 ) ) showMessage ( "我还没有其他衣服呢!" , 4000 , 10 ) ;
211- else showMessage ( "我的新衣服好看嘛?" , 4000 , 10 ) ;
212- loadModel ( modelId , result . textures . id ) ;
213- } ) ;
222+ if ( useCDN ) {
223+ if ( ! modelList ) await loadModelList ( ) ;
224+ let target = randomSelection ( modelList . models [ modelId ] ) ;
225+ loadlive2d ( "live2d" , `${ cdnPath } model/${ target } /index.json` ) ;
226+ showMessage ( "我的新衣服好看嘛?" , 4000 , 10 ) ;
227+ } else {
228+ // 可选 "rand"(随机), "switch"(顺序)
229+ fetch ( `${ apiPath } rand_textures/?id=${ modelId } -${ modelTexturesId } ` )
230+ . then ( response => response . json ( ) )
231+ . then ( result => {
232+ if ( result . textures . id == 1 && ( modelTexturesId == 1 || modelTexturesId == 0 ) ) showMessage ( "我还没有其他衣服呢!" , 4000 , 10 ) ;
233+ else loadModel ( modelId , result . textures . id , "我的新衣服好看嘛?" ) ;
234+ } ) ;
235+ }
214236 }
215237
216- function loadOtherModel ( ) {
238+ async function loadOtherModel ( ) {
217239 var modelId = localStorage . getItem ( "modelId" ) ;
218- fetch ( `${ apiPath } switch/?id=${ modelId } ` )
219- . then ( response => response . json ( ) )
220- . then ( result => {
221- loadModel ( result . model . id ) ;
222- showMessage ( result . model . message , 4000 , 10 ) ;
223- } ) ;
240+ if ( useCDN ) {
241+ if ( ! modelList ) await loadModelList ( ) ;
242+ let index = ( ++ modelId >= modelList . models . length ) ? 0 : modelId ;
243+ loadModel ( index , 0 , modelList . messages [ index ] ) ;
244+ } else {
245+ fetch ( `${ apiPath } switch/?id=${ modelId } ` )
246+ . then ( response => response . json ( ) )
247+ . then ( result => {
248+ loadModel ( result . model . id , 0 , result . model . message ) ;
249+ } ) ;
250+ }
224251 }
225252}
226253
0 commit comments