Refactor registerEventListener

This commit is contained in:
Mimi 2022-10-20 22:50:17 +08:00
parent d28d74c16f
commit e0be7adf1d
3 changed files with 56 additions and 52 deletions

View File

@ -22,25 +22,7 @@ function loadWidget(config) {
document.getElementById("waifu").style.bottom = 0;
}, 0);
// 检测用户活动状态,并在空闲时显示消息
let userAction = false,
userActionTimer,
messageArray = ["好久不见,日子过得好快呢……", "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!", "记得把小家加入 Adblock 白名单哦!"];
window.addEventListener("mousemove", () => userAction = true);
window.addEventListener("keydown", () => userAction = true);
setInterval(() => {
if (userAction) {
userAction = false;
clearInterval(userActionTimer);
userActionTimer = null;
} else if (!userActionTimer) {
userActionTimer = setInterval(() => {
showMessage(randomSelection(messageArray), 6000, 9);
}, 20000);
}
}, 1000);
(function registerEventListener() {
(function registerTools() {
tools["switch-model"].callback = () => model.loadOtherModel();
tools["switch-texture"].callback = () => model.loadRandModel();
if (!Array.isArray(config.tools)) {
@ -98,18 +80,24 @@ function loadWidget(config) {
return message;
}
(function initModel() {
let modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId");
if (modelId === null) {
// 首次访问加载 指定模型 的 指定材质
modelId = 1; // 模型 ID
modelTexturesId = 53; // 材质 ID
function registerEventListener(result) {
// 检测用户活动状态,并在空闲时显示消息
let userAction = false,
userActionTimer,
messageArray = result.message.default;
window.addEventListener("mousemove", () => userAction = true);
window.addEventListener("keydown", () => userAction = true);
setInterval(() => {
if (userAction) {
userAction = false;
clearInterval(userActionTimer);
userActionTimer = null;
} else if (!userActionTimer) {
userActionTimer = setInterval(() => {
showMessage(randomSelection(messageArray), 6000, 9);
}, 20000);
}
model.loadModel(modelId, modelTexturesId);
fetch(config.waifuPath)
.then(response => response.json())
.then(result => {
}, 1000);
showMessage(welcomeMessage(result.time), 7000, 11);
window.addEventListener("mouseover", event => {
for (let { selector, text } of result.mouseover) {
@ -139,7 +127,20 @@ function loadWidget(config) {
messageArray.push(text);
}
});
});
}
(function initModel() {
let modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId");
if (modelId === null) {
// 首次访问加载 指定模型 的 指定材质
modelId = 1; // 模型 ID
modelTexturesId = 53; // 材质 ID
}
model.loadModel(modelId, modelTexturesId);
fetch(config.waifuPath)
.then(response => response.json())
.then(registerEventListener);
})();
}

File diff suppressed because one or more lines are too long

View File

@ -245,5 +245,8 @@
}, {
"hour": "0-5",
"text": "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?"
}]
}],
"message": {
"default": ["好久不见,日子过得好快呢……", "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!", "记得把小家加入收藏夹哦!"]
}
}