Add bilibili toy form submit

This commit is contained in:
2024-03-06 11:14:16 +08:00
parent 9277ea51af
commit 4161c5587c
3 changed files with 73 additions and 13 deletions
+37
View File
@@ -0,0 +1,37 @@
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);
}
}
export default handler;