From 9f4ac1cf5bfc2dfe0a98ae0f549842e5d04dba4f Mon Sep 17 00:00:00 2001
From: Mimi <1119186082@qq.com>
Date: Thu, 20 Oct 2022 22:40:44 +0800
Subject: [PATCH] Refactor welcomeMessage
---
src/waifu-tips.js | 34 ++++++++++++++++------------------
waifu-tips.json | 25 +++++++++++++++++++++++++
2 files changed, 41 insertions(+), 18 deletions(-)
diff --git a/src/waifu-tips.js b/src/waifu-tips.js
index ea54ad2..1d4ef48 100755
--- a/src/waifu-tips.js
+++ b/src/waifu-tips.js
@@ -67,21 +67,21 @@ function loadWidget(config) {
});
})();
- function welcomeMessage() {
+ function welcomeMessage(time) {
const message = `欢迎阅读「${document.title.split(" - ")[0]}」`;
let text;
if (location.pathname === "/") { // 如果是主页
- const now = new Date().getHours();
- if (now > 5 && now <= 7) text = "早上好!一日之计在于晨,美好的一天就要开始了。";
- else if (now > 7 && now <= 11) text = "上午好!工作顺利嘛,不要久坐,多起来走动走动哦!";
- else if (now > 11 && now <= 13) text = "中午了,工作了一个上午,现在是午餐时间!";
- else if (now > 13 && now <= 17) text = "午后很容易犯困呢,今天的运动目标完成了吗?";
- else if (now > 17 && now <= 19) text = "傍晚了!窗外夕阳的景色很美丽呢,最美不过夕阳红~";
- else if (now > 19 && now <= 21) text = "晚上好,今天过得怎么样?";
- else if (now > 21 && now <= 23) text = ["已经这么晚了呀,早点休息吧,晚安~", "深夜时要爱护眼睛呀!"];
- else text = "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?";
- return text;
- } else if (document.referrer !== "") {
+ for (let { hour, text } of time) {
+ const now = new Date(),
+ after = hour.split("-")[0],
+ before = hour.split("-")[1] || after;
+ if (after <= now.getHours() && now.getHours() <= before) {
+ text = randomSelection(text);
+ return text;
+ }
+ }
+ }
+ if (document.referrer !== "") {
const referrer = new URL(document.referrer),
domain = referrer.hostname.split(".")[1];
const domains = {
@@ -93,12 +93,10 @@ function loadWidget(config) {
if (domain in domains) text = domains[domain];
else text = referrer.hostname;
- return `Hello!来自 ${text} 的朋友
${message}`;;
- } else {
- return message;
+ return `Hello!来自 ${text} 的朋友
${message}`;
}
- };
- showMessage(welcomeMessage(), 7000, 8);
+ return message;
+ }
(function initModel() {
let modelId = localStorage.getItem("modelId"),
@@ -112,6 +110,7 @@ function loadWidget(config) {
fetch(config.waifuPath)
.then(response => response.json())
.then(result => {
+ showMessage(welcomeMessage(result.time), 7000, 11);
window.addEventListener("mouseover", event => {
for (let { selector, text } of result.mouseover) {
if (!event.target.matches(selector)) continue;
@@ -137,7 +136,6 @@ function loadWidget(config) {
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());
- //showMessage(text, 7000, true);
messageArray.push(text);
}
});
diff --git a/waifu-tips.json b/waifu-tips.json
index 7fd2972..7d50c73 100755
--- a/waifu-tips.json
+++ b/waifu-tips.json
@@ -220,5 +220,30 @@
}, {
"date": "12/20-12/31",
"text": "这几天是圣诞节,主人肯定又去剁手买买买了~"
+ }],
+ "time": [{
+ "hour": "6-7",
+ "text": "早上好!一日之计在于晨,美好的一天就要开始了~"
+ }, {
+ "hour": "8-11",
+ "text": "上午好!工作顺利嘛,不要久坐,多起来走动走动哦!"
+ }, {
+ "hour": "12-13",
+ "text": "中午了,工作了一个上午,现在是午餐时间!"
+ }, {
+ "hour": "14-17",
+ "text": "午后很容易犯困呢,今天的运动目标完成了吗?"
+ }, {
+ "hour": "18-19",
+ "text": "傍晚了!窗外夕阳的景色很美丽呢,最美不过夕阳红~"
+ }, {
+ "hour": "20-21",
+ "text": "晚上好,今天过得怎么样?"
+ }, {
+ "hour": "22-23",
+ "text": ["已经这么晚了呀,早点休息吧,晚安~", "深夜时要爱护眼睛呀!"]
+ }, {
+ "hour": "0-5",
+ "text": "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?"
}]
}