Refactor showHitokoto

This commit is contained in:
Mimi 2019-12-29 23:39:46 +08:00
parent 3526cbcbf2
commit 3d99f6e9ed

View File

@ -92,9 +92,9 @@ function loadWidget(waifuPath, apiPath) {
showMessage(text, 7000, 8); showMessage(text, 7000, 8);
} }
welcomeMessage(); welcomeMessage();
// 检测用户活动状态,并在空闲时定时显示一言 // 检测用户活动状态,并在空闲时显示消息
var userAction = false, var userAction = false,
hitokotoTimer = null, userActionTimer = null,
messageTimer = null, messageTimer = null,
messageArray = ["好久不见,日子过得好快呢……", "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!"]; messageArray = ["好久不见,日子过得好快呢……", "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!"];
if ($(".fa-share-alt").is(":hidden")) messageArray.push("记得把小家加入 Adblock 白名单哦!"); if ($(".fa-share-alt").is(":hidden")) messageArray.push("记得把小家加入 Adblock 白名单哦!");
@ -104,19 +104,20 @@ function loadWidget(waifuPath, apiPath) {
userAction = true; userAction = true;
}); });
setInterval(() => { setInterval(() => {
if (!userAction) { if (userAction) {
if (!hitokotoTimer) hitokotoTimer = setInterval(showHitokoto, 25000);
} else {
userAction = false; userAction = false;
clearInterval(hitokotoTimer); clearInterval(userActionTimer);
hitokotoTimer = null; userActionTimer = null;
} else if (!userActionTimer) {
userActionTimer = setInterval(() => {
showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9);
}, 20000);
} }
}, 1000); }, 1000);
function showHitokoto() { function showHitokoto() {
// 增加 hitokoto.cn 的 API // 增加 hitokoto.cn 的 API
if (Math.random() < 0.6 && messageArray.length > 0) showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9); fetch("https://v1.hitokoto.cn")
else fetch("https://v1.hitokoto.cn")
.then(response => response.json()) .then(response => response.json())
.then(result => { .then(result => {
var text = `这句一言来自 <span>「${result.from}」</span>,是 <span>${result.creator}</span> 在 hitokoto.cn 投稿的。`; var text = `这句一言来自 <span>「${result.from}」</span>,是 <span>${result.creator}</span> 在 hitokoto.cn 投稿的。`;