54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
import type { PlasmoMessaging } from "@plasmohq/messaging";
|
|
|
|
const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
|
|
const { token, siteUrl } = await chrome.storage.local.get(["token", "siteUrl"]);
|
|
|
|
// 添加手办
|
|
if (req.body.action === "submitAddForm") {
|
|
const { values } = req.body;
|
|
|
|
let imageBlob: Blob | undefined;
|
|
|
|
if (values.imageUrl) {
|
|
imageBlob = await fetch(values.imageUrl).then((res) => res.blob());
|
|
}
|
|
|
|
const formData = new FormData();
|
|
|
|
Object.keys(values).forEach((item) => {
|
|
formData.append(item, String(values[item]));
|
|
});
|
|
|
|
if (imageBlob) {
|
|
formData.append("image", imageBlob);
|
|
}
|
|
|
|
const addReq = await fetch(`${siteUrl}/api/toy/add`, {
|
|
method: "POST",
|
|
body: formData,
|
|
headers: {
|
|
"paul-token-code": token,
|
|
},
|
|
}).then((res) => res.json());
|
|
|
|
res.send(addReq);
|
|
}
|
|
|
|
// 更新追番进度
|
|
if (req.body.action === "updateBangumiProgress") {
|
|
const { values } = req.body;
|
|
|
|
const updateRequest = await fetch(`${siteUrl}/api/bangumi/update`, {
|
|
method: "POST",
|
|
body: JSON.stringify(values),
|
|
headers: {
|
|
"paul-token-code": token,
|
|
},
|
|
}).then((res) => res.json());
|
|
|
|
res.send(updateRequest);
|
|
}
|
|
}
|
|
|
|
export default handler;
|