From 4161c5587cea21fc78565742a43ea4e981efc8ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=87=E8=B6=A3=E4=BF=9D=E7=BD=97?= Date: Wed, 6 Mar 2024 11:14:16 +0800 Subject: [PATCH] Add bilibili toy form submit --- background/messages/bili.ts | 37 +++++++++++++++++++++++++++++ popup/bili.tsx | 47 +++++++++++++++++++++++++++---------- popup/read.tsx | 2 +- 3 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 background/messages/bili.ts diff --git a/background/messages/bili.ts b/background/messages/bili.ts new file mode 100644 index 0000000..4e4034d --- /dev/null +++ b/background/messages/bili.ts @@ -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; diff --git a/popup/bili.tsx b/popup/bili.tsx index 6afb018..4299bba 100644 --- a/popup/bili.tsx +++ b/popup/bili.tsx @@ -1,10 +1,12 @@ -import { useEffect, useState, type FormEvent } from "react"; +import { useEffect, useState } from "react"; +import { sendToBackground } from "@plasmohq/messaging"; import Placeholder from "~components/ui/placeholder"; import Form from "~components/ui/form"; import { IconBack } from "~assets/icons"; import styles from "./popup.module.less"; import stylesB from "./bili.module.less"; import useForm from "~hooks/useForm"; +import { add } from "~components/ui/message/utils"; interface ReadProps { onBack: () => void; @@ -39,15 +41,36 @@ function Toy({ onBack }: ReadProps) { initialValues: { type: 3, }, - onSubmit: (values) => { - console.log(values); - }, }); const [imgs, setImgs] = useState([]); const [currentImg, setCurrentImg] = useState(0); const [failed, setFailed] = useState(false); + const submitForm = (body: FormValue) => { + sendToBackground({ + name: "bili", + body: { + action: "submitAddForm", + values: { + ...body, + imageUrl: imgs[0], + }, + }, + }).then((res) => { + if (res.status === "Success") { + add({ + content: "提交成功", + }); + } + else { + add({ + content: res.msg, + }); + } + }); + } + useEffect(() => { getInfo().then((res) => { if (!res) { @@ -77,34 +100,34 @@ function Toy({ onBack }: ReadProps) {
{imgs.map((item, index) => ( - setCurrentImg(index)} /> + setCurrentImg(index)} /> ))}
-
console.log(data))}> +
- +
- +
- +
- +
- @@ -112,7 +135,7 @@ function Toy({ onBack }: ReadProps) {
- +
diff --git a/popup/read.tsx b/popup/read.tsx index fe22229..fb36501 100644 --- a/popup/read.tsx +++ b/popup/read.tsx @@ -1,5 +1,5 @@ -import { sendToBackground } from "@plasmohq/messaging"; import { useEffect, useState } from "react"; +import { sendToBackground } from "@plasmohq/messaging"; import Form from "~components/ui/form"; import { IconBack } from "~assets/icons"; import styles from "./popup.module.less";