From 46d7d4b26f76c77a18de64f6ae5d46b4f3a850c8 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, 24 Apr 2024 13:14:30 +0800 Subject: [PATCH] Update background message name --- background/messages/bangumi.ts | 22 ++++++++++++++++++++++ background/messages/read.ts | 2 +- background/messages/{bili.ts => toy.ts} | 17 +---------------- components/biz/read/index.tsx | 15 ++++----------- components/biz/toy/index.tsx | 17 ++++++++--------- contents/biliProgress.tsx | 2 +- 6 files changed, 37 insertions(+), 38 deletions(-) create mode 100644 background/messages/bangumi.ts rename background/messages/{bili.ts => toy.ts} (67%) diff --git a/background/messages/bangumi.ts b/background/messages/bangumi.ts new file mode 100644 index 0000000..97de9f5 --- /dev/null +++ b/background/messages/bangumi.ts @@ -0,0 +1,22 @@ +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 === "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; diff --git a/background/messages/read.ts b/background/messages/read.ts index fdc6ad2..b496a6b 100644 --- a/background/messages/read.ts +++ b/background/messages/read.ts @@ -3,7 +3,7 @@ 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") { + if (req.body.action === "addRead") { const { values } = req.body; const formData = new FormData(); diff --git a/background/messages/bili.ts b/background/messages/toy.ts similarity index 67% rename from background/messages/bili.ts rename to background/messages/toy.ts index 5f235d6..dfd24aa 100644 --- a/background/messages/bili.ts +++ b/background/messages/toy.ts @@ -4,7 +4,7 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => { const { token, siteUrl } = await chrome.storage.local.get(["token", "siteUrl"]); // 添加手办 - if (req.body.action === "submitAddForm") { + if (req.body.action === "addToy") { const { values } = req.body; let imageBlob: Blob | undefined; @@ -33,21 +33,6 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => { 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; diff --git a/components/biz/read/index.tsx b/components/biz/read/index.tsx index 70d42d5..ee7319c 100644 --- a/components/biz/read/index.tsx +++ b/components/biz/read/index.tsx @@ -28,20 +28,13 @@ const submitForm = (body: FormValue) => { sendToBackground({ name: "read", body: { - action: "submitAddForm", + action: "addRead", values: body, }, }).then((res) => { - if (res.status === "Success") { - add({ - content: "提交成功", - }); - } - else { - add({ - content: res.msg, - }); - } + add({ + content: res.msg, + }); }).catch((e) => { if (e instanceof Error) { add({ diff --git a/components/biz/toy/index.tsx b/components/biz/toy/index.tsx index 5d83d96..8148aab 100644 --- a/components/biz/toy/index.tsx +++ b/components/biz/toy/index.tsx @@ -42,23 +42,22 @@ function Toy() { const submitForm = (body: FormValue) => { sendToBackground({ - name: "bili", + name: "toy", body: { - action: "submitAddForm", + action: "addToy", values: { ...body, imageUrl: imgs[0], }, }, }).then((res) => { - if (res.status === "Success") { + add({ + content: res.msg, + }); + }).catch((e) => { + if (e instanceof Error) { add({ - content: "提交成功", - }); - } - else { - add({ - content: res.msg, + content: e.message, }); } }); diff --git a/contents/biliProgress.tsx b/contents/biliProgress.tsx index 3eb56f5..7e5a778 100644 --- a/contents/biliProgress.tsx +++ b/contents/biliProgress.tsx @@ -33,7 +33,7 @@ const SyncButton = () => { console.log("Toolbox: 获得追番信息", res.value); sendToBackground({ - name: "bili", + name: "bangumi", body: { action: "updateBangumiProgress", values: res.value,