live2d-widget/waifu-tips.js

289 lines
11 KiB
JavaScript
Raw Normal View History

2018-11-14 01:28:01 +08:00
/*
* https://imjad.cn/archives/lab/add-dynamic-poster-girl-with-live2d-to-your-blog-02
* https://www.fghrsh.net/post/123.html
*/
2018-07-11 15:38:35 +08:00
function initWidget(waifuPath, apiPath) {
2018-11-14 01:28:01 +08:00
if (screen.width <= 768) return;
2018-12-11 19:57:55 +08:00
if (localStorage.getItem("waifu-display")) {
if (new Date().getTime() - localStorage.getItem("waifu-display") <= 86400000) return;
2018-11-14 01:28:01 +08:00
}
2018-12-11 19:57:55 +08:00
localStorage.removeItem("waifu-display");
2018-07-11 13:25:04 +08:00
sessionStorage.removeItem("waifu-text");
2018-11-14 01:28:01 +08:00
$("body").append('<div id="waifu">\
<div id="waifu-tips"></div>\
<canvas id="live2d" width="300" height="300"></canvas>\
<div id="waifu-tool">\
2018-07-11 13:25:04 +08:00
<span class="fa fa-lg fa-home"></span>\
<span class="fa fa-lg fa-comment"></span>\
<span class="fa fa-lg fa-paper-plane"></span>\
2018-12-18 00:01:42 +08:00
<span class="fa fa-lg fa-user-circle"></span>\
2018-07-11 13:25:04 +08:00
<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>');
var re = /x/,
OriginTitile = document.title,
2018-11-14 01:28:01 +08:00
titleTime = null,
titleIndex = 0,
title = [{
blur: "(●—●) 哎呦,崩溃啦!",
focus: "o(≧∇≦o) 啊咧,又好了……"
}, {
blur: "(つェ⊂) 看不到我~",
focus: "(*´∇`*) 被发现啦~"
}, {
blur: "(>_<) 我藏好了哦",
focus: "(*´∇`*) 被发现啦~"
}];
2018-07-11 13:25:04 +08:00
console.log(re);
re.toString = function() {
showMessage("哈哈,你打开了控制台,是想要看看我的秘密吗?", 6000, 9);
return "";
};
$(document).on("visibilitychange", function() {
if (document.hidden) {
2018-11-14 01:28:01 +08:00
titleIndex = Math.floor(Math.random() * 3);
document.title = title[titleIndex].blur;
if (titleTime) {
clearTimeout(titleTime);
titleTime = null;
}
2018-07-11 13:25:04 +08:00
} else {
2018-11-14 01:28:01 +08:00
document.title = title[titleIndex].focus;
showMessage("哇,你又回来了~", 6000, 9);
2018-07-11 13:25:04 +08:00
titleTime = setTimeout(function() {
document.title = OriginTitile;
2018-11-14 01:28:01 +08:00
}, 3000);
2018-07-11 13:25:04 +08:00
}
});
$(document).on("copy", function() {
2018-12-18 00:01:42 +08:00
showMessage("你都复制了些什么呀,转载要记得加上出处哦!", 6000, 9);
2018-07-11 13:25:04 +08:00
});
2018-11-14 01:28:01 +08:00
$("#waifu-tool .fa-home").click(function() {
2018-07-11 13:25:04 +08:00
location.href = "/";
});
2018-11-14 01:28:01 +08:00
$("#waifu-tool .fa-comment").click(function() {
2018-07-11 13:25:04 +08:00
showHitokoto();
});
2018-11-14 01:28:01 +08:00
$("#waifu-tool .fa-paper-plane").click(function() {
2018-07-11 13:25:04 +08:00
var s = document.createElement("script");
document.body.appendChild(s);
s.src = "https://galaxymimi.com/js/asteroids.js";
});
2018-11-14 01:28:01 +08:00
$("#waifu-tool .fa-user-circle-o").click(function() {
2018-07-11 13:25:04 +08:00
loadOtherModel();
});
2018-11-14 01:28:01 +08:00
$("#waifu-tool .fa-street-view").click(function() {
2018-07-11 13:25:04 +08:00
loadRandModel();
});
2018-11-14 01:28:01 +08:00
$("#waifu-tool .fa-camera-retro").click(function() {
2018-07-11 13:25:04 +08:00
showMessage("照好了嘛,是不是很可爱呢?", 6000, 9);
window.Live2D.captureName = "photo.png";
window.Live2D.captureFrame = true;
});
2018-11-14 01:28:01 +08:00
$("#waifu-tool .fa-info-circle").click(function() {
2018-12-10 17:19:13 +08:00
window.open("https://github.com/stevenjoezhang/live2d-widget");
2018-07-11 13:25:04 +08:00
});
2018-11-14 01:28:01 +08:00
$("#waifu-tool .fa-times").click(function() {
2018-12-11 19:57:55 +08:00
localStorage.setItem("waifu-display", new Date().getTime());
2018-12-18 00:01:42 +08:00
showMessage("愿你有一天能与重要的人重逢。", 2000, 11);
$("#waifu").animate({bottom: -500}, 3000, function() {
2018-11-14 01:28:01 +08:00
$("#waifu").hide();
});
2018-07-11 13:25:04 +08:00
});
(function() {
var text,
2018-12-10 17:19:13 +08:00
SiteIndexUrl = location.port ? `${location.protocol}//${location.hostname}:${location.port}/` : `${location.protocol}//${location.hostname}/`; //自动获取主页
//var SiteIndexUrl = "https://www.fghrsh.net/"; //手动指定主页
if (location.href == SiteIndexUrl) { //如果是主页
2018-07-11 13:25:04 +08:00
var now = (new Date()).getHours();
if (now > 23 || now <= 5) {
2018-12-18 00:01:42 +08:00
text = "你是夜猫子呀?这么晚还不睡觉,明天起的来嘛?";
2018-07-11 13:25:04 +08:00
} else if (now > 5 && now <= 7) {
2018-12-18 00:01:42 +08:00
text = "早上好!一日之计在于晨,美好的一天就要开始了。";
2018-07-11 13:25:04 +08: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) {
2018-12-18 00:01:42 +08:00
text = ["已经这么晚了呀,早点休息吧,晚安~", "深夜时要爱护眼睛呀!"];
2018-07-11 13:25:04 +08:00
} else {
2018-12-18 00:01:42 +08:00
text = "好久不见,日子过得好快呢……";
2018-07-11 13:25:04 +08:00
}
} else {
if (document.referrer !== "") {
var referrer = document.createElement("a");
referrer.href = document.referrer;
var domain = referrer.hostname.split(".")[1];
if (location.hostname == referrer.hostname) {
text = '欢迎阅读<span style="color:#0099cc;">『' + document.title.split(' - ')[0] + '』</span>';
} 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>';
} else {
text = 'Hello! 来自 <span style="color:#0099cc;">' + referrer.hostname + '</span> 的朋友';
}
} else {
text = '欢迎阅读<span style="color:#0099cc;">『' + document.title.split(' - ')[0] + '』</span>';
}
}
showMessage(text, 7000, 8);
})();
2018-12-10 17:19:13 +08:00
//检测用户活动状态,并在空闲时 定时显示一言
2018-07-11 13:25:04 +08:00
var getActed = false,
hitokotoTimer = null,
messageTimer = null,
2018-12-18 00:01:42 +08:00
messageArray = ["已经过了这么久了呀,日子过得好快呢……", "使用Chrome可以获得最佳浏览体验哦", "嗨~快来逗我玩吧!", "拿小拳拳锤你胸口!"],
2018-07-11 13:25:04 +08:00
apiURL = "";
2018-12-18 00:01:42 +08:00
if ($(".fa-share-alt").is(":hidden")) messageArray.push("记得把小家加入Adblock白名单哦");
2018-07-11 13:25:04 +08:00
$(document).mousemove(function() {
getActed = true;
}).keydown(function() {
getActed = true;
});
//hitokotoTimer = setInterval(showHitokoto, 30000);
setInterval(function() {
if (!getActed) {
if (!hitokotoTimer) {
hitokotoTimer = setInterval(showHitokoto, 25000);
}
} else {
getActed = false;
clearInterval(hitokotoTimer);
hitokotoTimer = null;
}
}, 1000);
function showHitokoto() {
2018-12-10 17:19:13 +08:00
//增加 hitokoto.cn API
2018-07-11 13:25:04 +08: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 15:08:33 +08:00
var text = `这句一言来自 <span style="color:#0099cc;">『${result.from}』</span>,是 <span style="color:#0099cc;">${result.creator}</span> 在 hitokoto.cn 投稿的。`;
2018-07-11 13:25:04 +08:00
showMessage(result.hitokoto, 6000, 9);
setTimeout(function() {
showMessage(text, 4000, 9);
}, 6000);
});
}
function showMessage(text, timeout, priority) {
//console.log(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)];
//console.log(text);
sessionStorage.setItem("waifu-text", priority);
2018-11-14 01:28:01 +08:00
$("#waifu-tips").stop().html(text).fadeTo(200, 1);
2018-07-11 13:25:04 +08:00
messageTimer = setTimeout(function() {
sessionStorage.removeItem("waifu-text");
2018-11-14 01:28:01 +08:00
$("#waifu-tips").fadeTo(1000, 0);
2018-07-11 13:25:04 +08:00
}, timeout);
}
}
2018-07-11 15:38:35 +08:00
function initModel() {
2018-11-14 01:28:01 +08:00
waifuPath = waifuPath || "/waifu-tips.json";
2018-11-02 23:04:54 +08:00
apiURL = apiPath || "";
2018-07-11 13:25:04 +08:00
var modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId");
if (modelId == null) {
2018-12-10 17:19:13 +08:00
//首次访问加载 指定模型 的 指定材质
var modelId = 1, //模型 ID
modelTexturesId = 53; //材质 ID
2018-07-11 13:25:04 +08:00
}
loadModel(modelId, modelTexturesId);
$.ajax({
cache: true,
2018-11-03 18:52:03 +08:00
url: waifuPath,
2018-07-11 13:25:04 +08:00
dataType: "json",
success: function(result) {
$.each(result.mouseover, function(index, tips) {
$(document).on("mouseover", tips.selector, function() {
var text = tips.text;
if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
2019-01-01 15:08:33 +08:00
text = text.replace("{text}", $(this).text());
2018-07-11 13:25:04 +08:00
showMessage(text, 4000, 8);
});
});
$.each(result.click, function(index, tips) {
$(document).on("click", tips.selector, function() {
var text = tips.text;
if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
2019-01-01 15:08:33 +08:00
text = text.replace("{text}", $(this).text());
2018-07-11 13:25:04 +08:00
showMessage(text, 4000, 8);
});
});
$.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])) {
var text = tips.text;
2019-01-01 15:08:33 +08:00
if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
text = text.replace("{year}", now.getFullYear());
2018-07-11 13:25:04 +08:00
//showMessage(text, 7000, true);
messageArray.push(text);
}
});
}
});
}
function loadModel(modelId, modelTexturesId) {
localStorage.setItem("modelId", modelId);
if (modelTexturesId === undefined) modelTexturesId = 0;
localStorage.setItem("modelTexturesId", modelTexturesId);
2018-12-10 17:19:13 +08:00
loadlive2d("live2d", `${apiURL}/get/?id=${modelId}-${modelTexturesId}`, console.log("live2d", `模型 ${modelId}-${modelTexturesId} 加载完成`));
2018-07-11 13:25:04 +08:00
}
function loadRandModel() {
var modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId");
2018-12-10 17:19:13 +08:00
//可选 "rand"(随机), "switch"(顺序)
2018-07-11 13:25:04 +08:00
$.ajax({
cache: false,
2018-12-10 17:19:13 +08:00
url: `${apiURL}/rand_textures/?id=${modelId}-${modelTexturesId}`,
2018-07-11 13:25:04 +08:00
dataType: "json",
success: function(result) {
if (result.textures["id"] == 1 && (modelTexturesId == 1 || modelTexturesId == 0)) {
2018-12-18 00:01:42 +08:00
showMessage("我还没有其他衣服呢!", 4000, 10);
2018-07-11 13:25:04 +08:00
} else {
2018-12-18 00:01:42 +08:00
showMessage("我的新衣服好看嘛?", 4000, 10);
2018-07-11 13:25:04 +08:00
}
loadModel(modelId, result.textures["id"]);
}
});
}
function loadOtherModel() {
var modelId = localStorage.getItem("modelId");
$.ajax({
cache: false,
url: apiURL + "/switch/?id=" + modelId,
dataType: "json",
success: function(result) {
loadModel(result.model["id"]);
showMessage(result.model["message"], 4000, 10);
}
});
}
2018-07-11 15:38:35 +08:00
initModel();
}