live2d-widget/waifu-tips.js

251 lines
11 KiB
JavaScript
Raw Normal View History

2018-11-13 17:28:01 +00:00
/*
* https://imjad.cn/archives/lab/add-dynamic-poster-girl-with-live2d-to-your-blog-02
* https://www.fghrsh.net/post/123.html
*/
function loadWidget(waifuPath, apiPath) {
2018-12-11 11:57:55 +00:00
localStorage.removeItem("waifu-display");
2018-07-11 05:25:04 +00:00
sessionStorage.removeItem("waifu-text");
2019-02-06 09:27:05 +00:00
$("body").append(`<div id="waifu">
<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>`);
$("#waifu").show().animate({ bottom: 0 }, 3000);
2019-06-13 05:34:47 +00:00
function registerEventListener() {
$("#waifu-tool .fa-comment").click(showHitokoto);
2019-08-06 17:14:34 +00:00
$("#waifu-tool .fa-paper-plane").click(() => {
2019-08-06 17:12:42 +00:00
if (window.Asteroids) {
if (!window.ASTEROIDSPLAYERS) window.ASTEROIDSPLAYERS = [];
window.ASTEROIDSPLAYERS.push(new Asteroids());
} else {
$.ajax({
url: "https://cdn.jsdelivr.net/gh/GalaxyMimi/CDN/asteroids.js",
dataType: "script",
cache: true
});
}
2018-11-13 17:28:01 +00:00
});
2019-06-13 05:34:47 +00:00
$("#waifu-tool .fa-user-circle").click(loadOtherModel);
$("#waifu-tool .fa-street-view").click(loadRandModel);
2019-08-06 17:14:34 +00:00
$("#waifu-tool .fa-camera-retro").click(() => {
2019-06-13 05:34:47 +00:00
showMessage("照好了嘛,是不是很可爱呢?", 6000, 9);
2019-06-13 06:42:07 +00:00
Live2D.captureName = "photo.png";
Live2D.captureFrame = true;
2019-06-13 05:34:47 +00:00
});
2019-08-06 17:14:34 +00:00
$("#waifu-tool .fa-info-circle").click(() => {
2019-06-13 06:42:07 +00:00
open("https://github.com/stevenjoezhang/live2d-widget");
2019-06-13 05:34:47 +00:00
});
2019-08-06 17:14:34 +00:00
$("#waifu-tool .fa-times").click(() => {
2019-06-13 05:34:47 +00:00
localStorage.setItem("waifu-display", new Date().getTime());
showMessage("愿你有一天能与重要的人重逢。", 2000, 11);
2019-08-06 17:14:34 +00:00
$("#waifu").animate({ bottom: -500 }, 3000, () => {
2019-06-13 05:34:47 +00:00
$("#waifu").hide();
$("#waifu-toggle").show().animate({ "margin-left": -50 }, 1000);
2019-06-13 05:34:47 +00:00
});
});
var re = /x/;
console.log(re);
2019-08-06 17:14:34 +00:00
re.toString = () => {
2019-08-06 17:43:23 +00:00
showMessage("哈哈,你打开了控制台,是想要看看我的小秘密吗?", 6000, 9);
2019-06-13 05:34:47 +00:00
return "";
};
2019-08-06 17:14:34 +00:00
$(document).on("copy", () => {
2019-06-13 05:34:47 +00:00
showMessage("你都复制了些什么呀,转载要记得加上出处哦!", 6000, 9);
});
2019-08-06 17:14:34 +00:00
$(document).on("visibilitychange", () => {
2019-06-13 05:34:47 +00:00
if (!document.hidden) showMessage("哇,你终于回来了~", 6000, 9);
});
}
registerEventListener();
function welcomeMessage() {
2019-02-06 09:27:05 +00:00
var SiteIndexUrl = location.port ? `${location.protocol}//${location.hostname}:${location.port}/` : `${location.protocol}//${location.hostname}/`, text; //自动获取主页
2018-12-10 09:19:13 +00:00
if (location.href == SiteIndexUrl) { //如果是主页
2019-02-06 09:27:05 +00:00
var now = new Date().getHours();
2019-08-06 17:49:39 +00:00
if (now > 5 && now <= 7) text = "早上好!一日之计在于晨,美好的一天就要开始了。";
2019-01-20 17:53:34 +00:00
else if (now > 7 && now <= 11) text = "上午好!工作顺利嘛,不要久坐,多起来走动走动哦!";
else if (now > 11 && now <= 14) text = "中午了,工作了一个上午,现在是午餐时间!";
else if (now > 14 && now <= 17) text = "午后很容易犯困呢,今天的运动目标完成了吗?";
else if (now > 17 && now <= 19) text = "傍晚了!窗外夕阳的景色很美丽呢,最美不过夕阳红~";
else if (now > 19 && now <= 21) text = "晚上好,今天过得怎么样?";
else if (now > 21 && now <= 23) text = ["已经这么晚了呀,早点休息吧,晚安~", "深夜时要爱护眼睛呀!"];
2019-08-06 17:49:39 +00:00
else text = "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?";
} else if (document.referrer !== "") {
2019-02-06 09:27:05 +00:00
var referrer = document.createElement("a");
referrer.href = document.referrer;
var domain = referrer.hostname.split(".")[1];
2019-06-13 05:49:59 +00:00
if (location.hostname == referrer.hostname) text = `欢迎阅读<span style="color:#0099cc;">『${document.title.split(" - ")[0]}』</span>`;
2019-08-06 17:49:39 +00:00
else if (domain == "baidu") text = `Hello来自 百度搜索 的朋友<br>你是搜索 <span style="color:#0099cc;">${referrer.search.split("&wd=")[1].split("&")[0]}</span> 找到的我吗?`;
else if (domain == "so") text = `Hello来自 360搜索 的朋友<br>你是搜索 <span style="color:#0099cc;">${referrer.search.split("&q=")[1].split("&")[0]}</span> 找到的我吗?`;
else if (domain == "google") text = `Hello来自 谷歌搜索 的朋友<br>欢迎阅读<span style="color:#0099cc;">『${document.title.split(" - ")[0]}』</span>`;
2019-06-13 05:49:59 +00:00
else text = `Hello来自 <span style="color:#0099cc;">${referrer.hostname}</span> 的朋友`;
2019-08-06 17:49:39 +00:00
} else {
2019-06-13 05:49:59 +00:00
text = `欢迎阅读<span style="color:#0099cc;">『${document.title.split(" - ")[0]}』</span>`;
2019-06-13 05:34:47 +00:00
}
2018-07-11 05:25:04 +00:00
showMessage(text, 7000, 8);
2019-06-13 05:34:47 +00:00
}
welcomeMessage();
2019-02-06 09:27:05 +00:00
//检测用户活动状态,并在空闲时定时显示一言
var userAction = false,
2018-07-11 05:25:04 +00:00
hitokotoTimer = null,
messageTimer = null,
2019-11-12 09:26:03 +00:00
messageArray = ["好久不见,日子过得好快呢……", "大坏蛋!你都多久没理人家了呀,嘤嘤嘤~", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!"];
if ($(".fa-share-alt").is(":hidden")) messageArray.push("记得把小家加入 Adblock 白名单哦!");
2019-08-06 17:14:34 +00:00
$(document).mousemove(() => {
2019-02-06 09:27:05 +00:00
userAction = true;
2019-08-06 17:14:34 +00:00
}).keydown(() => {
2019-02-06 09:27:05 +00:00
userAction = true;
2018-07-11 05:25:04 +00:00
});
2019-08-06 17:14:34 +00:00
setInterval(() => {
2019-02-06 09:27:05 +00:00
if (!userAction) {
2019-01-20 17:53:34 +00:00
if (!hitokotoTimer) hitokotoTimer = setInterval(showHitokoto, 25000);
2019-08-06 17:49:39 +00:00
} else {
2019-02-06 09:27:05 +00:00
userAction = false;
2018-07-11 05:25:04 +00:00
clearInterval(hitokotoTimer);
hitokotoTimer = null;
}
}, 1000);
function showHitokoto() {
2019-02-06 09:27:05 +00:00
//增加 hitokoto.cn 的 API
2019-01-20 17:53:34 +00:00
if (Math.random() < 0.6 && messageArray.length > 0) showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9);
else $.getJSON("https://v1.hitokoto.cn", function(result) {
2019-01-01 07:08:33 +00:00
var text = `这句一言来自 <span style="color:#0099cc;">『${result.from}』</span>,是 <span style="color:#0099cc;">${result.creator}</span> 在 hitokoto.cn 投稿的。`;
2018-07-11 05:25:04 +00:00
showMessage(result.hitokoto, 6000, 9);
2019-08-06 17:14:34 +00:00
setTimeout(() => {
2018-07-11 05:25:04 +00:00
showMessage(text, 4000, 9);
}, 6000);
});
}
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);
2018-11-13 17:28:01 +00:00
$("#waifu-tips").stop().html(text).fadeTo(200, 1);
2019-08-06 17:14:34 +00:00
messageTimer = setTimeout(() => {
2018-07-11 05:25:04 +00:00
sessionStorage.removeItem("waifu-text");
2018-11-13 17:28:01 +00:00
$("#waifu-tips").fadeTo(1000, 0);
2018-07-11 05:25:04 +00:00
}, timeout);
}
}
2018-07-11 07:38:35 +00:00
function initModel() {
2018-07-11 05:25:04 +00:00
var modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId");
if (modelId == null) {
2018-12-10 09:19:13 +00:00
//首次访问加载 指定模型 的 指定材质
var modelId = 1, //模型 ID
modelTexturesId = 53; //材质 ID
2018-07-11 05:25:04 +00:00
}
loadModel(modelId, modelTexturesId);
2019-02-06 09:27:05 +00:00
$.getJSON(waifuPath, function(result) {
$.each(result.mouseover, function(index, tips) {
$(document).on("mouseover", tips.selector, function() {
var text = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text;
text = text.replace("{text}", $(this).text());
showMessage(text, 4000, 8);
2018-07-11 05:25:04 +00:00
});
2019-02-06 09:27:05 +00:00
});
$.each(result.click, function(index, tips) {
$(document).on("click", tips.selector, function() {
var text = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text;
text = text.replace("{text}", $(this).text());
showMessage(text, 4000, 8);
2018-07-11 05:25:04 +00:00
});
2019-02-06 09:27:05 +00:00
});
$.each(result.seasons, function(index, 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])) {
2019-02-10 00:53:00 +00:00
var text = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text;
2019-02-06 09:27:05 +00:00
text = text.replace("{year}", now.getFullYear());
//showMessage(text, 7000, true);
messageArray.push(text);
}
});
2018-07-11 05:25:04 +00:00
});
}
2019-06-13 05:49:59 +00:00
initModel();
2018-07-11 05:25:04 +00:00
function loadModel(modelId, modelTexturesId) {
localStorage.setItem("modelId", modelId);
if (modelTexturesId === undefined) modelTexturesId = 0;
localStorage.setItem("modelTexturesId", modelTexturesId);
2019-06-13 06:42:07 +00:00
loadlive2d("live2d", `${apiPath}/get/?id=${modelId}-${modelTexturesId}`, console.log(`Live2D 模型 ${modelId}-${modelTexturesId} 加载完成`));
2018-07-11 05:25:04 +00:00
}
function loadRandModel() {
var modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId");
2018-12-10 09:19:13 +00:00
//可选 "rand"(随机), "switch"(顺序)
2018-07-11 05:25:04 +00:00
$.ajax({
cache: false,
2019-06-13 05:49:59 +00:00
url: `${apiPath}/rand_textures/?id=${modelId}-${modelTexturesId}`,
2018-07-11 05:25:04 +00:00
dataType: "json",
success: function(result) {
2019-01-20 17:53:34 +00:00
if (result.textures["id"] == 1 && (modelTexturesId == 1 || modelTexturesId == 0)) showMessage("我还没有其他衣服呢!", 4000, 10);
else showMessage("我的新衣服好看嘛?", 4000, 10);
2018-07-11 05:25:04 +00:00
loadModel(modelId, result.textures["id"]);
}
});
}
function loadOtherModel() {
var modelId = localStorage.getItem("modelId");
$.ajax({
cache: false,
2019-06-13 05:49:59 +00:00
url: `${apiPath}/switch/?id=${modelId}`,
2018-07-11 05:25:04 +00:00
dataType: "json",
success: function(result) {
loadModel(result.model["id"]);
showMessage(result.model["message"], 4000, 10);
}
});
}
2018-07-11 07:38:35 +00:00
}
function initWidget(waifuPath = "/waifu-tips.json", apiPath = "") {
if (screen.width <= 768) return;
$("body").append(`<div id="waifu-toggle" style="margin-left: -100px;">
<span>看板娘</span>
</div>`);
2019-08-06 17:14:34 +00:00
$("#waifu-toggle").hover(() => {
$("#waifu-toggle").animate({ "margin-left": -30 }, 500);
2019-08-06 17:14:34 +00:00
}, () => {
$("#waifu-toggle").animate({ "margin-left": -50 }, 500);
2019-08-06 17:14:34 +00:00
}).click(() => {
$("#waifu-toggle").animate({ "margin-left": -100 }, 1000, () => {
$("#waifu-toggle").hide();
});
if ($("#waifu-toggle").attr("first-time")) {
loadWidget(waifuPath, apiPath);
$("#waifu-toggle").attr("first-time", false);
2019-08-06 17:49:39 +00:00
} else {
localStorage.removeItem("waifu-display");
$("#waifu").show().animate({ bottom: 0 }, 3000);
}
});
if (localStorage.getItem("waifu-display") && new Date().getTime() - localStorage.getItem("waifu-display") <= 86400000) {
$("#waifu-toggle").attr("first-time", true).css({ "margin-left": -50 });
2019-08-06 17:49:39 +00:00
} else {
loadWidget(waifuPath, apiPath);
}
}