@@ -2,7 +2,7 @@ var fs = require('hexo-fs');
22var path = require ( 'path' ) ;
33var url = require ( "url" ) ;
44
5-
5+ // 将文件加入复制队列
66function registerFile ( pathname , file ) {
77 generators . push ( {
88 path : pathname ,
@@ -12,6 +12,8 @@ function registerFile(pathname, file) {
1212 } ) ;
1313}
1414
15+ // 使用DFS将目录中的所有文件加入队列
16+ // 目前仍使用同步
1517function registerDir ( pathname , dir ) {
1618 var lsDir = fs . listDirSync ( dir )
1719 lsDir . forEach ( function ( file ) {
@@ -40,7 +42,7 @@ var config = Object.assign( {
4042 hexo . config . live2d ,
4143 hexo . theme . config . live2d
4244) ;
43-
45+ // 将替换模板中 live2d 的网页代码
4446hexo . extend . helper . register ( 'live2d' , function ( ) {
4547 return `
4648<div id="hexo-helper-live2d">
@@ -60,44 +62,18 @@ hexo.extend.helper.register('live2d', function() {
6062</style>
6163<script type="text/javascript" src="${ config . deviceJsSource == "local" ? `/live2d/device.min.js` : ( config . deviceJsSource == "official" ? `https://unpkg.com/current-device/umd/current-device.min.js` : config . deviceJsSource ) } "></script>
6264<script type="text/javascript">
63- function loadScript(url, callback) {
64- var script = document.createElement("script");
65- script.type = "text/javascript";
66- if(typeof(callback) != "undefined"){
67- if (script.readyState) {
68- script.onreadystatechange = function () {
69- if (script.readyState == "loaded" || script.readyState == "complete") {
70- script.onreadystatechange = null;
71- callback();
72- }
73- };
74- } else {
75- script.onload = function () {
76- callback();
77- };
78- }
79- }
80- script.src = url;
81- document.body.appendChild(script);
82- }
65+ function loadScript(c,b){var a=document.createElement("script");a.type="text/javascript";"undefined"!=typeof b&&(a.readyState?a.onreadystatechange=function(){if("loaded"==a.readyState||"complete"==a.readyState)a.onreadystatechange=null,b()}:a.onload=function(){b()});a.src=c;document.body.appendChild(a)};
8366(function(){
8467 if(typeof(device) != 'undefined'){
8568 if(device.mobile()){
86- ${ config . mobileShow ? `document.getElementById("${ config . id } ").style.width = '${ config . width * config . mobileScaling } px';
87- document.getElementById("${ config . id } ").style.height = '${ config . height * config . mobileScaling } px';
88- loadScript("/live2d/script.js", function(){
89- loadlive2d(${ JSON . stringify ( config . id ) } , ${ JSON . stringify ( url . resolve ( "/live2d/assets/" , config . model + ".model.json" ) ) } , 0.5);
90- });` : `` }
69+ ${ config . mobileShow ? `document.getElementById("${ config . id } ").style.width = '${ config . width * config . mobileScaling } px';document.getElementById("${ config . id } ").style.height = '${ config . height * config . mobileScaling } px';
70+ loadScript("/live2d/script.js", function(){loadlive2d(${ JSON . stringify ( config . id ) } , ${ JSON . stringify ( url . resolve ( "/live2d/assets/" , config . model + ".model.json" ) ) } , 0.5);});` : `` }
9171 }else{
92- loadScript("/live2d/script.js", function(){
93- loadlive2d(${ JSON . stringify ( config . id ) } , ${ JSON . stringify ( url . resolve ( "/live2d/assets/" , config . model + ".model.json" ) ) } , 0.5);
94- });
72+ loadScript("/live2d/script.js", function(){loadlive2d(${ JSON . stringify ( config . id ) } , ${ JSON . stringify ( url . resolve ( "/live2d/assets/" , config . model + ".model.json" ) ) } , 0.5);});
9573 }
9674 }else{
9775 console.error('Cannot find current-device script.');
98- loadScript("/live2d/script.js", function(){
99- loadlive2d(${ JSON . stringify ( config . id ) } , ${ JSON . stringify ( url . resolve ( "/live2d/assets/" , config . model + ".model.json" ) ) } , 0.5);
100- });
76+ loadScript("/live2d/script.js", function(){loadlive2d(${ JSON . stringify ( config . id ) } , ${ JSON . stringify ( url . resolve ( "/live2d/assets/" , config . model + ".model.json" ) ) } , 0.5);});
10177 }
10278})();
10379</script>
@@ -118,14 +94,16 @@ fs.exists(path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model
11894registerFile ( 'live2d/script.js' , path . resolve ( __dirname , './dist/bundle.js' ) ) ;
11995
12096// 复制 device.js 脚本
121- fs . exists ( path . resolve ( __dirname , './dist/device.min.js' ) , function ( exists ) {
97+ if ( config . deviceJsSource == "local" ) {
98+ fs . exists ( path . resolve ( __dirname , './dist/device.min.js' ) , function ( exists ) {
12299 if ( exists ) {
123100 registerFile ( 'live2d/device.min.js' , path . resolve ( __dirname , './dist/device.min.js' ) ) ;
124- } else {
101+ } else { // 若未找到,则报错
125102 console . log ( "Live2d serverJs: can't find device.js, contant the author for support." ) ;
126103 return ;
127104 }
128- } ) ;
105+ } ) ;
106+ }
129107
130108hexo . extend . generator . register ( 'live2d' , function ( locals ) {
131109 return generators ;
0 commit comments