mirror of
https://github.com/027xiguapi/code-box.git
synced 2026-09-27 17:48:27 +00:00
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import type { PlasmoCSConfig } from "plasmo"
|
|
|
|
import { useMessage } from "@plasmohq/messaging/hook"
|
|
|
|
import { saveHtml, saveMarkdown } from "~tools"
|
|
import useCssCodeHook from "~utils/cssCodeHook"
|
|
import { savePdf } from "~utils/downloadPdf"
|
|
import { useContent } from "~utils/editMarkdownHook"
|
|
import Turndown from "~utils/turndown"
|
|
|
|
export const config: PlasmoCSConfig = {
|
|
matches: ["https://*.segmentfault.com/*"]
|
|
}
|
|
|
|
const turndownService = Turndown()
|
|
const articleTitle = document
|
|
.querySelector<HTMLElement>("head title")
|
|
.innerText.trim()
|
|
|
|
export default function Segmentfault() {
|
|
const [cssCode, runCss] = useCssCodeHook("segmentfault")
|
|
const [content, setContent] = useContent()
|
|
|
|
useMessage(async (req, res) => {
|
|
if (req.name == "segmentfault-isShow") {
|
|
res.send({ isShow: true })
|
|
}
|
|
if (req.name == "segmentfault-editMarkdown") {
|
|
setContent("article.article")
|
|
}
|
|
if (req.name == "segmentfault-downloadMarkdown") {
|
|
downloadMarkdown()
|
|
}
|
|
if (req.name == "segmentfault-downloadHtml") {
|
|
downloadHtml()
|
|
}
|
|
if (req.name == "segmentfault-downloadPdf") {
|
|
var article = document.querySelector<HTMLElement>("article.article")
|
|
savePdf(article, articleTitle)
|
|
}
|
|
})
|
|
|
|
function downloadMarkdown() {
|
|
const html = document.querySelector("article.article")
|
|
const markdown = turndownService.turndown(html)
|
|
saveMarkdown(markdown, articleTitle)
|
|
}
|
|
|
|
function downloadHtml() {
|
|
const dom = document.querySelector("article.article")
|
|
saveHtml(dom, articleTitle)
|
|
}
|
|
|
|
return <div style={{ display: "none" }}></div>
|
|
}
|