mirror of
https://github.com/stevenjoezhang/live2d-widget.git
synced 2024-11-24 22:00:32 +08:00
Refactor registerEventListener
This commit is contained in:
parent
d28d74c16f
commit
e0be7adf1d
@ -22,25 +22,7 @@ function loadWidget(config) {
|
|||||||
document.getElementById("waifu").style.bottom = 0;
|
document.getElementById("waifu").style.bottom = 0;
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
// 检测用户活动状态,并在空闲时显示消息
|
(function registerTools() {
|
||||||
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() {
|
|
||||||
tools["switch-model"].callback = () => model.loadOtherModel();
|
tools["switch-model"].callback = () => model.loadOtherModel();
|
||||||
tools["switch-texture"].callback = () => model.loadRandModel();
|
tools["switch-texture"].callback = () => model.loadRandModel();
|
||||||
if (!Array.isArray(config.tools)) {
|
if (!Array.isArray(config.tools)) {
|
||||||
@ -98,6 +80,55 @@ function loadWidget(config) {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
showMessage(welcomeMessage(result.time), 7000, 11);
|
||||||
|
window.addEventListener("mouseover", event => {
|
||||||
|
for (let { selector, text } of result.mouseover) {
|
||||||
|
if (!event.target.matches(selector)) continue;
|
||||||
|
text = randomSelection(text);
|
||||||
|
text = text.replace("{text}", event.target.innerText);
|
||||||
|
showMessage(text, 4000, 8);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
window.addEventListener("click", event => {
|
||||||
|
for (let { selector, text } of result.click) {
|
||||||
|
if (!event.target.matches(selector)) continue;
|
||||||
|
text = randomSelection(text);
|
||||||
|
text = text.replace("{text}", event.target.innerText);
|
||||||
|
showMessage(text, 4000, 8);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
result.seasons.forEach(({ date, text }) => {
|
||||||
|
const now = new Date(),
|
||||||
|
after = date.split("-")[0],
|
||||||
|
before = date.split("-")[1] || after;
|
||||||
|
if ((after.split("/")[0] <= now.getMonth() + 1 && now.getMonth() + 1 <= before.split("/")[0]) && (after.split("/")[1] <= now.getDate() && now.getDate() <= before.split("/")[1])) {
|
||||||
|
text = randomSelection(text);
|
||||||
|
text = text.replace("{year}", now.getFullYear());
|
||||||
|
messageArray.push(text);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
(function initModel() {
|
(function initModel() {
|
||||||
let modelId = localStorage.getItem("modelId"),
|
let modelId = localStorage.getItem("modelId"),
|
||||||
modelTexturesId = localStorage.getItem("modelTexturesId");
|
modelTexturesId = localStorage.getItem("modelTexturesId");
|
||||||
@ -109,37 +140,7 @@ function loadWidget(config) {
|
|||||||
model.loadModel(modelId, modelTexturesId);
|
model.loadModel(modelId, modelTexturesId);
|
||||||
fetch(config.waifuPath)
|
fetch(config.waifuPath)
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(result => {
|
.then(registerEventListener);
|
||||||
showMessage(welcomeMessage(result.time), 7000, 11);
|
|
||||||
window.addEventListener("mouseover", event => {
|
|
||||||
for (let { selector, text } of result.mouseover) {
|
|
||||||
if (!event.target.matches(selector)) continue;
|
|
||||||
text = randomSelection(text);
|
|
||||||
text = text.replace("{text}", event.target.innerText);
|
|
||||||
showMessage(text, 4000, 8);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
window.addEventListener("click", event => {
|
|
||||||
for (let { selector, text } of result.click) {
|
|
||||||
if (!event.target.matches(selector)) continue;
|
|
||||||
text = randomSelection(text);
|
|
||||||
text = text.replace("{text}", event.target.innerText);
|
|
||||||
showMessage(text, 4000, 8);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
result.seasons.forEach(({ date, text }) => {
|
|
||||||
const now = new Date(),
|
|
||||||
after = date.split("-")[0],
|
|
||||||
before = date.split("-")[1] || after;
|
|
||||||
if ((after.split("/")[0] <= now.getMonth() + 1 && now.getMonth() + 1 <= before.split("/")[0]) && (after.split("/")[1] <= now.getDate() && now.getDate() <= before.split("/")[1])) {
|
|
||||||
text = randomSelection(text);
|
|
||||||
text = text.replace("{year}", now.getFullYear());
|
|
||||||
messageArray.push(text);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File diff suppressed because one or more lines are too long
@ -245,5 +245,8 @@
|
|||||||
}, {
|
}, {
|
||||||
"hour": "0-5",
|
"hour": "0-5",
|
||||||
"text": "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?"
|
"text": "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?"
|
||||||
}]
|
}],
|
||||||
|
"message": {
|
||||||
|
"default": ["好久不见,日子过得好快呢……", "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!", "记得把小家加入收藏夹哦!"]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user