From 12f2aa8bfd5278cbeaa497227e22564566842e3e Mon Sep 17 00:00:00 2001 From: fzk <458813868@qq.com> Date: Tue, 24 Sep 2024 16:58:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BE=A7=E8=BE=B9=E6=A0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- background/index.ts | 28 +++++++++++ background/messages/sidepanel.ts | 9 ++++ component/options/content.tsx | 13 +++-- component/options/custom.tsx | 14 ++++-- contents/custom.tsx | 12 +++-- locales/en/messages.json | 3 ++ locales/zh_CN/messages.json | 3 ++ package.json | 2 +- popup/index.tsx | 16 ++++++ sidepanel/index.module.scss | 38 +++++++++++++++ sidepanel/index.tsx | 83 ++++++++++++++++++++++++++++++++ 11 files changed, 208 insertions(+), 13 deletions(-) create mode 100644 background/messages/sidepanel.ts create mode 100644 sidepanel/index.module.scss create mode 100644 sidepanel/index.tsx diff --git a/background/index.ts b/background/index.ts index 51e1c52..8383206 100644 --- a/background/index.ts +++ b/background/index.ts @@ -24,3 +24,31 @@ chrome.runtime.requestUpdateCheck(function (status, details) { console.log("更新检查被限流。") } }) + +chrome.tabs.onUpdated.addListener((tabId, _, tab) => { + // console.log(tabId, _, tab) + // if (!("sidePanel" in chrome)) { + // return + // } + // + // void (async () => { + // if (!tab.url) { + // return + // } + // // const url = new URL(tab.url) + // // if ( + // // url.origin === "" + // // ) { + // // await chrome.sidePanel.setOptions({ + // // tabId, + // // // path: "pages/options.html", + // // enabled: true + // // }) + // // } else { + // // await chrome.sidePanel.setOptions({ + // // tabId, + // // enabled: false + // // }) + // // } + // })() +}) diff --git a/background/messages/sidepanel.ts b/background/messages/sidepanel.ts new file mode 100644 index 0000000..25f648b --- /dev/null +++ b/background/messages/sidepanel.ts @@ -0,0 +1,9 @@ +import type { PlasmoMessaging } from "@plasmohq/messaging" + +const handler: PlasmoMessaging.MessageHandler = (req, res) => { + chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => { + chrome.sidePanel.open({ tabId: tab.id }) + }) +} + +export default handler diff --git a/component/options/content.tsx b/component/options/content.tsx index edf0057..fb7c35b 100644 --- a/component/options/content.tsx +++ b/component/options/content.tsx @@ -1,6 +1,7 @@ import React, { useEffect, useState } from "react" import { sendToContentScript } from "@plasmohq/messaging" +import { useStorage } from "@plasmohq/storage/dist/hook" import Cto51 from "~component/options/51cto" import Baidu from "~component/options/baidu" @@ -18,6 +19,7 @@ import Weixin from "~component/options/weixin" import Zhihu from "~component/options/zhihu" export default function Content() { + const [closeLog] = useStorage("config-closeLog", true) const [csdnIsShow, setCsdnIsShow] = useState(false) const [zhihuIsShow, setZhihuIsShow] = useState(false) const [baiduIsShow, setBaiduIsShow] = useState(false) @@ -64,9 +66,14 @@ export default function Content() { custom: setCustomIsShow } - async function getIsShow(type) { - const res = await sendToContentScript({ name: `${type}-isShow` }) - res?.isShow && setIsShowMap[type] && setIsShowMap[type](res.isShow) + function getIsShow(type) { + sendToContentScript({ name: `${type}-isShow` }) + .then((res) => { + res?.isShow && setIsShowMap[type] && setIsShowMap[type](res.isShow) + }) + .catch((err) => { + closeLog || console.log("getIsShow", err) + }) } return ( diff --git a/component/options/custom.tsx b/component/options/custom.tsx index a13e422..c538a39 100644 --- a/component/options/custom.tsx +++ b/component/options/custom.tsx @@ -11,18 +11,22 @@ export default function Custom() { v === undefined ? false : v ) const [cssCode, setCssCode] = useStorage("custom-cssCode") + const [closeLog] = useStorage("config-closeLog", true) const [codes, setCodes] = useState([]) useEffect(() => { - if (window.location.protocol != "chrome-extension:") { - getCodes() - } + getCodes() }, []) async function getCodes() { - const res = await sendToContentScript({ name: `custom-getCodes` }) - res?.codes && setCodes(res?.codes) + sendToContentScript({ name: `custom-getCodes` }) + .then((res) => { + res?.codes && setCodes(res?.codes) + }) + .catch((err) => { + closeLog || console.log("getCodes", err) + }) } function downloadCode(index) { diff --git a/contents/custom.tsx b/contents/custom.tsx index ec8ed03..e27679f 100644 --- a/contents/custom.tsx +++ b/contents/custom.tsx @@ -173,9 +173,11 @@ export default function CustomOverlay() { target.classList.add("codebox-current") event.stopPropagation() event.preventDefault() - if (confirm("是否下载?")) { - handleDownload() - } + setTimeout(() => { + if (confirm("是否下载?")) { + handleDownload() + } + }, 1000) } }) } @@ -209,7 +211,6 @@ export default function CustomOverlay() { function handleDownload() { const currentDom = document.querySelector(".codebox-current") removeCurrentDom() - setIsCurrentDom(false) if (isDownloadType == "html") { downloadHtml(currentDom) } else if (isDownloadType == "markdown") { @@ -219,6 +220,9 @@ export default function CustomOverlay() { } else if (isDownloadType == "img") { downloadImg(currentDom) } + isReady = false + isSelect = false + setIsCurrentDom(false) } function handleConfirm() { diff --git a/locales/en/messages.json b/locales/en/messages.json index d81e359..4e40e02 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -11,6 +11,9 @@ "history": { "message": "Historical records" }, + "sidePanel": { + "message": "Side Panel" + }, "more": { "message": "More settings" }, diff --git a/locales/zh_CN/messages.json b/locales/zh_CN/messages.json index ec92f99..f6f67db 100644 --- a/locales/zh_CN/messages.json +++ b/locales/zh_CN/messages.json @@ -11,6 +11,9 @@ "history": { "message": "历史记录" }, + "sidePanel": { + "message": "侧边栏" + }, "more": { "message": "更多设置" }, diff --git a/package.json b/package.json index 719147b..87dbe6f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "code-box", "displayName": "__MSG_extensionName__", - "version": "0.7.1", + "version": "0.7.2", "description": "__MSG_extensionDescription__", "author": "027xiguapi. <458813868@qq.com>", "scripts": { diff --git a/popup/index.tsx b/popup/index.tsx index 3f73586..c69fda2 100644 --- a/popup/index.tsx +++ b/popup/index.tsx @@ -4,7 +4,18 @@ import { i18n } from "~tools" import "~index.css" +import { sendToBackground } from "@plasmohq/messaging" + export default function IndexPopup() { + function hanleOpenSidePanel() { + sendToBackground({ + name: "sidepanel", + body: { + active: true + } + }) + } + return (
@@ -16,6 +27,11 @@ export default function IndexPopup() {
+
{ + getCodes() + }, []) + + async function getCodes() { + sendToContentScript({ name: `custom-getCodes` }) + .then((res) => { + res?.codes && setCodes(res?.codes) + }) + .catch((err) => { + closeLog || console.log("getCodes", err) + }) + } + + function pinCode(index) { + sendToContentScript({ name: `custom-scrollIntoViewCode`, body: { index } }) + } + + function downloadCode(index) { + sendToContentScript({ name: `custom-downloadCode`, body: { index } }) + } + + function handleSubmit() {} + + return ( + +
+
+

微信公众号

+

关注公众号,了解更多功能

+ 微信公众号 + + } /> + + +
+ {codes.map((code, index) => ( +
pinCode(index)} key={index}> + + {index + 1}-{JSON.stringify(code)} + + pinCode(index)} + /> + downloadCode(index)} + /> +
+ ))} +
+
+ ) +} + +export default IndexSidePanel