mirror of
https://github.com/caojiezi2003/live2d-widget.git
synced 2024-11-22 15:19:44 +00:00
Use let
statements
This commit is contained in:
parent
a4f1df83f8
commit
de073bc60c
103
waifu-tips.js
103
waifu-tips.js
@ -31,6 +31,28 @@ function loadWidget(config) {
|
|||||||
document.getElementById("waifu").style.bottom = 0;
|
document.getElementById("waifu").style.bottom = 0;
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
|
function randomSelection(obj) {
|
||||||
|
return Array.isArray(obj) ? obj[Math.floor(Math.random() * obj.length)] : obj;
|
||||||
|
}
|
||||||
|
// 检测用户活动状态,并在空闲时显示消息
|
||||||
|
let userAction = false,
|
||||||
|
userActionTimer,
|
||||||
|
messageTimer,
|
||||||
|
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 registerEventListener() {
|
||||||
document.querySelector("#waifu-tool .fa-comment").addEventListener("click", showHitokoto);
|
document.querySelector("#waifu-tool .fa-comment").addEventListener("click", showHitokoto);
|
||||||
document.querySelector("#waifu-tool .fa-paper-plane").addEventListener("click", () => {
|
document.querySelector("#waifu-tool .fa-paper-plane").addEventListener("click", () => {
|
||||||
@ -38,7 +60,7 @@ function loadWidget(config) {
|
|||||||
if (!window.ASTEROIDSPLAYERS) window.ASTEROIDSPLAYERS = [];
|
if (!window.ASTEROIDSPLAYERS) window.ASTEROIDSPLAYERS = [];
|
||||||
window.ASTEROIDSPLAYERS.push(new Asteroids());
|
window.ASTEROIDSPLAYERS.push(new Asteroids());
|
||||||
} else {
|
} else {
|
||||||
var script = document.createElement("script");
|
let script = document.createElement("script");
|
||||||
script.src = "https://cdn.jsdelivr.net/gh/GalaxyMimi/CDN/asteroids.js";
|
script.src = "https://cdn.jsdelivr.net/gh/GalaxyMimi/CDN/asteroids.js";
|
||||||
document.head.appendChild(script);
|
document.head.appendChild(script);
|
||||||
}
|
}
|
||||||
@ -62,7 +84,7 @@ function loadWidget(config) {
|
|||||||
document.getElementById("waifu-toggle").classList.add("waifu-toggle-active");
|
document.getElementById("waifu-toggle").classList.add("waifu-toggle-active");
|
||||||
}, 3000);
|
}, 3000);
|
||||||
});
|
});
|
||||||
var devtools = () => {};
|
let devtools = () => {};
|
||||||
console.log("%c", devtools);
|
console.log("%c", devtools);
|
||||||
devtools.toString = () => {
|
devtools.toString = () => {
|
||||||
showMessage("哈哈,你打开了控制台,是想要看看我的小秘密吗?", 6000, 9);
|
showMessage("哈哈,你打开了控制台,是想要看看我的小秘密吗?", 6000, 9);
|
||||||
@ -76,9 +98,9 @@ function loadWidget(config) {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
(function welcomeMessage() {
|
(function welcomeMessage() {
|
||||||
var text;
|
let text;
|
||||||
if (location.pathname === "/") { // 如果是主页
|
if (location.pathname === "/") { // 如果是主页
|
||||||
var now = new Date().getHours();
|
let now = new Date().getHours();
|
||||||
if (now > 5 && now <= 7) text = "早上好!一日之计在于晨,美好的一天就要开始了。";
|
if (now > 5 && now <= 7) text = "早上好!一日之计在于晨,美好的一天就要开始了。";
|
||||||
else if (now > 7 && now <= 11) text = "上午好!工作顺利嘛,不要久坐,多起来走动走动哦!";
|
else if (now > 7 && now <= 11) text = "上午好!工作顺利嘛,不要久坐,多起来走动走动哦!";
|
||||||
else if (now > 11 && now <= 13) text = "中午了,工作了一个上午,现在是午餐时间!";
|
else if (now > 11 && now <= 13) text = "中午了,工作了一个上午,现在是午餐时间!";
|
||||||
@ -88,7 +110,7 @@ function loadWidget(config) {
|
|||||||
else if (now > 21 && now <= 23) text = ["已经这么晚了呀,早点休息吧,晚安~", "深夜时要爱护眼睛呀!"];
|
else if (now > 21 && now <= 23) text = ["已经这么晚了呀,早点休息吧,晚安~", "深夜时要爱护眼睛呀!"];
|
||||||
else text = "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?";
|
else text = "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?";
|
||||||
} else if (document.referrer !== "") {
|
} else if (document.referrer !== "") {
|
||||||
var referrer = new URL(document.referrer),
|
let referrer = new URL(document.referrer),
|
||||||
domain = referrer.hostname.split(".")[1];
|
domain = referrer.hostname.split(".")[1];
|
||||||
if (location.hostname === referrer.hostname) text = `欢迎阅读<span>「${document.title.split(" - ")[0]}」</span>`;
|
if (location.hostname === referrer.hostname) text = `欢迎阅读<span>「${document.title.split(" - ")[0]}」</span>`;
|
||||||
else if (domain === "baidu") text = `Hello!来自 百度搜索 的朋友<br>你是搜索 <span>${referrer.search.split("&wd=")[1].split("&")[0]}</span> 找到的我吗?`;
|
else if (domain === "baidu") text = `Hello!来自 百度搜索 的朋友<br>你是搜索 <span>${referrer.search.split("&wd=")[1].split("&")[0]}</span> 找到的我吗?`;
|
||||||
@ -100,34 +122,13 @@ function loadWidget(config) {
|
|||||||
}
|
}
|
||||||
showMessage(text, 7000, 8);
|
showMessage(text, 7000, 8);
|
||||||
})();
|
})();
|
||||||
function randomSelection(obj) {
|
|
||||||
return Array.isArray(obj) ? obj[Math.floor(Math.random() * obj.length)] : obj;
|
|
||||||
}
|
|
||||||
// 检测用户活动状态,并在空闲时显示消息
|
|
||||||
var userAction = false,
|
|
||||||
userActionTimer,
|
|
||||||
messageTimer,
|
|
||||||
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 showHitokoto() {
|
function showHitokoto() {
|
||||||
// 增加 hitokoto.cn 的 API
|
// 增加 hitokoto.cn 的 API
|
||||||
fetch("https://v1.hitokoto.cn")
|
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 投稿的。`;
|
let text = `这句一言来自 <span>「${result.from}」</span>,是 <span>${result.creator}</span> 在 hitokoto.cn 投稿的。`;
|
||||||
showMessage(result.hitokoto, 6000, 9);
|
showMessage(result.hitokoto, 6000, 9);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
showMessage(text, 4000, 9);
|
showMessage(text, 4000, 9);
|
||||||
@ -136,31 +137,29 @@ function loadWidget(config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function showMessage(text, timeout, priority) {
|
function showMessage(text, timeout, priority) {
|
||||||
if (!text) return;
|
if (!text || (sessionStorage.getItem("waifu-text") && sessionStorage.getItem("waifu-text") > priority)) return;
|
||||||
if (!sessionStorage.getItem("waifu-text") || sessionStorage.getItem("waifu-text") <= priority) {
|
if (messageTimer) {
|
||||||
if (messageTimer) {
|
clearTimeout(messageTimer);
|
||||||
clearTimeout(messageTimer);
|
messageTimer = null;
|
||||||
messageTimer = null;
|
|
||||||
}
|
|
||||||
text = randomSelection(text);
|
|
||||||
sessionStorage.setItem("waifu-text", priority);
|
|
||||||
var tips = document.getElementById("waifu-tips");
|
|
||||||
tips.innerHTML = text;
|
|
||||||
tips.classList.add("waifu-tips-active");
|
|
||||||
messageTimer = setTimeout(() => {
|
|
||||||
sessionStorage.removeItem("waifu-text");
|
|
||||||
tips.classList.remove("waifu-tips-active");
|
|
||||||
}, timeout);
|
|
||||||
}
|
}
|
||||||
|
text = randomSelection(text);
|
||||||
|
sessionStorage.setItem("waifu-text", priority);
|
||||||
|
let tips = document.getElementById("waifu-tips");
|
||||||
|
tips.innerHTML = text;
|
||||||
|
tips.classList.add("waifu-tips-active");
|
||||||
|
messageTimer = setTimeout(() => {
|
||||||
|
sessionStorage.removeItem("waifu-text");
|
||||||
|
tips.classList.remove("waifu-tips-active");
|
||||||
|
}, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
(function initModel() {
|
(function initModel() {
|
||||||
var modelId = localStorage.getItem("modelId"),
|
let modelId = localStorage.getItem("modelId"),
|
||||||
modelTexturesId = localStorage.getItem("modelTexturesId");
|
modelTexturesId = localStorage.getItem("modelTexturesId");
|
||||||
if (modelId === null) {
|
if (modelId === null) {
|
||||||
// 首次访问加载 指定模型 的 指定材质
|
// 首次访问加载 指定模型 的 指定材质
|
||||||
var modelId = 1, // 模型 ID
|
modelId = 1; // 模型 ID
|
||||||
modelTexturesId = 53; // 材质 ID
|
modelTexturesId = 53; // 材质 ID
|
||||||
}
|
}
|
||||||
loadModel(modelId, modelTexturesId);
|
loadModel(modelId, modelTexturesId);
|
||||||
fetch(waifuPath)
|
fetch(waifuPath)
|
||||||
@ -169,7 +168,7 @@ function loadWidget(config) {
|
|||||||
result.mouseover.forEach(tips => {
|
result.mouseover.forEach(tips => {
|
||||||
window.addEventListener("mouseover", event => {
|
window.addEventListener("mouseover", event => {
|
||||||
if (!event.target.matches(tips.selector)) return;
|
if (!event.target.matches(tips.selector)) return;
|
||||||
var text = randomSelection(tips.text);
|
let text = randomSelection(tips.text);
|
||||||
text = text.replace("{text}", event.target.innerText);
|
text = text.replace("{text}", event.target.innerText);
|
||||||
showMessage(text, 4000, 8);
|
showMessage(text, 4000, 8);
|
||||||
});
|
});
|
||||||
@ -177,17 +176,17 @@ function loadWidget(config) {
|
|||||||
result.click.forEach(tips => {
|
result.click.forEach(tips => {
|
||||||
window.addEventListener("click", event => {
|
window.addEventListener("click", event => {
|
||||||
if (!event.target.matches(tips.selector)) return;
|
if (!event.target.matches(tips.selector)) return;
|
||||||
var text = randomSelection(tips.text);
|
let text = randomSelection(tips.text);
|
||||||
text = text.replace("{text}", event.target.innerText);
|
text = text.replace("{text}", event.target.innerText);
|
||||||
showMessage(text, 4000, 8);
|
showMessage(text, 4000, 8);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
result.seasons.forEach(tips => {
|
result.seasons.forEach(tips => {
|
||||||
var now = new Date(),
|
let now = new Date(),
|
||||||
after = tips.date.split("-")[0],
|
after = tips.date.split("-")[0],
|
||||||
before = tips.date.split("-")[1] || after;
|
before = tips.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])) {
|
if ((after.split("/")[0] <= now.getMonth() + 1 && now.getMonth() + 1 <= before.split("/")[0]) && (after.split("/")[1] <= now.getDate() && now.getDate() <= before.split("/")[1])) {
|
||||||
var text = randomSelection(tips.text);
|
let text = randomSelection(tips.text);
|
||||||
text = text.replace("{year}", now.getFullYear());
|
text = text.replace("{year}", now.getFullYear());
|
||||||
//showMessage(text, 7000, true);
|
//showMessage(text, 7000, true);
|
||||||
messageArray.push(text);
|
messageArray.push(text);
|
||||||
@ -217,7 +216,7 @@ function loadWidget(config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadRandModel() {
|
async function loadRandModel() {
|
||||||
var modelId = localStorage.getItem("modelId"),
|
let modelId = localStorage.getItem("modelId"),
|
||||||
modelTexturesId = localStorage.getItem("modelTexturesId");
|
modelTexturesId = localStorage.getItem("modelTexturesId");
|
||||||
if (useCDN) {
|
if (useCDN) {
|
||||||
if (!modelList) await loadModelList();
|
if (!modelList) await loadModelList();
|
||||||
@ -236,7 +235,7 @@ function loadWidget(config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadOtherModel() {
|
async function loadOtherModel() {
|
||||||
var modelId = localStorage.getItem("modelId");
|
let modelId = localStorage.getItem("modelId");
|
||||||
if (useCDN) {
|
if (useCDN) {
|
||||||
if (!modelList) await loadModelList();
|
if (!modelList) await loadModelList();
|
||||||
let index = (++modelId >= modelList.models.length) ? 0 : modelId;
|
let index = (++modelId >= modelList.models.length) ? 0 : modelId;
|
||||||
@ -261,7 +260,7 @@ function initWidget(config, apiPath = "/") {
|
|||||||
document.body.insertAdjacentHTML("beforeend", `<div id="waifu-toggle">
|
document.body.insertAdjacentHTML("beforeend", `<div id="waifu-toggle">
|
||||||
<span>看板娘</span>
|
<span>看板娘</span>
|
||||||
</div>`);
|
</div>`);
|
||||||
var toggle = document.getElementById("waifu-toggle");
|
let toggle = document.getElementById("waifu-toggle");
|
||||||
toggle.addEventListener("click", () => {
|
toggle.addEventListener("click", () => {
|
||||||
toggle.classList.remove("waifu-toggle-active");
|
toggle.classList.remove("waifu-toggle-active");
|
||||||
if (toggle.getAttribute("first-time")) {
|
if (toggle.getAttribute("first-time")) {
|
||||||
|
Loading…
Reference in New Issue
Block a user