3,383
个编辑
MCBBS Wiki欢迎您共同参与编辑!在参与编辑之前请先阅读Wiki方针。
如果在编辑的过程中遇到了什么问题,可以去讨论板提问。
为了您能够无阻碍地参与编辑 未验证/绑定过邮箱的用户,请尽快绑定/验证。
MCBBS Wiki GitHub群组已上线!
您可以在回声洞中发表吐槽!
服务器状态监控。点击进入
本站由MCBBS用户自行搭建,与MCBBS及东银河系漫游指南(北京)科技有限公司没有从属关系。点此了解 MCBBS Wiki 不是什么>>
Salt lovely(留言 | 贡献) (模板搬运) |
Salt lovely(留言 | 贡献) (重构 salt-time-diff) |
||
<includeonly><script>
"use strict";
(() => {
// src/utils/utils.ts
function docReady(fn) {
if (document.readyState === "loading") {
window.addEventListener("DOMContentLoaded", fn);
} else {
fn();
}
}
// widget/SaltTimeDiff/widget.ts
//! <span class="salt-time-diff
//! 实时更新则额外加上 real-time ——结束时间会强制改为当前时间
//! 更复杂的显示请额外加上 complex ——可以用CSS修改渲染样式
//! ">{起始时间}SPLIT{结束时间}SPLIT{指令(d-天,h-小时,m-分钟,s-秒,M-毫秒)}</span>
var cmdAttr = "data-salt-time-diff-command";
var startAttr = "data-salt-time-diff-start";
var endAttr = "data-salt-time-diff-end";
function getDate(time) {
if (!time)
return new
if (/^\d+$/.test(time.trim())) {
const d2 = new Date(+time);
if (!isNaN(d2.valueOf()))
return d2;
return new
const d = new Date(time);
if (!isNaN(d.valueOf()))
return d;
return new Date();
}
function timeDiff(_ms = 0, cmd = "d", cpx = false) {
let ms = Math.abs(_ms);
const isBefore = _ms > 0;
let res = cpx ? isBefore ? '<span class="salt-time-diff-txt salt-time-diff-before">还有</span>' : '<span class="salt-time-diff-txt salt-time-diff-after">已过去</span>' : "";
if (cmd.indexOf("d") != -1) {
let days = Math.floor(ms / (24 * 3600 * 1e3));
ms = ms % (24 * 3600 * 1e3);
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-day">${days}</span>天` : `${days}天`;
}
if (cmd.indexOf("h") != -1) {
let hours = Math.floor(ms / (3600 * 1e3));
ms = ms % (3600 * 1e3);
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-hour">${hours}</span>小时` : `${hours}小时`;
}
if (cmd.indexOf("m") != -1) {
let minutes = Math.floor(ms / (60 * 1e3));
ms = ms % (60 * 1e3);
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-minute">${minutes}</span>分钟` : `${minutes}分钟`;
}
if (cmd.indexOf("s") != -1) {
let seconds = Math.floor(ms / 1e3);
ms = ms % 1e3;
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-second">${seconds}</span>秒` : `${seconds}秒`;
}
if (cmd.indexOf("M") != -1 || res.length < 1) {
res += cpx ? `<span class="salt-time-diff-res salt-time-diff-res-ms">${ms}</span>毫秒` : `${ms}毫秒`;
}
return res;
}
function timeDiffHandler() {
let elems = document.querySelectorAll(".salt-time-diff");
for (let i = 0; i < elems.length; i++) {
let el
let txt = (el.textContent || "").replace(/[年月日]/g, "/").replace(/[;:]/g, ":").split("SPLIT");
let time1
let cmd = "d";
if (txt.length > 2) {
cmd = txt[2].replace(/[天日]/, "d").replace(/小?时/, "h").replace(/分钟?/, "m").replace("毫秒", "M").replace("秒", "s");
}
const cps = el.classList.contains("complex");
const t = timeDiff(time1.getTime() - time2.getTime(), cmd, cps);
if (!cps)
else
el.classList.remove("salt-time-diff");
el.classList.add("salt-time-diff-done");
el.setAttribute(cmdAttr, cmd);
el.setAttribute(startAttr, time1.toString());
el.setAttribute(endAttr, time2.toString());
}
}
docReady(() => {
if (window.saltTimeDiffNoConflictSign)
return;
else
window.saltTimeDiffNoConflictSign = true;
timeDiffHandler();
let elems = document.querySelectorAll(".salt-time-diff-done.real-time");
const update = () => {
for (let i = 0; i < elems.length; i++) {
setTimeout(() => {
const el = elems[i];
const time1 = getDate(el.getAttribute(startAttr));
const time2 = getDate();
const cmd = el.getAttribute(cmdAttr) || "d";
const cps = el.classList.contains("complex");
const t = timeDiff(time1.getTime() - time2.getTime(), cmd, cps);
if (!cps) {
if (el.textContent != t)
el.textContent = t;
} else {
if (el.innerHTML != t)
el.innerHTML = t;
}
}, 0);
}
};
setInterval(() => {
window.requestAnimationFrame ? window.requestAnimationFrame(update) : update();
}, 50);
setInterval(() => {
if (document.querySelectorAll(".salt-time-diff").length) {
timeDiffHandler();
}
elems = document.querySelectorAll(".salt-time-diff-done.real-time");
}, 5e3);
});
})();
</script></includeonly><noinclude>请使用模板{{tl|时间差}}。
原微件搬运自[https://wiki.biligame.com/mcplayer/Widget:TimeDiff 我的世界玩家社区Wiki],原作者:Salt_lovely(原作者即搬运者)。</noinclude>
|