From ce7ec2c0bd244f8639ecb532bedd8a496a68328e Mon Sep 17 00:00:00 2001 From: fzk <458813868@qq.com> Date: Mon, 2 Sep 2024 11:09:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0oschina=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- component/content.tsx | 5 ++++ component/oschina.tsx | 33 ++++++++++++++++++++++ component/sortableItem.tsx | 4 +++ contents/juejin.tsx | 2 +- contents/oschina.tsx | 55 +++++++++++++++++++++++++++---------- locales/en/messages.json | 3 ++ locales/zh_CN/messages.json | 7 +++-- options/index.tsx | 14 +++++++++- package.json | 4 +-- 10 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 component/oschina.tsx diff --git a/README.md b/README.md index b3e28da..a2e0360 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ https://github.com/027xiguapi/code-box ## 介绍 -本浏览器插件可以用于CSDN/知乎/脚本之家/博客园/博客园/51CTO博客/php中文网等网站,一键下载文章html或markdown文件;实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗. +本浏览器插件可以用于CSDN/知乎/脚本之家/博客园/博客园/51CTO博客/php中文网/掘金等网站,一键下载文章成html或markdown文件;实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗. ## 安装 diff --git a/component/content.tsx b/component/content.tsx index de05059..da52f99 100644 --- a/component/content.tsx +++ b/component/content.tsx @@ -11,6 +11,7 @@ import Custom from "~component/custom" import Jb51 from "~component/jb51" import Jianshu from "~component/jianshu" import Juejin from "~component/juejin" +import Oschina from "~component/oschina" import Php from "~component/php" import Zhihu from "~component/zhihu" @@ -24,6 +25,7 @@ export default function Content() { const [ctoIsShow, set51ctoIsShow] = useState(false) const [juejinIsShow, setJuejinIsShow] = useState(false) const [phpIsShow, setPhpIsShow] = useState(false) + const [oschinaIsShow, setOschinaIsShow] = useState(false) const [customIsShow, setCustomIsShow] = useState(false) useEffect(() => { @@ -36,6 +38,7 @@ export default function Content() { getIsShow("51cto") getIsShow("juejin") getIsShow("php") + getIsShow("oschina") getIsShow("custom") }, []) @@ -49,6 +52,7 @@ export default function Content() { "51cto": set51ctoIsShow, php: setPhpIsShow, juejin: setJuejinIsShow, + oschina: setOschinaIsShow, custom: setCustomIsShow } @@ -68,6 +72,7 @@ export default function Content() { {ctoIsShow ? : <>} {juejinIsShow ? : <>} {phpIsShow ? : <>} + {oschinaIsShow ? : <>} {customIsShow ? : <>} diff --git a/component/oschina.tsx b/component/oschina.tsx new file mode 100644 index 0000000..793d6b2 --- /dev/null +++ b/component/oschina.tsx @@ -0,0 +1,33 @@ +import { DownloadOutlined } from "@ant-design/icons" + +import { sendToContentScript } from "@plasmohq/messaging" + +import { i18n } from "~tools" + +export default function Oschina() { + function downloadMarkdown() { + sendToContentScript({ + name: "oschina-downloadMarkdown" + }) + } + + function downloadHtml() { + sendToContentScript({ + name: "oschina-downloadHtml" + }) + } + + return ( +
+ {i18n("oschinaConfig")} +
+ {i18n("downloadMarkdown")} + +
+
+ {i18n("downloadHtml")} + +
+
+ ) +} diff --git a/component/sortableItem.tsx b/component/sortableItem.tsx index 37b57a7..ba47897 100644 --- a/component/sortableItem.tsx +++ b/component/sortableItem.tsx @@ -10,6 +10,8 @@ import Csdn from "~component/csdn" import Custom from "~component/custom" import Jb51 from "~component/jb51" import Jianshu from "~component/jianshu" +import Juejin from "~component/juejin" +import Oschina from "~component/oschina" import Php from "~component/php" import Zhihu from "~component/zhihu" @@ -19,6 +21,8 @@ const itemMap = { csdn: , zhihu: , baidu: , + juejin: , + oschina: , jianshu: , jb51: , cnblogs: , diff --git a/contents/juejin.tsx b/contents/juejin.tsx index 1696296..19b50c2 100644 --- a/contents/juejin.tsx +++ b/contents/juejin.tsx @@ -24,7 +24,7 @@ const author = article.byline ?? "" const authorLink = getMetaContentByProperty("article:author") const domain = window.location.hostname -export default function Custom() { +export default function Juejin() { useEffect(() => { setIcon(true) }, []) diff --git a/contents/oschina.tsx b/contents/oschina.tsx index 888efb4..e067f44 100644 --- a/contents/oschina.tsx +++ b/contents/oschina.tsx @@ -1,28 +1,55 @@ import type { PlasmoCSConfig } from "plasmo" import { useEffect } from "react" -import { useStorage } from "@plasmohq/storage/hook" +import { useMessage } from "@plasmohq/messaging/hook" -import { addCss } from "~tools" +import { Readability } from "~node_modules/@mozilla/readability" +import { + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" +import Turndown from "~utils/turndown" export const config: PlasmoCSConfig = { - matches: ["https://*.baidu.com/*"] + matches: ["https://*.oschina.net/*"] } -export default function Custom() { - const [closeAIBox] = useStorage("baidu-closeAIBox") - const [closeLog] = useStorage("config-closeLog", true) +const turndownService = Turndown() +const documentClone = document.cloneNode(true) +const article = new Readability(documentClone as Document, {}).parse() +const articleUrl = window.location.href +const author = article.byline ?? "" +const authorLink = getMetaContentByProperty("article:author") +const domain = window.location.hostname +export default function Oschina() { useEffect(() => { - closeLog || console.log("baidu", { closeAIBox }) - closeAIBox && closeAIBoxFunc() - }, [closeAIBox]) + setIcon(true) + }, []) - /* 删除百度AI对话框 */ - function closeAIBoxFunc() { - addCss(`.wd-ai-index-pc{ - display:none !important; - }`) + useMessage(async (req, res) => { + if (req.name == "oschina-isShow") { + res.send({ isShow: true }) + } + if (req.name == "oschina-downloadMarkdown") { + downloadMarkdown() + } + if (req.name == "oschina-downloadHtml") { + downloadHtml() + } + }) + + function downloadMarkdown() { + const html = document.querySelector(".article-box") + const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) + } + + function downloadHtml() { + const dom = document.querySelector(".article-box") + saveHtml(dom, article.title) } return
diff --git a/locales/en/messages.json b/locales/en/messages.json index 0fcc4ae..aa5c807 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -133,5 +133,8 @@ }, "juejinConfig": { "message": "Juejin Config" + }, + "oschinaConfig": { + "message": "Oschina Config" } } \ No newline at end of file diff --git a/locales/zh_CN/messages.json b/locales/zh_CN/messages.json index 5995b46..bae71a1 100644 --- a/locales/zh_CN/messages.json +++ b/locales/zh_CN/messages.json @@ -1,9 +1,9 @@ { "extensionName": { - "message": "codebox-免登录一键复制代码" + "message": "codebox-一键复制代码/下载文章" }, "extensionDescription": { - "message": "实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗;页面自定义样式." + "message": "实现一键下载文章成html或markdown文件;实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗;页面自定义样式." }, "popupDescription": { "message": "免登录一键复制代码" @@ -133,5 +133,8 @@ }, "juejinConfig": { "message": "掘金设置" + }, + "oschinaConfig": { + "message": "oschina设置" } } \ No newline at end of file diff --git a/options/index.tsx b/options/index.tsx index 43dd64e..39a3497 100644 --- a/options/index.tsx +++ b/options/index.tsx @@ -68,12 +68,24 @@ export default function IndexOptions() { }, { id: "8", + value: "juejin", + label: "掘金", + isShow: true + }, + { + id: "9", + value: "oschina", + label: "oschina", + isShow: true + }, + { + id: "10", value: "custom", label: "自定义", isShow: true }, { - id: "9", + id: "11", value: "app", label: "app", isShow: true diff --git a/package.json b/package.json index 6533238..20e14fd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "code-box", "displayName": "__MSG_extensionName__", - "version": "0.5.7", + "version": "0.5.8", "description": "__MSG_extensionDescription__", "author": "027xiguapi. <458813868@qq.com>", "scripts": { @@ -34,7 +34,7 @@ "dayjs": "^1.11.12", "file-saver": "^2.0.5", "jssha": "^3.3.1", - "plasmo": "0.88.0", + "plasmo": "0.89.1", "react": "18.2.0", "react-dom": "18.2.0", "turndown": "^7.2.0",