From de073bc60ca1a3aa13fe2ea00139079d3777d347 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Fri, 28 Feb 2020 22:02:35 +0800 Subject: [PATCH] Use `let` statements --- waifu-tips.js | 103 +++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/waifu-tips.js b/waifu-tips.js index d2c3165..2d1b2bf 100755 --- a/waifu-tips.js +++ b/waifu-tips.js @@ -31,6 +31,28 @@ function loadWidget(config) { document.getElementById("waifu").style.bottom = 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() { document.querySelector("#waifu-tool .fa-comment").addEventListener("click", showHitokoto); document.querySelector("#waifu-tool .fa-paper-plane").addEventListener("click", () => { @@ -38,7 +60,7 @@ function loadWidget(config) { if (!window.ASTEROIDSPLAYERS) window.ASTEROIDSPLAYERS = []; window.ASTEROIDSPLAYERS.push(new Asteroids()); } else { - var script = document.createElement("script"); + let script = document.createElement("script"); script.src = "https://cdn.jsdelivr.net/gh/GalaxyMimi/CDN/asteroids.js"; document.head.appendChild(script); } @@ -62,7 +84,7 @@ function loadWidget(config) { document.getElementById("waifu-toggle").classList.add("waifu-toggle-active"); }, 3000); }); - var devtools = () => {}; + let devtools = () => {}; console.log("%c", devtools); devtools.toString = () => { showMessage("哈哈,你打开了控制台,是想要看看我的小秘密吗?", 6000, 9); @@ -76,9 +98,9 @@ function loadWidget(config) { })(); (function welcomeMessage() { - var text; + let text; if (location.pathname === "/") { // 如果是主页 - var now = new Date().getHours(); + let now = new Date().getHours(); if (now > 5 && now <= 7) text = "早上好!一日之计在于晨,美好的一天就要开始了。"; else if (now > 7 && now <= 11) text = "上午好!工作顺利嘛,不要久坐,多起来走动走动哦!"; else if (now > 11 && now <= 13) text = "中午了,工作了一个上午,现在是午餐时间!"; @@ -88,7 +110,7 @@ function loadWidget(config) { else if (now > 21 && now <= 23) text = ["已经这么晚了呀,早点休息吧,晚安~", "深夜时要爱护眼睛呀!"]; else text = "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?"; } else if (document.referrer !== "") { - var referrer = new URL(document.referrer), + let referrer = new URL(document.referrer), domain = referrer.hostname.split(".")[1]; if (location.hostname === referrer.hostname) text = `欢迎阅读「${document.title.split(" - ")[0]}」`; else if (domain === "baidu") text = `Hello!来自 百度搜索 的朋友
你是搜索 ${referrer.search.split("&wd=")[1].split("&")[0]} 找到的我吗?`; @@ -100,34 +122,13 @@ function loadWidget(config) { } 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() { // 增加 hitokoto.cn 的 API fetch("https://v1.hitokoto.cn") .then(response => response.json()) .then(result => { - var text = `这句一言来自 「${result.from}」,是 ${result.creator} 在 hitokoto.cn 投稿的。`; + let text = `这句一言来自 「${result.from}」,是 ${result.creator} 在 hitokoto.cn 投稿的。`; showMessage(result.hitokoto, 6000, 9); setTimeout(() => { showMessage(text, 4000, 9); @@ -136,31 +137,29 @@ function loadWidget(config) { } function showMessage(text, timeout, priority) { - if (!text) return; - if (!sessionStorage.getItem("waifu-text") || sessionStorage.getItem("waifu-text") <= priority) { - if (messageTimer) { - clearTimeout(messageTimer); - 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); + if (!text || (sessionStorage.getItem("waifu-text") && sessionStorage.getItem("waifu-text") > priority)) return; + if (messageTimer) { + clearTimeout(messageTimer); + messageTimer = null; } + 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() { - var modelId = localStorage.getItem("modelId"), + let modelId = localStorage.getItem("modelId"), modelTexturesId = localStorage.getItem("modelTexturesId"); if (modelId === null) { // 首次访问加载 指定模型 的 指定材质 - var modelId = 1, // 模型 ID - modelTexturesId = 53; // 材质 ID + modelId = 1; // 模型 ID + modelTexturesId = 53; // 材质 ID } loadModel(modelId, modelTexturesId); fetch(waifuPath) @@ -169,7 +168,7 @@ function loadWidget(config) { result.mouseover.forEach(tips => { window.addEventListener("mouseover", event => { if (!event.target.matches(tips.selector)) return; - var text = randomSelection(tips.text); + let text = randomSelection(tips.text); text = text.replace("{text}", event.target.innerText); showMessage(text, 4000, 8); }); @@ -177,17 +176,17 @@ function loadWidget(config) { result.click.forEach(tips => { window.addEventListener("click", event => { if (!event.target.matches(tips.selector)) return; - var text = randomSelection(tips.text); + let text = randomSelection(tips.text); text = text.replace("{text}", event.target.innerText); showMessage(text, 4000, 8); }); }); result.seasons.forEach(tips => { - var now = new Date(), + let now = new Date(), after = tips.date.split("-")[0], 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])) { - var text = randomSelection(tips.text); + let text = randomSelection(tips.text); text = text.replace("{year}", now.getFullYear()); //showMessage(text, 7000, true); messageArray.push(text); @@ -217,7 +216,7 @@ function loadWidget(config) { } async function loadRandModel() { - var modelId = localStorage.getItem("modelId"), + let modelId = localStorage.getItem("modelId"), modelTexturesId = localStorage.getItem("modelTexturesId"); if (useCDN) { if (!modelList) await loadModelList(); @@ -236,7 +235,7 @@ function loadWidget(config) { } async function loadOtherModel() { - var modelId = localStorage.getItem("modelId"); + let modelId = localStorage.getItem("modelId"); if (useCDN) { if (!modelList) await loadModelList(); let index = (++modelId >= modelList.models.length) ? 0 : modelId; @@ -261,7 +260,7 @@ function initWidget(config, apiPath = "/") { document.body.insertAdjacentHTML("beforeend", `
看板娘
`); - var toggle = document.getElementById("waifu-toggle"); + let toggle = document.getElementById("waifu-toggle"); toggle.addEventListener("click", () => { toggle.classList.remove("waifu-toggle-active"); if (toggle.getAttribute("first-time")) {