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) {