mirror of
https://github.com/027xiguapi/code-box.git
synced 2026-04-28 15:22:15 +00:00
167 lines
4.7 KiB
TypeScript
167 lines
4.7 KiB
TypeScript
import type {
|
|
PlasmoCSConfig,
|
|
PlasmoCSUIProps,
|
|
PlasmoGetOverlayAnchor,
|
|
PlasmoGetShadowHostId,
|
|
PlasmoGetStyle
|
|
} from "plasmo"
|
|
import React, { useEffect, useState, type FC } from "react"
|
|
|
|
import { useMessage } from "@plasmohq/messaging/hook"
|
|
import { useStorage } from "@plasmohq/storage/hook"
|
|
|
|
import TagBtnStyle from "~component/tagBtn/style"
|
|
import Tags from "~component/ui/tags"
|
|
import ToolBox from "~component/ui/toolBox"
|
|
import { addCss, i18n, removeCss, saveHtml, saveMarkdown } from "~tools"
|
|
import useCssCodeHook from "~utils/cssCodeHook"
|
|
import { useEditMarkdown } from "~utils/editMarkdownHook"
|
|
import { useParseMarkdown } from "~utils/parseMarkdownHook"
|
|
import { Print } from "~utils/print"
|
|
import Turndown from "~utils/turndown"
|
|
|
|
export const config: PlasmoCSConfig = {
|
|
matches: ["https://*.php.cn/*"]
|
|
}
|
|
|
|
const turndownService = Turndown()
|
|
const articleTitle = document
|
|
.querySelector<HTMLElement>("head title")
|
|
.innerText.trim()
|
|
|
|
const HOST_ID = "codebox-php"
|
|
export const getShadowHostId: PlasmoGetShadowHostId = () => HOST_ID
|
|
|
|
export const getOverlayAnchor: PlasmoGetOverlayAnchor = async () =>
|
|
document.querySelector(".wzContent .wzctTitle")
|
|
|
|
export const getStyle: PlasmoGetStyle = () => TagBtnStyle()
|
|
|
|
const PlasmoOverlay: FC<PlasmoCSUIProps> = ({ anchor }) => {
|
|
const [parseContent, setParseContent] = useParseMarkdown()
|
|
const [allShowTag, setAllShowTag] = useStorage("config-allShowTag", true)
|
|
const [showTag, setShowTag] = useStorage<boolean>("php-showTag", true)
|
|
const [cssCode, runCss] = useCssCodeHook("php")
|
|
const [copyCode] = useStorage("php-copyCode", true)
|
|
const [closeLoginModal] = useStorage<boolean>("php-closeLoginModal")
|
|
const [history, setHistory] = useStorage<any[]>("codebox-history")
|
|
const [closeLog] = useStorage("config-closeLog", true)
|
|
const [content, setContent] = useEditMarkdown()
|
|
|
|
useEffect(() => {
|
|
closeLog || console.log("PHP status", { closeLoginModal })
|
|
copyCodeFunc(copyCode)
|
|
closeLoginModal && closeLoginModalFunc()
|
|
}, [closeLoginModal])
|
|
|
|
useMessage(async (req, res) => {
|
|
if (req.name == "php-isShow") {
|
|
res.send({ isShow: true })
|
|
}
|
|
if (req.name == "php-editMarkdown") {
|
|
editMarkdown()
|
|
}
|
|
if (req.name == "php-downloadMarkdown") {
|
|
downloadMarkdown()
|
|
}
|
|
if (req.name == "php-downloadHtml") {
|
|
downloadHtml()
|
|
}
|
|
if (req.name == "oschina-downloadPdf") {
|
|
downloadPdf()
|
|
}
|
|
})
|
|
|
|
function downloadPdf() {
|
|
const article = document.querySelector<HTMLElement>(
|
|
".phpscMain .php-article"
|
|
)
|
|
if (article) {
|
|
Print.print(article, { title: articleTitle })
|
|
.then(() => console.log("Printing complete"))
|
|
.catch((error) => console.error("Printing failed:", error))
|
|
}
|
|
}
|
|
|
|
/* 未登录复制代码 */
|
|
function copyCodeCssFunc(copyCode) {
|
|
copyCode
|
|
? addCss(
|
|
`
|
|
.php-article .code,
|
|
.php-article{
|
|
-webkit-touch-callout: auto !important;
|
|
-webkit-user-select: auto !important;
|
|
-khtml-user-select: auto !important;
|
|
-moz-user-select: auto !important;
|
|
-ms-user-select: auto !important;
|
|
user-select: auto !important;
|
|
}`,
|
|
`php-copyCode-css`
|
|
)
|
|
: removeCss(`php-copyCode-css`)
|
|
}
|
|
|
|
function copyCodeFunc(copyCode) {
|
|
copyCode &&
|
|
document.addEventListener("copy", function (event) {
|
|
const selectedText = window.getSelection().toString()
|
|
event.clipboardData.setData("text/plain", selectedText)
|
|
event.preventDefault()
|
|
})
|
|
copyCodeCssFunc(copyCode)
|
|
}
|
|
|
|
// 关闭登录弹框
|
|
function closeLoginModalFunc() {
|
|
const css = `
|
|
.layui-layer-shade,
|
|
.layui-layer-iframe {
|
|
display:none !important;
|
|
}`
|
|
addCss(css)
|
|
}
|
|
|
|
function getDescription() {
|
|
const summary = document.querySelector<HTMLMetaElement>(
|
|
'meta[name="description"]'
|
|
).content
|
|
summary && prompt("文章摘要:", summary)
|
|
}
|
|
|
|
function editMarkdown() {
|
|
const dom = document.querySelector(".phpscMain .php-article")
|
|
setContent(dom, articleTitle)
|
|
}
|
|
|
|
function downloadMarkdown() {
|
|
const html = document.querySelector(".phpscMain .php-article")
|
|
const markdown = turndownService.turndown(html)
|
|
saveMarkdown(markdown, articleTitle)
|
|
}
|
|
|
|
function downloadHtml() {
|
|
const dom = document.querySelector(".phpscMain .php-article")
|
|
saveHtml(dom, articleTitle)
|
|
}
|
|
|
|
function parseMarkdown() {
|
|
const dom = document.querySelector(".phpscMain .php-article")
|
|
setParseContent(dom)
|
|
}
|
|
|
|
return showTag && allShowTag ? (
|
|
<ToolBox
|
|
onGetDescription={getDescription}
|
|
onEditMarkdown={editMarkdown}
|
|
onDownloadMarkdown={downloadMarkdown}
|
|
onPrint={downloadPdf}
|
|
onParseMarkdown={parseMarkdown}
|
|
/>
|
|
) : (
|
|
<></>
|
|
)
|
|
}
|
|
|
|
export default PlasmoOverlay
|