/** * 观看历史页:清空记录 */ (function () { var clearBtns = document.querySelectorAll("[data-history-clear]"); var confirmMask = document.querySelector("[data-history-confirm]"); var confirmCancelBtns = document.querySelectorAll( "[data-history-confirm-cancel]" ); var confirmOkBtn = document.querySelector("[data-history-confirm-ok]"); var clearing = false; function toast(message, type) { if (window.AuthAPI && typeof window.AuthAPI.toast === "function") { window.AuthAPI.toast(message, type); } } function openClearConfirm() { if (!confirmMask) return; confirmMask.hidden = false; confirmMask.removeAttribute("hidden"); confirmMask.classList.add("is-open"); document.body.style.overflow = "hidden"; } function closeClearConfirm() { if (!confirmMask) return; confirmMask.classList.remove("is-open"); confirmMask.hidden = true; confirmMask.setAttribute("hidden", ""); document.body.style.overflow = ""; } function clearHistory() { if (clearing) return; if (!window.HttpRequest) { toast("清空失败,请稍后重试", "error"); return; } clearing = true; window.HttpRequest.post("/api/playlet/history/clear") .then(function (res) { if (!res || res.code !== 200) { throw new Error((res && (res.tip || res.msg)) || "清空失败"); } window.location.reload(); }) .catch(function (err) { clearing = false; toast((err && err.message) || "清空失败,请稍后重试", "error"); }); } clearBtns.forEach(function (btn) { btn.addEventListener("click", function () { openClearConfirm(); }); }); confirmCancelBtns.forEach(function (btn) { btn.addEventListener("click", function () { closeClearConfirm(); }); }); if (confirmMask) { confirmMask.addEventListener("click", function (e) { if (e.target === confirmMask) closeClearConfirm(); }); } if (confirmOkBtn) { confirmOkBtn.addEventListener("click", function () { closeClearConfirm(); clearHistory(); }); } document.addEventListener("keydown", function (e) { if (e.key !== "Escape") return; if (!confirmMask || !confirmMask.classList.contains("is-open")) return; closeClearConfirm(); }); })();