Files
trpic/web/scripts/submit_url/submit.js
2026-04-18 00:26:51 +08:00

49 lines
1.5 KiB
JavaScript

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;
button.addEventListener("click", () => {
const url = input.value;
if (url == "" || fetching == true) return;
fetching = true;
button.style.backgroundColor = "rgb(50, 80, 80)";
fetch(`/api/artworks/link/${url}`)
.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 400:
alert("提交失败:无效的链接");
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}`);
});
})
}
});