From 8e4e8fe74eeac6f406e6d0a0616f7bb7830a1637 Mon Sep 17 00:00:00 2001 From: fzk <458813868@qq.com> Date: Wed, 28 Aug 2024 09:43:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E7=BD=91=E9=A1=B5=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E6=88=90markdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/51cto.tsx | 9 +++++++++ component/baidu.tsx | 9 +++++++++ component/cnblogs.tsx | 9 +++++++++ component/csdn.tsx | 2 +- component/jb51.tsx | 9 +++++++++ component/jianshu.tsx | 9 +++++++++ component/php.tsx | 9 +++++++++ component/zhihu.tsx | 9 +++++++++ contents/51cto.tsx | 18 +++++++++++++++++- contents/baidu.tsx | 18 ++++++++++++++++-- contents/cnblogs.tsx | 16 +++++++++++++++- contents/csdn.tsx | 9 ++++++++- contents/jb51.tsx | 17 ++++++++++++++++- contents/jianshu.tsx | 17 ++++++++++++++++- contents/php.tsx | 17 ++++++++++++++++- contents/zhihu.tsx | 17 ++++++++++++++++- package.json | 2 +- tools.ts | 8 ++++++++ 18 files changed, 193 insertions(+), 11 deletions(-) diff --git a/component/51cto.tsx b/component/51cto.tsx index 204ab3a..36ca18c 100644 --- a/component/51cto.tsx +++ b/component/51cto.tsx @@ -12,6 +12,12 @@ export default function Cto51() { (v) => (v === undefined ? true : v) ) + function downloadMarkdown() { + sendToContentScript({ + name: "51cto-downloadMarkdown" + }) + } + function downloadHtml() { sendToContentScript({ name: "51cto-downloadHtml" @@ -47,6 +53,9 @@ export default function Cto51() { htmlFor="51cto-closeLoginModal" className="codebox-switch"> +
+ {i18n("downloadMarkdown")} +
{i18n("downloadHtml")}
diff --git a/component/baidu.tsx b/component/baidu.tsx index 91564aa..8b852a5 100644 --- a/component/baidu.tsx +++ b/component/baidu.tsx @@ -8,6 +8,12 @@ export default function Custom() { v === undefined ? false : v ) + function downloadMarkdown() { + sendToContentScript({ + name: "baidu-downloadMarkdown" + }) + } + function downloadHtml() { sendToContentScript({ name: "baidu-downloadHtml" @@ -29,6 +35,9 @@ export default function Custom() { /> +
+ {i18n("downloadMarkdown")} +
{i18n("downloadHtml")}
diff --git a/component/cnblogs.tsx b/component/cnblogs.tsx index 386814a..c3250d9 100644 --- a/component/cnblogs.tsx +++ b/component/cnblogs.tsx @@ -14,6 +14,12 @@ export default function Cnblogs() { }) } + function downloadMarkdown() { + sendToContentScript({ + name: "cnblogs-downloadMarkdown" + }) + } + return (
{i18n("cnblogsConfig")} @@ -29,6 +35,9 @@ export default function Cnblogs() { /> +
+ {i18n("downloadMarkdown")} +
{i18n("downloadHtml")}
diff --git a/component/csdn.tsx b/component/csdn.tsx index 511cc86..fb841f1 100644 --- a/component/csdn.tsx +++ b/component/csdn.tsx @@ -133,7 +133,7 @@ export default function Csdn() { htmlFor="csdn-closeRedirectModal" className="codebox-switch"> -
+
{i18n("downloadMarkdown")}
diff --git a/component/jb51.tsx b/component/jb51.tsx index 7d635e2..8236feb 100644 --- a/component/jb51.tsx +++ b/component/jb51.tsx @@ -11,6 +11,12 @@ export default function Jb51() { v === undefined ? true : v ) + function downloadMarkdown() { + sendToContentScript({ + name: "jb51-downloadMarkdown" + }) + } + function downloadHtml() { sendToContentScript({ name: "jb51-downloadHtml" @@ -44,6 +50,9 @@ export default function Jb51() { />
+
+ {i18n("downloadMarkdown")} +
{i18n("downloadHtml")}
diff --git a/component/jianshu.tsx b/component/jianshu.tsx index c354f17..c93e0e4 100644 --- a/component/jianshu.tsx +++ b/component/jianshu.tsx @@ -16,6 +16,12 @@ export default function Jianshu() { (v) => (v === undefined ? true : v) ) + function downloadMarkdown() { + sendToContentScript({ + name: "jianshu-downloadMarkdown" + }) + } + function downloadHtml() { sendToContentScript({ name: "jianshu-downloadHtml" @@ -65,6 +71,9 @@ export default function Jianshu() { className="codebox-switch" htmlFor="jianshu-autoOpenCode">
+
+ {i18n("downloadMarkdown")} +
{i18n("downloadHtml")}
diff --git a/component/php.tsx b/component/php.tsx index 67b6f7e..80017d2 100644 --- a/component/php.tsx +++ b/component/php.tsx @@ -18,6 +18,12 @@ export default function Php() { }) } + function downloadMarkdown() { + sendToContentScript({ + name: "php-downloadMarkdown" + }) + } + return (
{i18n("phpConfig")} @@ -45,6 +51,9 @@ export default function Php() { /> +
+ {i18n("downloadMarkdown")} +
{i18n("downloadHtml")}
diff --git a/component/zhihu.tsx b/component/zhihu.tsx index 53e1879..78f5f1e 100644 --- a/component/zhihu.tsx +++ b/component/zhihu.tsx @@ -22,6 +22,12 @@ export default function Zhihu() { }) } + function downloadMarkdown() { + sendToContentScript({ + name: "zhihu-downloadMarkdown" + }) + } + return (
{i18n("zhihuConfig")} @@ -63,6 +69,9 @@ export default function Zhihu() { /> +
+ {i18n("downloadMarkdown")} +
{i18n("downloadHtml")}
diff --git a/contents/51cto.tsx b/contents/51cto.tsx index 7e26803..36a8d0c 100644 --- a/contents/51cto.tsx +++ b/contents/51cto.tsx @@ -1,17 +1,26 @@ import type { PlasmoCSConfig } from "plasmo" import { useEffect } from "react" +import TurndownService from "turndown" import { v4 as uuidv4 } from "uuid" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" import { Readability } from "~node_modules/@mozilla/readability" -import { addCss, getMetaContentByProperty, saveHtml, setIcon } from "~tools" +import { + addCss, + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" export const config: PlasmoCSConfig = { matches: ["https://*.51cto.com/*"] } +const turndownService = new TurndownService() + const documentClone = document.cloneNode(true) const article = new Readability(documentClone as Document, {}).parse() const articleUrl = window.location.href @@ -39,6 +48,7 @@ export default function Cto51() { res.send({ isShow: true }) } if (req.name == "51cto-downloadMarkdown") { + downloadMarkdown() } if (req.name == "51cto-downloadHtml") { downloadHtml() @@ -192,6 +202,12 @@ export default function Cto51() { addCss(css) } + function downloadMarkdown() { + const html = document.querySelector("article") + const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) + } + function downloadHtml() { const dom = document.querySelector("article") saveHtml(dom, article.title) diff --git a/contents/baidu.tsx b/contents/baidu.tsx index fb68250..3bc3c52 100644 --- a/contents/baidu.tsx +++ b/contents/baidu.tsx @@ -1,16 +1,23 @@ import type { PlasmoCSConfig } from "plasmo" import { useEffect } from "react" +import TurndownService from "turndown" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" import { Readability } from "~node_modules/@mozilla/readability" -import { addCss, getMetaContentByProperty, saveHtml, setIcon } from "~tools" +import { + addCss, + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" export const config: PlasmoCSConfig = { matches: ["https://*.baidu.com/*"] } - +const turndownService = new TurndownService() const documentClone = document.cloneNode(true) const article = new Readability(documentClone as Document, {}).parse() const articleUrl = window.location.href @@ -33,6 +40,7 @@ export default function Custom() { res.send({ isShow: true }) } if (req.name == "baidu-downloadMarkdown") { + downloadMarkdown() } if (req.name == "baidu-downloadHtml") { downloadHtml() @@ -46,6 +54,12 @@ export default function Custom() { }`) } + function downloadMarkdown() { + const html = document.querySelector(".wd-ai-index-pc") + const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) + } + function downloadHtml() { const dom = document.querySelector(".wd-ai-index-pc") saveHtml(dom, article.title) diff --git a/contents/cnblogs.tsx b/contents/cnblogs.tsx index a833696..10c128e 100644 --- a/contents/cnblogs.tsx +++ b/contents/cnblogs.tsx @@ -1,18 +1,25 @@ import type { PlasmoCSConfig } from "plasmo" import { useEffect, useRef } from "react" +import TurndownService from "turndown" import { v4 as uuidv4 } from "uuid" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" import { Readability } from "~node_modules/@mozilla/readability" -import { getMetaContentByProperty, saveHtml, setIcon } from "~tools" +import { + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" export const config: PlasmoCSConfig = { matches: ["https://*.cnblogs.com/*"], all_frames: true } +const turndownService = new TurndownService() const documentClone = document.cloneNode(true) const article = new Readability(documentClone as Document, {}).parse() const articleUrl = window.location.href @@ -36,6 +43,7 @@ export default function cnblogs() { res.send({ isShow: true }) } if (req.name == "cnblogs-downloadMarkdown") { + downloadMarkdown() } if (req.name == "cnblogs-downloadHtml") { downloadHtml() @@ -116,6 +124,12 @@ export default function cnblogs() { }) } + function downloadMarkdown() { + const html = document.querySelector("#post_detail") + const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) + } + function downloadHtml() { const dom = document.querySelector("#post_detail") saveHtml(dom, article.title) diff --git a/contents/csdn.tsx b/contents/csdn.tsx index 3806ced..e03d2f6 100644 --- a/contents/csdn.tsx +++ b/contents/csdn.tsx @@ -7,7 +7,13 @@ import { v4 as uuidv4 } from "uuid" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" -import { addCss, getMetaContentByProperty, saveHtml, setIcon } from "~tools" +import { + addCss, + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" export const config: PlasmoCSConfig = { matches: ["https://*.blog.csdn.net/*"] @@ -246,6 +252,7 @@ const csdn = () => { function downloadMarkdown() { const html = document.querySelector(".blog-content-box") const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) } function downloadHtml() { diff --git a/contents/jb51.tsx b/contents/jb51.tsx index 40e2158..3e1e458 100644 --- a/contents/jb51.tsx +++ b/contents/jb51.tsx @@ -1,17 +1,25 @@ import type { PlasmoCSConfig } from "plasmo" import { useEffect } from "react" +import TurndownService from "turndown" import { v4 as uuidv4 } from "uuid" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" import { Readability } from "~node_modules/@mozilla/readability" -import { addCss, getMetaContentByProperty, saveHtml, setIcon } from "~tools" +import { + addCss, + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" export const config: PlasmoCSConfig = { matches: ["https://*.jb51.net/*"] } +const turndownService = new TurndownService() const documentClone = document.cloneNode(true) const article = new Readability(documentClone as Document, {}).parse() const articleUrl = window.location.href @@ -37,6 +45,7 @@ export default function jb51() { res.send({ isShow: true }) } if (req.name == "jb51-downloadMarkdown") { + downloadMarkdown() } if (req.name == "jb51-downloadHtml") { downloadHtml() @@ -196,6 +205,12 @@ export default function jb51() { }`) } + function downloadMarkdown() { + const html = document.querySelector("#article") + const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) + } + function downloadHtml() { const dom = document.querySelector("#article") saveHtml(dom, article.title) diff --git a/contents/jianshu.tsx b/contents/jianshu.tsx index b13ab1b..183fd3b 100644 --- a/contents/jianshu.tsx +++ b/contents/jianshu.tsx @@ -1,17 +1,25 @@ import type { PlasmoCSConfig } from "plasmo" import { useEffect, useRef } from "react" +import TurndownService from "turndown" import { v4 as uuidv4 } from "uuid" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" import { Readability } from "~node_modules/@mozilla/readability" -import { addCss, getMetaContentByProperty, saveHtml, setIcon } from "~tools" +import { + addCss, + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" export const config: PlasmoCSConfig = { matches: ["https://*.jianshu.com/*"] } +const turndownService = new TurndownService() const documentClone = document.cloneNode(true) const article = new Readability(documentClone as Document, {}).parse() const articleUrl = window.location.href @@ -40,6 +48,7 @@ export default function Jianshu() { res.send({ isShow: true }) } if (req.name == "jianshu-downloadMarkdown") { + downloadMarkdown() } if (req.name == "jianshu-downloadHtml") { downloadHtml() @@ -146,6 +155,12 @@ export default function Jianshu() { } } + function downloadMarkdown() { + const html = document.querySelector("section.ouvJEz") + const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) + } + function downloadHtml() { const dom = document.querySelector("section.ouvJEz") saveHtml(dom, article.title) diff --git a/contents/php.tsx b/contents/php.tsx index 2295aff..9306538 100644 --- a/contents/php.tsx +++ b/contents/php.tsx @@ -1,17 +1,25 @@ import type { PlasmoCSConfig } from "plasmo" import { useEffect } from "react" +import TurndownService from "turndown" import { v4 as uuidv4 } from "uuid" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" import { Readability } from "~node_modules/@mozilla/readability" -import { addCss, getMetaContentByProperty, saveHtml, setIcon } from "~tools" +import { + addCss, + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" export const config: PlasmoCSConfig = { matches: ["https://*.php.cn/*"] } +const turndownService = new TurndownService() const documentClone = document.cloneNode(true) const article = new Readability(documentClone as Document, {}).parse() const articleUrl = window.location.href @@ -39,6 +47,7 @@ export default function Php() { res.send({ isShow: true }) } if (req.name == "php-downloadMarkdown") { + downloadMarkdown() } if (req.name == "php-downloadHtml") { downloadHtml() @@ -186,6 +195,12 @@ export default function Php() { addCss(css) } + function downloadMarkdown() { + const html = document.querySelector(".phpscMain .php-article") + const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) + } + function downloadHtml() { const dom = document.querySelector(".phpscMain .php-article") saveHtml(dom, article.title) diff --git a/contents/zhihu.tsx b/contents/zhihu.tsx index 6b0e4ec..62c0fe8 100644 --- a/contents/zhihu.tsx +++ b/contents/zhihu.tsx @@ -1,17 +1,25 @@ import type { PlasmoCSConfig } from "plasmo" import { useEffect, useRef } from "react" +import TurndownService from "turndown" import { v4 as uuidv4 } from "uuid" import { useMessage } from "@plasmohq/messaging/hook" import { useStorage } from "@plasmohq/storage/hook" import { Readability } from "~node_modules/@mozilla/readability" -import { addCss, getMetaContentByProperty, saveHtml, setIcon } from "~tools" +import { + addCss, + getMetaContentByProperty, + saveHtml, + saveMarkdown, + setIcon +} from "~tools" export const config: PlasmoCSConfig = { matches: ["https://*.zhihu.com/*"] } +const turndownService = new TurndownService() const documentClone = document.cloneNode(true) const article = new Readability(documentClone as Document, {}).parse() const articleUrl = window.location.href @@ -40,6 +48,7 @@ export default function zhihu() { res.send({ isShow: true }) } if (req.name == "zhihu-downloadMarkdown") { + downloadMarkdown() } if (req.name == "zhihu-downloadHtml") { downloadHtml() @@ -166,6 +175,12 @@ export default function zhihu() { } } + function downloadMarkdown() { + const html = document.querySelector("article.Post-Main") + const markdown = turndownService.turndown(html) + saveMarkdown(markdown, article.title) + } + function downloadHtml() { const dom = document.querySelector("article.Post-Main") saveHtml(dom, article.title) diff --git a/package.json b/package.json index a5ce1b4..55c83d8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "code-box", "displayName": "__MSG_extensionName__", - "version": "0.5.2", + "version": "0.5.3", "description": "__MSG_extensionDescription__", "author": "027xiguapi. <458813868@qq.com>", "scripts": { diff --git a/tools.ts b/tools.ts index 8a7afb1..49d196f 100644 --- a/tools.ts +++ b/tools.ts @@ -35,6 +35,14 @@ export function saveHtml(dom: Element, filename?: string) { } } +export function saveMarkdown(markdown: string, filename?: string) { + if (markdown) { + const blob = new Blob([markdown], { type: "text/markdown;charset=utf-8" }) + filename = filename || "CodeBox-page" + saveAs(blob, `${filename}.md`) + } +} + export function i18n(key: string) { return chrome.i18n.getMessage(key) }