mirror of
https://github.com/stevenjoezhang/live2d-widget.git
synced 2024-11-22 12:39:59 +08:00
remove string.render
This commit is contained in:
parent
d0024c91db
commit
c043b28aef
@ -3,29 +3,6 @@
|
|||||||
* https://www.fghrsh.net/post/123.html
|
* https://www.fghrsh.net/post/123.html
|
||||||
*/
|
*/
|
||||||
|
|
||||||
String.prototype.render = function(context) {
|
|
||||||
var tokenReg = /(\\)?\{([^\{\}\\]+)(\\)?\}/g,
|
|
||||||
strFlag = true,
|
|
||||||
result = this.replace(tokenReg, function(word, slash1, token, slash2) {
|
|
||||||
if (slash1 || slash2) {
|
|
||||||
return word.replace("\\", "");
|
|
||||||
}
|
|
||||||
var variables = token.replace(/\s/g, "").split(".");
|
|
||||||
var currentObject = context;
|
|
||||||
var i, length, variable;
|
|
||||||
for (i = 0, length = variables.length; i < length; ++i) {
|
|
||||||
variable = variables[i];
|
|
||||||
currentObject = currentObject[variable].replace(/\s/g, "").replace(/\s+/g, "");
|
|
||||||
if (!currentObject) {
|
|
||||||
strFlag = false;
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return currentObject;
|
|
||||||
});
|
|
||||||
return strFlag ? result : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function initWidget(waifuPath, apiPath) {
|
function initWidget(waifuPath, apiPath) {
|
||||||
if (screen.width <= 768) return;
|
if (screen.width <= 768) return;
|
||||||
if (localStorage.getItem("waifu-display")) {
|
if (localStorage.getItem("waifu-display")) {
|
||||||
@ -194,11 +171,7 @@ function initWidget(waifuPath, apiPath) {
|
|||||||
if (Math.random() < 0.6 && messageArray.length > 0) {
|
if (Math.random() < 0.6 && messageArray.length > 0) {
|
||||||
showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9);
|
showMessage(messageArray[Math.floor(Math.random() * messageArray.length)], 6000, 9);
|
||||||
} else $.getJSON("https://v1.hitokoto.cn", function(result) {
|
} else $.getJSON("https://v1.hitokoto.cn", function(result) {
|
||||||
var text = '这句一言来自 <span style="color:#0099cc;">『{source}』</span>,是 <span style="color:#0099cc;">{creator}</span> 在 hitokoto.cn 投稿的。';
|
var text = `这句一言来自 <span style="color:#0099cc;">『${result.from}』</span>,是 <span style="color:#0099cc;">${result.creator}</span> 在 hitokoto.cn 投稿的。`;
|
||||||
text = text.render({
|
|
||||||
source: result.from,
|
|
||||||
creator: result.creator
|
|
||||||
});
|
|
||||||
showMessage(result.hitokoto, 6000, 9);
|
showMessage(result.hitokoto, 6000, 9);
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
showMessage(text, 4000, 9);
|
showMessage(text, 4000, 9);
|
||||||
@ -245,9 +218,7 @@ function initWidget(waifuPath, apiPath) {
|
|||||||
$(document).on("mouseover", tips.selector, function() {
|
$(document).on("mouseover", tips.selector, function() {
|
||||||
var text = tips.text;
|
var text = tips.text;
|
||||||
if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
|
if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
|
||||||
text = text.render({
|
text = text.replace("{text}", $(this).text());
|
||||||
text: $(this).text()
|
|
||||||
});
|
|
||||||
showMessage(text, 4000, 8);
|
showMessage(text, 4000, 8);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -255,9 +226,7 @@ function initWidget(waifuPath, apiPath) {
|
|||||||
$(document).on("click", tips.selector, function() {
|
$(document).on("click", tips.selector, function() {
|
||||||
var text = tips.text;
|
var text = tips.text;
|
||||||
if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
|
if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
|
||||||
text = text.render({
|
text = text.replace("{text}", $(this).text());
|
||||||
text: $(this).text()
|
|
||||||
});
|
|
||||||
showMessage(text, 4000, 8);
|
showMessage(text, 4000, 8);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -267,10 +236,8 @@ function initWidget(waifuPath, apiPath) {
|
|||||||
before = tips.date.split("-")[1] || after;
|
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])) {
|
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;
|
var text = tips.text;
|
||||||
//if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
|
if (Array.isArray(tips.text)) text = tips.text[Math.floor(Math.random() * tips.text.length)];
|
||||||
text = text.render({
|
text = text.replace("{year}", now.getFullYear());
|
||||||
year: now.getFullYear()
|
|
||||||
});
|
|
||||||
//showMessage(text, 7000, true);
|
//showMessage(text, 7000, true);
|
||||||
messageArray.push(text);
|
messageArray.push(text);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user