fix: unblock add-node API and improve online status detection

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-16 11:15:51 +08:00
parent db251c39bf
commit 33533d7ebc
3 changed files with 51 additions and 15 deletions
+29 -10
View File
@@ -90,23 +90,38 @@ if (cancelBtn) {
cancelBtn.addEventListener("click", () => modal.classList.add("hidden"));
}
function setButtonBusy(btn, busy, busyText) {
if (!btn) return;
if (busy) {
if (!btn.dataset.label) btn.dataset.label = btn.textContent;
btn.textContent = busyText;
btn.disabled = true;
} else {
btn.textContent = btn.dataset.label || btn.textContent;
btn.disabled = false;
}
}
if (confirmAddBtn) {
confirmAddBtn.addEventListener("click", async () => {
const name = nodeName.value.trim() || "新节点";
confirmAddBtn.disabled = true;
setButtonBusy(confirmAddBtn, true, "创建中…");
if (cancelBtn) cancelBtn.disabled = true;
try {
const res = await fetch(apiUrl("/api/nodes"), {
method: "POST",
credentials: "same-origin",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || "创建失败");
location.reload();
toast("节点已创建,配置生效中…");
setTimeout(() => location.reload(), 600);
} catch (err) {
toast(err.message);
} finally {
confirmAddBtn.disabled = false;
toast(err.message || "创建失败");
setButtonBusy(confirmAddBtn, false);
if (cancelBtn) cancelBtn.disabled = false;
}
});
}
@@ -115,15 +130,19 @@ document.querySelectorAll(".delete-btn").forEach((btn) => {
btn.addEventListener("click", async () => {
const id = btn.dataset.id;
if (!confirm("确定删除该节点?删除后对应链接将失效。")) return;
btn.disabled = true;
setButtonBusy(btn, true, "删除中…");
try {
const res = await fetch(apiUrl(`/api/nodes/${id}`), { method: "DELETE" });
const res = await fetch(apiUrl(`/api/nodes/${id}`), {
method: "DELETE",
credentials: "same-origin",
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || "删除失败");
location.reload();
toast("已删除,配置生效中…");
setTimeout(() => location.reload(), 600);
} catch (err) {
toast(err.message);
btn.disabled = false;
toast(err.message || "删除失败");
setButtonBusy(btn, false);
}
});
});