Use fetch api

This commit is contained in:
Mimi 2019-12-04 18:30:44 +08:00
parent 619b7e4ace
commit cb77fe55e8

View File

@ -119,13 +119,15 @@ function loadWidget(waifuPath, apiPath) {
function showHitokoto() { function showHitokoto() {
//增加 hitokoto.cn 的 API //增加 hitokoto.cn 的 API
if (Math.random() < 0.6 && messageArray.length > 0) showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9); 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) { else fetch("https://v1.hitokoto.cn")
.then(response => response.json())
.then(result => {
var text = `这句一言来自 <span style="color:#0099cc;">『${result.from}』</span>,是 <span style="color:#0099cc;">${result.creator}</span> 在 hitokoto.cn 投稿的。`; var text = `这句一言来自 <span style="color:#0099cc;">『${result.from}』</span>,是 <span style="color:#0099cc;">${result.creator}</span> 在 hitokoto.cn 投稿的。`;
showMessage(result.hitokoto, 6000, 9); showMessage(result.hitokoto, 6000, 9);
setTimeout(() => { setTimeout(() => {
showMessage(text, 4000, 9); showMessage(text, 4000, 9);
}, 6000); }, 6000);
}); });
} }
function showMessage(text, timeout, priority) { function showMessage(text, timeout, priority) {
@ -154,33 +156,35 @@ function loadWidget(waifuPath, apiPath) {
modelTexturesId = 53; //材质 ID modelTexturesId = 53; //材质 ID
} }
loadModel(modelId, modelTexturesId); loadModel(modelId, modelTexturesId);
$.getJSON(waifuPath, function(result) { fetch(waifuPath)
result.mouseover.forEach(tips => { .then(response => response.json())
$(document).on("mouseover", tips.selector, function() { .then(result => {
var text = Array.isArray(tips.text) ? tips.text[Math.floor(Math.random() * tips.text.length)] : tips.text; result.mouseover.forEach(tips => {
text = text.replace("{text}", $(this).text()); $(document).on("mouseover", tips.selector, function() {
showMessage(text, 4000, 8); 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);
});
});
result.click.forEach(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);
});
});
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);
}
}); });
}); });
result.click.forEach(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);
});
});
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);
}
});
});
} }
initModel(); initModel();
@ -194,30 +198,24 @@ function loadWidget(waifuPath, apiPath) {
function loadRandModel() { function loadRandModel() {
var modelId = localStorage.getItem("modelId"), var modelId = localStorage.getItem("modelId"),
modelTexturesId = localStorage.getItem("modelTexturesId"); modelTexturesId = localStorage.getItem("modelTexturesId");
//可选 "rand"(随机), "switch"(顺序) //可选 "rand"(随机), "switch"(顺序)
$.ajax({ fetch(`${apiPath}/rand_textures/?id=${modelId}-${modelTexturesId}`)
cache: false, .then(response => response.json())
url: `${apiPath}/rand_textures/?id=${modelId}-${modelTexturesId}`, .then(result => {
dataType: "json",
success: function(result) {
if (result.textures["id"] == 1 && (modelTexturesId == 1 || modelTexturesId == 0)) showMessage("我还没有其他衣服呢!", 4000, 10); if (result.textures["id"] == 1 && (modelTexturesId == 1 || modelTexturesId == 0)) showMessage("我还没有其他衣服呢!", 4000, 10);
else showMessage("我的新衣服好看嘛?", 4000, 10); else showMessage("我的新衣服好看嘛?", 4000, 10);
loadModel(modelId, result.textures["id"]); loadModel(modelId, result.textures["id"]);
} });
});
} }
function loadOtherModel() { function loadOtherModel() {
var modelId = localStorage.getItem("modelId"); var modelId = localStorage.getItem("modelId");
$.ajax({ fetch(`${apiPath}/switch/?id=${modelId}`)
cache: false, .then(response => response.json())
url: `${apiPath}/switch/?id=${modelId}`, .then(result => {
dataType: "json",
success: function(result) {
loadModel(result.model["id"]); loadModel(result.model["id"]);
showMessage(result.model["message"], 4000, 10); showMessage(result.model["message"], 4000, 10);
} });
});
} }
} }