/*! admin-leads-online.js — animate 在线 column on /leads without touching SPA bundle */
(function () {
"use strict";
if (window.__adminLeadsOnlineBooted) return;
window.__adminLeadsOnlineBooted = true;
if (!/admin\.hackrobot\.cn$/i.test(location.hostname)) return;
var TTL_MS = 120000;
var STYLE_ID = "admin-leads-online-style";
function ensureStyle() {
if (document.getElementById(STYLE_ID)) return;
var s = document.createElement("style");
s.id = STYLE_ID;
s.textContent =
"@keyframes comOnlinePulse{0%{box-shadow:0 0 0 0 rgba(52,211,153,.55)}70%{box-shadow:0 0 0 8px rgba(52,211,153,0)}100%{box-shadow:0 0 0 0 rgba(52,211,153,0)}}" +
"@keyframes comOnlineGlow{0%,100%{filter:none}50%{filter:drop-shadow(0 0 6px rgba(52,211,153,.4))}}" +
".alo-cell{display:inline-flex;flex-direction:column;align-items:flex-start;gap:4px;min-width:64px}" +
".alo-badge{display:inline-flex;align-items:center;gap:6px;padding:3px 8px;border-radius:999px;font-size:10px;font-weight:800;letter-spacing:.1em}" +
".alo-badge.on{color:#6ee7b7;border:1px solid rgba(52,211,153,.4);background:rgba(52,211,153,.08);animation:comOnlineGlow 2s ease-in-out infinite}" +
".alo-badge.on i{width:7px;height:7px;border-radius:50%;background:#34d399;animation:comOnlinePulse 1.6s ease-out infinite}" +
".alo-badge.off{color:#a8a29e;border:1px solid rgba(168,162,158,.25);background:rgba(255,255,255,.03)}" +
".alo-time{font-size:12px;color:#a8a29e}" +
".alo-badge.on + .alo-time,.alo-cell.is-live .alo-time{color:#6ee7b7;font-weight:600}" +
"th.alo-th-live{color:#6ee7b7}" +
"tr.alo-row-live td:first-child{animation:comOnlineGlow 2.4s ease-in-out infinite}";
document.head.appendChild(s);
}
function parseTs(text, title) {
// title often full locale string; text is relative like 刚刚 / 3分钟前
if (title && title !== "—") {
var d = Date.parse(title.replace(/年|月/g, "/").replace(/日/g, " "));
if (!Number.isNaN(d)) return d;
d = Date.parse(title);
if (!Number.isNaN(d)) return d;
}
var t = (text || "").trim();
if (!t || t === "—") return 0;
if (t === "刚刚") return Date.now();
var m = t.match(/^(\d+)\s*秒/);
if (m) return Date.now() - Number(m[1]) * 1000;
m = t.match(/^(\d+)\s*分钟/);
if (m) return Date.now() - Number(m[1]) * 60000;
m = t.match(/^(\d+)\s*小时/);
if (m) return Date.now() - Number(m[1]) * 3600000;
return 0;
}
function paintCell(td, live) {
if (!td) return;
if (td.getAttribute("data-alo") === "1") {
var badge = td.querySelector(".alo-badge");
var time = td.querySelector(".alo-time");
if (badge) {
badge.className = "alo-badge " + (live ? "on" : "off");
badge.innerHTML = live ? "在线" : "离线";
}
td.classList.toggle("is-live", !!live);
var tr = td.closest("tr");
if (tr) tr.classList.toggle("alo-row-live", !!live);
return;
}
var rawText = (td.textContent || "").trim();
var title = td.getAttribute("title") || "";
td.setAttribute("data-alo", "1");
td.innerHTML = "";
var wrap = document.createElement("div");
wrap.className = "alo-cell" + (live ? " is-live" : "");
var badge = document.createElement("span");
badge.className = "alo-badge " + (live ? "on" : "off");
badge.innerHTML = live ? "在线" : "离线";
var time = document.createElement("span");
time.className = "alo-time";
time.textContent = rawText || "—";
if (title) time.title = title;
wrap.appendChild(badge);
wrap.appendChild(time);
td.appendChild(wrap);
var tr = td.closest("tr");
if (tr) tr.classList.toggle("alo-row-live", !!live);
}
function findOnlineColIndex(table) {
var ths = table.querySelectorAll("thead th");
for (var i = 0; i < ths.length; i++) {
var label = (ths[i].textContent || "").trim();
if (label === "最近活跃" || label === "在线" || label.indexOf("活跃") >= 0) {
ths[i].classList.add("alo-th-live");
if (label === "最近活跃") ths[i].textContent = "在线";
return i;
}
}
return 0; // leads list first col
}
function enhance() {
if (!location.pathname.replace(/\/$/, "").endsWith("/leads") && location.pathname !== "/leads") {
// also allow /leads with query
if (location.pathname.replace(/\/+$/, "") !== "/leads") return;
}
ensureStyle();
var tables = document.querySelectorAll("table.com-table");
tables.forEach(function (table) {
var idx = findOnlineColIndex(table);
var rows = table.querySelectorAll("tbody tr");
rows.forEach(function (tr) {
var tds = tr.querySelectorAll("td");
if (!tds.length || idx >= tds.length) return;
var td = tds[idx];
var title = td.getAttribute("title") || "";
var text = td.getAttribute("data-alo") === "1"
? (td.querySelector(".alo-time") || {}).textContent || ""
: (td.textContent || "").trim();
// Prefer title absolute time; if missing, relative text
var ts = parseTs(text, title);
var live = ts > 0 && Date.now() - ts < TTL_MS;
// relative "刚刚" / N分钟
if (!ts) {
var rel = text;
if (td.getAttribute("data-alo") === "1") {
rel = (td.querySelector(".alo-time") || {}).textContent || "";
}
if (rel === "刚刚" || /^\d+\s*秒/.test(rel) || /^[1-2]\s*分钟/.test(rel)) live = true;
}
paintCell(td, live);
});
});
}
function boot() {
ensureStyle();
enhance();
setInterval(enhance, 5000);
var obs = new MutationObserver(function () {
enhance();
});
obs.observe(document.documentElement, { childList: true, subtree: true });
window.addEventListener("popstate", function () {
setTimeout(enhance, 50);
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", boot);
} else {
boot();
}
})();