From 1bb492bd997cffb7a2cba91e175c78499b7ad3d7 Mon Sep 17 00:00:00 2001 From: fzk <458813868@qq.com> Date: Thu, 10 Oct 2024 11:40:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91markdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/options/custom.tsx | 15 ++++++++++++++- component/options/juejin.tsx | 15 ++++++++++++++- contents/custom.tsx | 30 ++++++++++++++++++++++++++---- contents/juejin.tsx | 21 ++++++++++++++++++++- contents/md.tsx | 29 +++++++++++++++++++++++++++++ locales/en/messages.json | 3 +++ locales/zh_CN/messages.json | 3 +++ package.json | 2 +- 8 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 contents/md.tsx diff --git a/component/options/custom.tsx b/component/options/custom.tsx index 055738d..fdc7abf 100644 --- a/component/options/custom.tsx +++ b/component/options/custom.tsx @@ -1,4 +1,4 @@ -import { DownloadOutlined, StarTwoTone } from "@ant-design/icons" +import { DownloadOutlined, EditOutlined, StarTwoTone } from "@ant-design/icons" import { Input } from "antd" import { useEffect, useState } from "react" @@ -25,6 +25,12 @@ export default function Custom() { sendToContentScript({ name: `custom-scrollIntoViewCode`, body: { index } }) } + function editMarkdown() { + sendToContentScript({ + name: "custom-editMarkdown" + }) + } + function downloadMarkdown() { sendToContentScript({ name: "custom-downloadMarkdown" @@ -86,6 +92,13 @@ export default function Custom() { onChange={(e) => setRenderValue(e.target.value)} onBlur={(e) => setStoreValue()}> +
+ + + {i18n("editMarkdown")} + + +
diff --git a/component/options/juejin.tsx b/component/options/juejin.tsx index 35a4399..831d611 100644 --- a/component/options/juejin.tsx +++ b/component/options/juejin.tsx @@ -1,10 +1,16 @@ -import { DownloadOutlined, StarTwoTone } from "@ant-design/icons" +import { DownloadOutlined, EditOutlined, StarTwoTone } from "@ant-design/icons" import { sendToContentScript } from "@plasmohq/messaging" import { i18n } from "~tools" export default function Juejin() { + function editMarkdown() { + sendToContentScript({ + name: "juejin-editMarkdown" + }) + } + function downloadMarkdown() { sendToContentScript({ name: "juejin-downloadMarkdown" @@ -20,6 +26,13 @@ export default function Juejin() { return (
{i18n("juejinConfig")} +
+ + + {i18n("editMarkdown")} + + +
diff --git a/contents/custom.tsx b/contents/custom.tsx index 416b3f4..55bc2c0 100644 --- a/contents/custom.tsx +++ b/contents/custom.tsx @@ -12,6 +12,7 @@ import { useEffect, useRef, useState } from "react" import { sendToBackground } from "@plasmohq/messaging" import { useMessage } from "@plasmohq/messaging/hook" +import { Storage } from "@plasmohq/storage" import { useStorage } from "@plasmohq/storage/hook" import { ThemeProvider } from "~theme" @@ -45,6 +46,12 @@ export default function CustomOverlay() { const [closeLog] = useStorage("config-closeLog", true) const [codesDom, setCodesDom] = useState([]) const [codes, setCodes] = useStorage("app-codes", []) + const [content, setContent] = useStorage({ + key: "md-content", + instance: new Storage({ + area: "local" + }) + }) const [isCurrentDom, setIsCurrentDom] = useState(false) const [rect, setRect] = useState(() => { return { top: 0, right: 0 } @@ -79,7 +86,7 @@ export default function CustomOverlay() { if (req.name == "custom-downloadMarkdown") { isReady = true isSelect = false - isDownloadType = "markdown" + isDownloadType = "downloadMarkdown" messageApi.info("请在页面选择要下载区域!") } if (req.name == "custom-downloadPdf") { @@ -94,6 +101,12 @@ export default function CustomOverlay() { isDownloadType = "img" messageApi.info("请在页面选择要下载区域!") } + if (req.name == "custom-editMarkdown") { + isReady = true + isSelect = false + isDownloadType = "editMarkdown" + messageApi.info("请在页面选择要下载区域!") + } if (req.name == "app-full-page-screenshot") { const { scrollHeight, clientHeight } = document.documentElement const devicePixelRatio = window.devicePixelRatio || 1 @@ -170,7 +183,8 @@ export default function CustomOverlay() { document.addEventListener("click", (event) => { const target = event.target as HTMLElement const tooltip = target.closest("#codebox-csui") - if (isReady && target && !tooltip) { + const modal = target.closest(".ant-modal") + if (isReady && target && !tooltip && !modal) { isSelect = true setIsCurrentDom(true) removeCurrentDom() @@ -209,6 +223,12 @@ export default function CustomOverlay() { saveMarkdown(markdown, articleTitle) } + function editMarkdown(currentDom) { + const markdown = turndownService.turndown(currentDom) + setContent(markdown) + window.open("https://md.randbox.top", "_blank") + } + function downloadPdf(currentDom) { const pdf = new Dom2Pdf(currentDom, articleTitle) pdf.downloadPdf() @@ -224,8 +244,10 @@ export default function CustomOverlay() { removeCurrentDom() if (isDownloadType == "html") { downloadHtml(currentDom) - } else if (isDownloadType == "markdown") { + } else if (isDownloadType == "downloadMarkdown") { downloadMarkdown(currentDom) + } else if (isDownloadType == "editMarkdown") { + editMarkdown(currentDom) } else if (isDownloadType == "pdf") { downloadPdf(currentDom) } else if (isDownloadType == "img") { @@ -243,7 +265,7 @@ export default function CustomOverlay() { <>
是否保存?
- 此功能限时免费免登录,预计10月份以后需要注册,后续可能收费... + 此功能限时免费免登录,预计需要注册,后续可能收费...
), diff --git a/contents/juejin.tsx b/contents/juejin.tsx index 10c2728..b721862 100644 --- a/contents/juejin.tsx +++ b/contents/juejin.tsx @@ -1,7 +1,9 @@ import type { PlasmoCSConfig } from "plasmo" -import { useEffect } from "react" +import { useEffect, useState } from "react" import { useMessage } from "@plasmohq/messaging/hook" +import { Storage } from "@plasmohq/storage" +import { useStorage } from "@plasmohq/storage/dist/hook" import { saveHtml, saveMarkdown, setIcon } from "~tools" import Turndown from "~utils/turndown" @@ -14,6 +16,13 @@ const turndownService = Turndown() const articleTitle = document.querySelector("head title").innerText export default function Juejin() { + const [content, setContent] = useStorage({ + key: "md-content", + instance: new Storage({ + area: "local" + }) + }) + useEffect(() => { setIcon(true) }, []) @@ -22,6 +31,9 @@ export default function Juejin() { if (req.name == "juejin-isShow") { res.send({ isShow: true }) } + if (req.name == "juejin-editMarkdown") { + editMarkdown() + } if (req.name == "juejin-downloadMarkdown") { downloadMarkdown() } @@ -30,6 +42,13 @@ export default function Juejin() { } }) + function editMarkdown() { + const html = document.querySelector("article.article") + const markdown = turndownService.turndown(html) + setContent(markdown) + window.open("https://md.randbox.top", "_blank") + } + function downloadMarkdown() { const html = document.querySelector("article.article") const markdown = turndownService.turndown(html) diff --git a/contents/md.tsx b/contents/md.tsx new file mode 100644 index 0000000..9edf591 --- /dev/null +++ b/contents/md.tsx @@ -0,0 +1,29 @@ +import type { PlasmoCSConfig } from "plasmo" +import { useEffect } from "react" + +import { Storage } from "@plasmohq/storage" +import { useStorage } from "@plasmohq/storage/dist/hook" + +export const config: PlasmoCSConfig = { + matches: ["https://md.randbox.top/*"] +} + +export default function Markdown() { + const [content] = useStorage({ + key: "md-content", + instance: new Storage({ + area: "local" + }) + }) + + useEffect(() => { + if (content) { + if (window.localStorage.getItem("__editor_content") != content) { + window.localStorage.setItem("__editor_content", content) + location.reload() + } + } + }, [content]) + + return
+} diff --git a/locales/en/messages.json b/locales/en/messages.json index 4e40e02..f0c3efd 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -128,6 +128,9 @@ "zhihuAutoOpenCode": { "message": "Auto Open Code" }, + "editMarkdown": { + "message": "Edit Markdown" + }, "downloadMarkdown": { "message": "Download Markdown" }, diff --git a/locales/zh_CN/messages.json b/locales/zh_CN/messages.json index f6f67db..c38177e 100644 --- a/locales/zh_CN/messages.json +++ b/locales/zh_CN/messages.json @@ -128,6 +128,9 @@ "zhihuAutoOpenCode": { "message": "自动展开全文" }, + "editMarkdown": { + "message": "编辑 Markdown" + }, "downloadMarkdown": { "message": "下载 Markdown" }, diff --git a/package.json b/package.json index 6d5336b..abc1820 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "code-box", "displayName": "__MSG_extensionName__", - "version": "0.7.6", + "version": "0.7.7", "description": "__MSG_extensionDescription__", "author": "027xiguapi. <458813868@qq.com>", "scripts": {