document.addEventListener("DOMContentLoaded", () => { if (document.cookie.includes("_trpic_user")) { document.getElementById("submit-box").style.display = "flex"; const input = document.getElementById("submit-input"); const button = document.getElementById("submit-button"); let fetching = false; input.addEventListener('keypress', (e) => { if (e.key === 'Enter') submit() }); button.addEventListener("click", submit); function submit() { const url = input.value; if (url == "" || fetching == true) return; fetching = true; button.style.backgroundColor = "rgb(50, 80, 80)"; const formData = new FormData(); formData.append("post_link", url); fetch("/api/artworks/", { method: "POST", body: formData }) .then(response => { response.json().then((resjson) => { switch (response.status) { case 200: if (confirm("作品已存在,前往查看?")) { window.location.href = `/artworks/${resjson.id}`; } break; case 201: resetGallery(); break; case 401: alert("提交失败:需要验证"); break; default: alert(`提交失败:${resjson.msg}: ${resjson.error}`); } }); }) .finally(() => { button.style.backgroundColor = "rgb(50, 50, 50)"; input.value = ""; fetching = false; }) .catch(error => { alert(`请求失败: ${error}`); }); } } });