forked from 027xiguapi/code-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoschina.tsx
56 lines (47 loc) · 1.43 KB
/
oschina.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { PlasmoCSConfig } from "plasmo"
import { useEffect } from "react"
import { useMessage } from "@plasmohq/messaging/hook"
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://*.oschina.net/*"]
}
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(() => {
setIcon(true)
}, [])
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 <div style={{ display: "none" }}></div>
}