Refactor welcomeMessage

This commit is contained in:
Mimi 2022-10-20 22:40:44 +08:00
parent 477be78693
commit 9f4ac1cf5b
2 changed files with 41 additions and 18 deletions

View File

@ -67,21 +67,21 @@ function loadWidget(config) {
});
})();
function welcomeMessage() {
function welcomeMessage(time) {
const message = `欢迎阅读<span>「${document.title.split(" - ")[0]}」</span>`;
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 = "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?";
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;
} else if (document.referrer !== "") {
}
}
}
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来自 <span>${text}</span> 的朋友<br>${message}`;;
} else {
return `Hello来自 <span>${text}</span> 的朋友<br>${message}`;
}
return message;
}
};
showMessage(welcomeMessage(), 7000, 8);
(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);
}
});

View File

@ -220,5 +220,30 @@
}, {
"date": "12/20-12/31",
"text": "这几天是<span>圣诞节</span>,主人肯定又去剁手买买买了~"
}],
"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": "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?"
}]
}