Files
crypto_monitor/lib/common/static/options_expiry_countdown.js
dekun b733e551a0 Normalize fullwidth punctuation to ASCII across codebase.
Add scripts/normalize_ambiguous_unicode.py; fix corrupted patch_instance_theme_templates.py. Preserves curly quotes in string literals; removes Git homoglyph warnings on .env.example.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-08 23:42:26 +08:00

59 lines
1.8 KiB
JavaScript

/**
* 期权到期倒计时(实例期权页 + 中控监控/看板共用)
*/
(function (global) {
function normalizeExpMs(v) {
if (v == null || v === "") return null;
var n = Number(v);
if (!Number.isFinite(n) || n <= 0) return null;
if (n < 1e12) n *= 1000;
return n;
}
function formatCountdown(expMs, nowMs) {
var ms = normalizeExpMs(expMs);
if (ms == null) return "—";
var now = nowMs != null ? nowMs : Date.now();
var rem = Math.max(0, Math.floor((ms - now) / 1000));
if (rem <= 0) return "已到期";
var d = Math.floor(rem / 86400);
var h = Math.floor((rem % 86400) / 3600);
var m = Math.floor((rem % 3600) / 60);
var s = rem % 60;
var pad = function (x) {
return String(x).padStart(2, "0");
};
if (d > 0) return d + "天 " + pad(h) + ":" + pad(m) + ":" + pad(s);
return pad(h) + ":" + pad(m) + ":" + pad(s);
}
function tick(root) {
var scope = root && root.querySelectorAll ? root : document;
var now = Date.now();
scope.querySelectorAll("[data-opt-exp-ms]").forEach(function (el) {
var exp = el.getAttribute("data-opt-exp-ms");
var text = formatCountdown(exp, now);
el.textContent = text;
var expMs = normalizeExpMs(exp);
el.classList.toggle("opt-expiry-cd--urgent", expMs != null && expMs - now > 0 && expMs - now < 3600000);
el.classList.toggle("opt-expiry-cd--expired", text === "已到期");
});
}
var timer = null;
function ensureTimer() {
tick();
if (timer) return;
timer = setInterval(function () {
tick();
}, 1000);
}
global.OptionsExpiryCountdown = {
normalizeExpMs: normalizeExpMs,
format: formatCountdown,
tick: tick,
ensureTimer: ensureTimer,
};
})(typeof window !== "undefined" ? window : globalThis);