mirror of
https://github.com/027xiguapi/code-box.git
synced 2026-09-27 17:48:27 +00:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import type { PlasmoCSConfig } from "plasmo"
|
|
import { useEffect } from "react"
|
|
|
|
import { useMessage } from "@plasmohq/messaging/hook"
|
|
|
|
import { saveHtml, saveMarkdown, setIcon } from "~tools"
|
|
import Turndown from "~utils/turndown"
|
|
|
|
export const config: PlasmoCSConfig = {
|
|
matches: ["https://*.segmentfault.com/*"]
|
|
}
|
|
|
|
const turndownService = Turndown()
|
|
const articleTitle = document.querySelector<HTMLElement>("head title").innerText
|
|
|
|
export default function Segmentfault() {
|
|
useEffect(() => {
|
|
setIcon(true)
|
|
}, [])
|
|
|
|
useMessage(async (req, res) => {
|
|
if (req.name == "segmentfault-isShow") {
|
|
res.send({ isShow: true })
|
|
}
|
|
if (req.name == "segmentfault-downloadMarkdown") {
|
|
downloadMarkdown()
|
|
}
|
|
if (req.name == "segmentfault-downloadHtml") {
|
|
downloadHtml()
|
|
}
|
|
})
|
|
|
|
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>
|
|
}
|