Skip to content

Add/Fix: Add JSDoc for function and type and Fix bugs #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
10 changes: 3 additions & 7 deletions build/index.js → build/initWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function loadWidget(config) {
localStorage.removeItem('waifu-display');
sessionStorage.removeItem('waifu-text');
document.body.insertAdjacentHTML('beforeend', "<div id=\"waifu\">\n <div id=\"waifu-tips\"></div>\n <canvas id=\"live2d\" width=\"800\" height=\"800\"></canvas>\n <div id=\"waifu-tool\"></div>\n </div>");
// https://stackoverflow.com/questions/24148403/trigger-css-transition-on-appended-element
setTimeout(function () {
document.getElementById('waifu').style.bottom = '0';
}, 0);
Expand All @@ -32,7 +31,6 @@ function loadWidget(config) {
})();
function welcomeMessage(time) {
if (location.pathname === '/') {
// 如果是主页
for (var _i = 0, time_1 = time; _i < time_1.length; _i++) {
var _a = time_1[_i], hour = _a.hour, text_1 = _a.text;
var now = new Date(), after = hour.split('-')[0], before = hour.split('-')[1] || after;
Expand Down Expand Up @@ -62,7 +60,6 @@ function loadWidget(config) {
return text;
}
function registerEventListener(result) {
// Detect user activity and display messages when idle
var userAction = false;
var userActionTimer;
var messageArray = result.message.default;
Expand Down Expand Up @@ -137,10 +134,9 @@ function loadWidget(config) {
(function initModel() {
var modelId = Number(localStorage.getItem('modelId'));
var modelTexturesId = Number(localStorage.getItem('modelTexturesId'));
if (modelId === null) {
// 首次访问加载 指定模型 的 指定材质
modelId = 1; // 模型 ID
modelTexturesId = 53; // 材质 ID
if (!modelId) {
modelId = 1;
modelTexturesId = 53;
}
void model.loadModel(modelId, modelTexturesId, '');
fetch(config.waifuPath)
Expand Down
16 changes: 9 additions & 7 deletions build/message.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import randomSelection from "./utils.js";
import randomSelection from './utils.js';
var messageTimer = null;
function showMessage(text, timeout, priority) {
if (!text || (sessionStorage.getItem("waifu-text") && Number(sessionStorage.getItem("waifu-text")) > priority))
if (!text ||
(sessionStorage.getItem('waifu-text') &&
Number(sessionStorage.getItem('waifu-text')) > priority))
return;
if (messageTimer) {
clearTimeout(messageTimer);
messageTimer = null;
}
text = randomSelection(text);
sessionStorage.setItem("waifu-text", String(priority));
var tips = document.getElementById("waifu-tips");
sessionStorage.setItem('waifu-text', String(priority));
var tips = document.getElementById('waifu-tips');
tips.innerHTML = text;
tips.classList.add("waifu-tips-active");
tips.classList.add('waifu-tips-active');
messageTimer = setTimeout(function () {
sessionStorage.removeItem("waifu-text");
tips.classList.remove("waifu-tips-active");
sessionStorage.removeItem('waifu-text');
tips.classList.remove('waifu-tips-active');
}, timeout);
}
export default showMessage;
39 changes: 19 additions & 20 deletions build/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
import showMessage from './message.js';
import randomSelection from './utils.js';
var Model = /** @class */ (function () {
var Model = (function () {
function Model(config) {
this.modelList = null;
var apiPath = config.apiPath, cdnPath = config.cdnPath;
Expand All @@ -62,14 +62,14 @@ var Model = /** @class */ (function () {
var response, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, fetch("".concat(this.cdnPath, "model_list.json"))];
case 0: return [4, fetch("".concat(this.cdnPath, "model_list.json"))];
case 1:
response = _b.sent();
_a = this;
return [4 /*yield*/, response.json()];
return [4, response.json()];
case 2:
_a.modelList = _b.sent();
return [2 /*return*/];
return [2];
}
});
});
Expand All @@ -83,21 +83,21 @@ var Model = /** @class */ (function () {
localStorage.setItem('modelId', modelId.toString());
localStorage.setItem('modelTexturesId', modelTexturesId.toString());
showMessage(message, 4000, 10);
if (!(this.useCDN && this.modelList)) return [3 /*break*/, 3];
if (!!this.modelList) return [3 /*break*/, 2];
return [4 /*yield*/, this.loadModelList()];
if (!(this.useCDN && this.modelList)) return [3, 3];
if (!!this.modelList) return [3, 2];
return [4, this.loadModelList()];
case 1:
_a.sent();
_a.label = 2;
case 2:
target = randomSelection(this.modelList.models[modelId]);
loadlive2d('live2d', "".concat(this.cdnPath, "model/").concat(target, "/index.json"));
return [3 /*break*/, 4];
return [3, 4];
case 3:
loadlive2d('live2d', "".concat(this.apiPath, "get/?id=").concat(modelId, "-").concat(modelTexturesId));
console.log("Live2D Model ".concat(modelId, "-").concat(modelTexturesId, " Loaded"));
_a.label = 4;
case 4: return [2 /*return*/];
case 4: return [2];
}
});
});
Expand All @@ -111,19 +111,18 @@ var Model = /** @class */ (function () {
case 0:
modelId = Number(localStorage.getItem('modelId'));
modelTexturesId = Number(localStorage.getItem('modelTexturesId'));
if (!(this.useCDN && modelId && this.modelList)) return [3 /*break*/, 3];
if (!!this.modelList) return [3 /*break*/, 2];
return [4 /*yield*/, this.loadModelList()];
if (!(this.useCDN && modelId && this.modelList)) return [3, 3];
if (!!this.modelList) return [3, 2];
return [4, this.loadModelList()];
case 1:
_a.sent();
_a.label = 2;
case 2:
target = randomSelection(this.modelList.models[modelId]);
loadlive2d('live2d', "".concat(this.cdnPath, "model/").concat(target, "/index.json"));
showMessage('我的新衣服好看嘛?', 4000, 10);
return [3 /*break*/, 4];
return [3, 4];
case 3:
// Optional "rand" (Random), "switch" (Switch by order)
fetch("".concat(this.apiPath, "rand_textures/?id=").concat(modelId, "-").concat(modelTexturesId))
.then(function (response) { return response.json(); })
.then(function (result) {
Expand All @@ -136,7 +135,7 @@ var Model = /** @class */ (function () {
}
});
_a.label = 4;
case 4: return [2 /*return*/];
case 4: return [2];
}
});
});
Expand All @@ -149,24 +148,24 @@ var Model = /** @class */ (function () {
switch (_a.label) {
case 0:
modelId = Number(localStorage.getItem('modelId'));
if (!(this.useCDN && modelId && this.modelList)) return [3 /*break*/, 3];
if (!!this.modelList) return [3 /*break*/, 2];
return [4 /*yield*/, this.loadModelList()];
if (!(this.useCDN && modelId && this.modelList)) return [3, 3];
if (!!this.modelList) return [3, 2];
return [4, this.loadModelList()];
case 1:
_a.sent();
_a.label = 2;
case 2:
index = ++modelId >= this.modelList.models.length ? 0 : modelId;
void this.loadModel(index, 0, this.modelList.messages[index]);
return [3 /*break*/, 4];
return [3, 4];
case 3:
fetch("".concat(this.apiPath, "switch/?id=").concat(modelId))
.then(function (response) { return response.json(); })
.then(function (result) {
_this.loadModel(result.model.id, 0, result.model.message);
});
_a.label = 4;
case 4: return [2 /*return*/];
case 4: return [2];
}
});
});
Expand Down
1 change: 0 additions & 1 deletion build/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import fa_info_circle from '@fortawesome/fontawesome-free/svgs/solid/circle-info
import fa_xmark from '@fortawesome/fontawesome-free/svgs/solid/xmark.svg';
import showMessage from './message.js';
function showHitokoto() {
// 增加 hitokoto.cn 的 API
fetch('https://v1.hitokoto.cn')
.then(function (response) { return response.json(); })
.then(function (result) {
Expand Down
2 changes: 1 addition & 1 deletion build/waifu-tips.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import initWidget from "./index.js";
import initWidget from './initWidget.js';
window.initWidget = initWidget;
6 changes: 3 additions & 3 deletions dist/autoload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// live2d_path 参数建议使用绝对路径
const live2d_path = "https://fastly.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/";
//const live2d_path = "/live2d-widget/";
const live2d_path = "https://fastly.jsdelivr.net/npm/live2d-widgets@0/";
// const live2d_path = "/live2d-widget/dist/";

// 封装异步加载资源的方法
function loadExternalResource(url, type) {
Expand Down Expand Up @@ -34,7 +34,7 @@ if (screen.width >= 768) {
// 配置选项的具体用法见 README.md
initWidget({
waifuPath: live2d_path + "waifu-tips.json",
//apiPath: "https://live2d.fghrsh.net/api/",
// apiPath: "https://live2d.fghrsh.net/api/",
cdnPath: "https://fastly.jsdelivr.net/gh/fghrsh/live2d_api/",
tools: ["hitokoto", "asteroids", "switch-model", "switch-texture", "photo", "info", "quit"]
});
Expand Down
Loading