live2d-widget/waifu-tips.js

251 lines
10 KiB
JavaScript
Raw Normal View History

2019-11-12 17:27:00 +08:00
/*
2019-12-29 23:04:20 +08:00
* Live2D Widget
* https://github.com/stevenjoezhang/live2d-widget
2019-11-12 17:27:00 +08:00
*/
function loadWidget(waifuPath, apiPath) {
localStorage.removeItem("waifu-display");
sessionStorage.removeItem("waifu-text");
2019-12-29 14:59:20 +08:00
document.body.insertAdjacentHTML("beforeend", `<div id="waifu">
2019-11-12 17:27:00 +08:00
<div id="waifu-tips"></div>
<canvas id="live2d" width="300" height="300"></canvas>
<div id="waifu-tool">
<span class="fa fa-lg fa-comment"></span>
<span class="fa fa-lg fa-paper-plane"></span>
<span class="fa fa-lg fa-user-circle"></span>
<span class="fa fa-lg fa-street-view"></span>
<span class="fa fa-lg fa-camera-retro"></span>
<span class="fa fa-lg fa-info-circle"></span>
<span class="fa fa-lg fa-times"></span>
</div>
</div>`);
2019-12-30 00:20:41 +08:00
// https://stackoverflow.com/questions/24148403/trigger-css-transition-on-appended-element
setTimeout(() => {
document.getElementById("waifu").style.bottom = 0;
}, 0);
2019-11-12 17:27:00 +08:00
function registerEventListener() {
2019-12-03 20:18:26 +08:00
document.querySelector("#waifu-tool .fa-comment").addEventListener("click", showHitokoto);
document.querySelector("#waifu-tool .fa-paper-plane").addEventListener("click", () => {
2019-11-12 17:27:00 +08:00
if (window.Asteroids) {
if (!window.ASTEROIDSPLAYERS) window.ASTEROIDSPLAYERS = [];
window.ASTEROIDSPLAYERS.push(new Asteroids());
} else {
2019-12-29 14:59:20 +08:00
var script = document.createElement("script");
2019-12-22 10:15:16 +08:00
script.src = "https://cdn.jsdelivr.net/gh/GalaxyMimi/CDN/asteroids.js";
document.head.appendChild(script);
2019-11-12 17:27:00 +08:00
}
});
2019-12-03 20:18:26 +08:00
document.querySelector("#waifu-tool .fa-user-circle").addEventListener("click", loadOtherModel);
document.querySelector("#waifu-tool .fa-street-view").addEventListener("click", loadRandModel);
document.querySelector("#waifu-tool .fa-camera-retro").addEventListener("click", () => {
2019-11-12 17:27:00 +08:00
showMessage("照好了嘛,是不是很可爱呢?", 6000, 9);
Live2D.captureName = "photo.png";
Live2D.captureFrame = true;
});
2019-12-03 20:18:26 +08:00
document.querySelector("#waifu-tool .fa-info-circle").addEventListener("click", () => {
2019-11-12 17:27:00 +08:00
open("https://github.com/stevenjoezhang/live2d-widget");
});
2019-12-03 20:18:26 +08:00
document.querySelector("#waifu-tool .fa-times").addEventListener("click", () => {
2019-12-01 01:34:53 +08:00
localStorage.setItem("waifu-display", Date.now());
2019-11-12 17:27:00 +08:00
showMessage("愿你有一天能与重要的人重逢。", 2000, 11);
2019-12-30 00:20:41 +08:00
document.getElementById("waifu").style.bottom = "-500px";
2019-12-29 19:40:35 +08:00
setTimeout(() => {
2019-12-30 00:20:41 +08:00
document.getElementById("waifu").style.display = "none";
2019-12-29 20:39:45 +08:00
document.getElementById("waifu-toggle").classList.add("waifu-toggle-active");
2019-12-29 19:40:35 +08:00
}, 3000);
2019-11-12 17:27:00 +08:00
});
var devtools = () => {};
console.log("%c", devtools);
devtools.toString = () => {
2019-11-12 17:27:00 +08:00
showMessage("哈哈,你打开了控制台,是想要看看我的小秘密吗?", 6000, 9);
};
2019-12-03 20:18:26 +08:00
window.addEventListener("copy", () => {
2019-11-12 17:27:00 +08:00
showMessage("你都复制了些什么呀,转载要记得加上出处哦!", 6000, 9);
});
2019-12-03 20:18:26 +08:00
window.addEventListener("visibilitychange", () => {
2019-11-12 17:27:00 +08:00
if (!document.hidden) showMessage("哇,你终于回来了~", 6000, 9);
});
}
registerEventListener();
function welcomeMessage() {
2019-12-29 15:13:38 +08:00
var text;
if (location.pathname === "/") { // 如果是主页
2019-11-12 17:27:00 +08:00
var now = new Date().getHours();
if (now > 5 && now <= 7) text = "早上好!一日之计在于晨,美好的一天就要开始了。";
else if (now > 7 && now <= 11) text = "上午好!工作顺利嘛,不要久坐,多起来走动走动哦!";
2019-12-29 14:59:20 +08:00
else if (now > 11 && now <= 13) text = "中午了,工作了一个上午,现在是午餐时间!";
else if (now > 13 && now <= 17) text = "午后很容易犯困呢,今天的运动目标完成了吗?";
2019-11-12 17:27:00 +08:00
else if (now > 17 && now <= 19) text = "傍晚了!窗外夕阳的景色很美丽呢,最美不过夕阳红~";
else if (now > 19 && now <= 21) text = "晚上好,今天过得怎么样?";
else if (now > 21 && now <= 23) text = ["已经这么晚了呀,早点休息吧,晚安~", "深夜时要爱护眼睛呀!"];
else text = "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?";
} else if (document.referrer !== "") {
2019-12-29 14:59:20 +08:00
var referrer = new URL(document.referrer),
domain = referrer.hostname.split(".")[1];
2019-12-26 11:38:44 +08:00
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 == "so") text = `Hello来自 360搜索 的朋友<br>你是搜索 <span>${referrer.search.split("&q=")[1].split("&")[0]}</span> 找到的我吗?`;
else if (domain == "google") text = `Hello来自 谷歌搜索 的朋友<br>欢迎阅读<span>「${document.title.split(" - ")[0]}」</span>`;
else text = `Hello来自 <span>${referrer.hostname}</span> 的朋友`;
2019-11-12 17:27:00 +08:00
} else {
2019-12-26 11:38:44 +08:00
text = `欢迎阅读<span>「${document.title.split(" - ")[0]}」</span>`;
2019-11-12 17:27:00 +08:00
}
showMessage(text, 7000, 8);
}
welcomeMessage();
2019-12-29 23:39:46 +08:00
// 检测用户活动状态,并在空闲时显示消息
2019-11-12 17:27:00 +08:00
var userAction = false,
2019-12-29 23:39:46 +08:00
userActionTimer = null,
2019-11-12 17:27:00 +08:00
messageTimer = null,
2019-12-30 00:20:41 +08:00
messageArray = ["好久不见,日子过得好快呢……", "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!", "记得把小家加入 Adblock 白名单哦!"];
window.addEventListener("mousemove", () => userAction = true);
window.addEventListener("keydown", () => userAction = true);
2019-11-12 17:27:00 +08:00
setInterval(() => {
2019-12-29 23:39:46 +08:00
if (userAction) {
2019-11-12 17:27:00 +08:00
userAction = false;
2019-12-29 23:39:46 +08:00
clearInterval(userActionTimer);
userActionTimer = null;
} else if (!userActionTimer) {
userActionTimer = setInterval(() => {
showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9);
}, 20000);
2019-11-12 17:27:00 +08:00
}
}, 1000);
function showHitokoto() {
// 增加 hitokoto.cn 的 API
2019-12-29 23:39:46 +08:00
fetch("https://v1.hitokoto.cn")
2019-12-04 18:30:44 +08:00
.then(response => response.json())
.then(result => {
2019-12-26 11:38:44 +08:00
var text = `这句一言来自 <span>「${result.from}」</span>,是 <span>${result.creator}</span> 在 hitokoto.cn 投稿的。`;
2019-12-04 18:30:44 +08:00
showMessage(result.hitokoto, 6000, 9);
setTimeout(() => {
showMessage(text, 4000, 9);
}, 6000);
});
2019-11-12 17:27:00 +08:00
}
function showMessage(text, timeout, priority) {
if (!text) return;
if (!sessionStorage.getItem("waifu-text") || sessionStorage.getItem("waifu-text") <= priority) {
if (messageTimer) {
clearTimeout(messageTimer);
messageTimer = null;
}
if (Array.isArray(text)) text = text[Math.floor(Math.random() * text.length)];
sessionStorage.setItem("waifu-text", priority);
2019-12-29 23:04:20 +08:00
var tips = document.getElementById("waifu-tips");
tips.innerHTML = text;
tips.classList.add("waifu-tips-active");
2019-11-12 17:27:00 +08:00
messageTimer = setTimeout(() => {
sessionStorage.removeItem("waifu-text");
2019-12-29 23:04:20 +08:00
tips.classList.remove("waifu-tips-active");
2019-11-12 17:27:00 +08:00
}, timeout);
}
}
function initModel() {
var modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId");
if (modelId == null) {
// 首次访问加载 指定模型 的 指定材质
var modelId = 1, // 模型 ID
modelTexturesId = 53; // 材质 ID
2019-11-12 17:27:00 +08:00
}
loadModel(modelId, modelTexturesId);
2019-12-04 18:30:44 +08:00
fetch(waifuPath)
.then(response => response.json())
.then(result => {
result.mouseover.forEach(tips => {
2019-12-04 23:56:02 +08:00
window.addEventListener("mouseover", event => {
if (!event.target.matches(tips.selector)) return;
2019-12-04 18:30:44 +08:00
var text = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text;
2019-12-04 23:56:02 +08:00
text = text.replace("{text}", event.target.innerText);
2019-12-04 18:30:44 +08:00
showMessage(text, 4000, 8);
});
2019-11-12 17:27:00 +08:00
});
2019-12-04 18:30:44 +08:00
result.click.forEach(tips => {
2019-12-04 23:56:02 +08:00
window.addEventListener("click", event => {
if (!event.target.matches(tips.selector)) return;
2019-12-04 18:30:44 +08:00
var text = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text;
2019-12-04 23:56:02 +08:00
text = text.replace("{text}", event.target.innerText);
2019-12-04 18:30:44 +08:00
showMessage(text, 4000, 8);
});
});
result.seasons.forEach(tips => {
var 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 = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text;
text = text.replace("{year}", now.getFullYear());
//showMessage(text, 7000, true);
messageArray.push(text);
}
2019-11-12 17:27:00 +08:00
});
});
}
initModel();
function loadModel(modelId, modelTexturesId) {
localStorage.setItem("modelId", modelId);
if (modelTexturesId === undefined) modelTexturesId = 0;
localStorage.setItem("modelTexturesId", modelTexturesId);
loadlive2d("live2d", `${apiPath}/get/?id=${modelId}-${modelTexturesId}`, console.log(`Live2D 模型 ${modelId}-${modelTexturesId} 加载完成`));
}
function loadRandModel() {
var modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId");
// 可选 "rand"(随机), "switch"(顺序)
2019-12-04 18:30:44 +08:00
fetch(`${apiPath}/rand_textures/?id=${modelId}-${modelTexturesId}`)
.then(response => response.json())
.then(result => {
2019-12-29 14:41:16 +08:00
if (result.textures.id == 1 && (modelTexturesId == 1 || modelTexturesId == 0)) showMessage("我还没有其他衣服呢!", 4000, 10);
2019-11-12 17:27:00 +08:00
else showMessage("我的新衣服好看嘛?", 4000, 10);
2019-12-29 14:41:16 +08:00
loadModel(modelId, result.textures.id);
2019-12-04 18:30:44 +08:00
});
2019-11-12 17:27:00 +08:00
}
function loadOtherModel() {
var modelId = localStorage.getItem("modelId");
2019-12-04 18:30:44 +08:00
fetch(`${apiPath}/switch/?id=${modelId}`)
.then(response => response.json())
.then(result => {
2019-12-29 14:41:16 +08:00
loadModel(result.model.id);
showMessage(result.model.message, 4000, 10);
2019-12-04 18:30:44 +08:00
});
2019-11-12 17:27:00 +08:00
}
}
function initWidget(waifuPath = "/waifu-tips.json", apiPath = "") {
if (screen.width <= 768) return;
2019-12-29 20:12:58 +08:00
document.body.insertAdjacentHTML("beforeend", `<div id="waifu-toggle">
2019-11-12 17:27:00 +08:00
<span>看板娘</span>
</div>`);
2019-12-29 20:39:45 +08:00
var toggle = document.getElementById("waifu-toggle");
toggle.addEventListener("click", () => {
toggle.classList.remove("waifu-toggle-active");
if (toggle.getAttribute("first-time")) {
2019-11-12 17:27:00 +08:00
loadWidget(waifuPath, apiPath);
2019-12-29 20:39:45 +08:00
toggle.removeAttribute("first-time");
2019-11-12 17:27:00 +08:00
} else {
localStorage.removeItem("waifu-display");
2019-12-30 00:20:41 +08:00
document.getElementById("waifu").style.display = "";
setTimeout(() => {
document.getElementById("waifu").style.bottom = 0;
}, 0);
2019-11-12 17:27:00 +08:00
}
});
2019-12-01 01:34:53 +08:00
if (localStorage.getItem("waifu-display") && Date.now() - localStorage.getItem("waifu-display") <= 86400000) {
2019-12-29 20:39:45 +08:00
toggle.setAttribute("first-time", true);
2019-12-29 20:12:58 +08:00
setTimeout(() => {
2019-12-29 20:39:45 +08:00
toggle.classList.add("waifu-toggle-active");
2019-12-29 20:12:58 +08:00
}, 0);
2019-11-12 17:27:00 +08:00
} else {
loadWidget(waifuPath, apiPath);
}
}