删除模块复制代码

This commit is contained in:
fzk
2024-10-18 17:09:56 +08:00
parent f8e6fc41ab
commit 8cc55ccd57
11 changed files with 96 additions and 815 deletions
+3 -133
View File
@@ -1,6 +1,5 @@
import type { PlasmoCSConfig } from "plasmo"
import { useEffect } from "react"
import { v4 as uuidv4 } from "uuid"
import { useMessage } from "@plasmohq/messaging/hook"
import { useStorage } from "@plasmohq/storage/hook"
@@ -19,20 +18,15 @@ const articleTitle = document.querySelector<HTMLElement>("head title").innerText
export default function Cto51() {
const [cssCode, runCss] = useCssCodeHook("51cto")
const [copyCode] = useStorage<boolean>("51cto-copyCode")
const [closeLoginModal] = useStorage<boolean>("51cto-closeLoginModal")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
const [content, setContent] = useContent()
useEffect(() => {
closeLog || console.log("51CTO status", { closeLoginModal, copyCode })
setTimeout(() => {
copyCode && copyCodeFunc()
}, 500)
closeLog || console.log("51CTO status", { closeLoginModal })
closeLoginModal && closeLoginModalFunc()
setIcon(copyCode || closeLoginModal)
}, [copyCode, closeLoginModal])
setIcon(true)
}, [closeLoginModal])
useMessage(async (req, res) => {
if (req.name == "51cto-isShow") {
@@ -63,130 +57,6 @@ export default function Cto51() {
}`)
}
function copyCodeFunc() {
copyCodeCssFunc()
// 内容区开启复制
const content_views = document.querySelector("article")
content_views && content_views.replaceWith(content_views.cloneNode(true))
// 功能一: 修改复制按钮,支持一键复制
const buttons = document.querySelectorAll<HTMLElement>(
".hljs-cto .copy_btn"
)
if (buttons.length > 0) {
buttons.forEach((btn) => {
btn.innerText = "复制"
btn.setAttribute("onclick", "")
const elClone = btn.cloneNode(true)
btn.parentNode.replaceChild(elClone, btn)
elClone.addEventListener("click", (e) => {
const target = e.target as HTMLElement
const parentPreBlock = target.closest(".hljs-cto")
const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
const codeIndex =
codeBlock.querySelector<HTMLElement>(".pre-numbering")
codeBlock.removeChild(codeIndex)
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "51CTO",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "51CTO",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
} else {
const codes = document.querySelectorAll<HTMLElement>(
"article .has-pre-numbering"
)
codes.forEach((code) => {
const codeBlock = code.closest("div")
const button = document.createElement("button")
button.innerText = "复制"
button.style.position = "absolute"
button.style.top = "0"
button.style.right = "0"
button.title = "一键复制代码"
button.classList.add("Button")
button.classList.add("VoteButton")
codeBlock.appendChild(button)
codeBlock.style.position = "relative"
const codeIndex = codeBlock.querySelector<HTMLElement>(".pre-numbering")
code.removeChild(codeIndex)
button.addEventListener("click", (e) => {
const target = e.target as HTMLElement
navigator.clipboard.writeText(code.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "51CTO",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "51CTO",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
}
// 关闭登录弹框
function closeLoginModalFunc() {
const css = `
+1 -1
View File
@@ -25,7 +25,7 @@ export default function Custom() {
useEffect(() => {
closeLog || console.log("baidu", { closeAIBox })
closeAIBox && closeAIBoxFunc()
setIcon(closeAIBox)
setIcon(true)
}, [closeAIBox])
useMessage(async (req, res) => {
+2 -80
View File
@@ -1,6 +1,5 @@
import type { PlasmoCSConfig } from "plasmo"
import { useEffect, useRef } from "react"
import { v4 as uuidv4 } from "uuid"
import { useMessage } from "@plasmohq/messaging/hook"
import { useStorage } from "@plasmohq/storage/hook"
@@ -20,16 +19,13 @@ const articleTitle = document.querySelector<HTMLElement>("head title").innerText
export default function cnblogs() {
const [cssCode, runCss] = useCssCodeHook("cnblogs")
const [copyCode] = useStorage<boolean>("cnblogs-copyCode")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
const [content, setContent] = useContent()
useEffect(() => {
closeLog || console.log("cnblogs copyCode", copyCode)
copyCode && copyCodeFunc()
setIcon(copyCode)
}, [copyCode])
setIcon(true)
}, [])
useMessage(async (req, res) => {
if (req.name == "cnblogs-isShow") {
@@ -46,80 +42,6 @@ export default function cnblogs() {
}
})
// 功能一: 修改复制按钮,支持一键复制
function copyCodeFunc() {
const toolbars = document.querySelectorAll<HTMLElement>(
".cnblogs_code_toolbar"
)
toolbars.forEach((toolbar) => {
const button = document.createElement("button")
button.innerText = "复制"
button.style.float = "right"
button.title = "一键复制代码"
button.classList.add("copy-btn")
toolbar.appendChild(button)
})
const buttons = document.querySelectorAll<HTMLElement>(
".cnblogs_code_toolbar .copy-btn"
)
buttons.forEach((btn) => {
// 移除点击事件
btn.setAttribute("onclick", "")
// 克隆按钮
var elClone = btn.cloneNode(true)
// 替回按钮
btn.parentNode.replaceChild(elClone, btn)
// 重新添加点击事件
elClone.addEventListener("click", (e) => {
// 实现复制
const target = e.target as HTMLElement
const parentPreBlock = target.closest(".cnblogs_code")
const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "博客园",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "博客园",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
function downloadMarkdown() {
const html = document.querySelector("#post_detail")
const markdown = turndownService.turndown(html)
+73 -2
View File
@@ -4,11 +4,14 @@ import type {
PlasmoGetOverlayAnchorList,
PlasmoGetStyle
} from "plasmo"
import { useState } from "react"
import { useEffect, useState } from "react"
import type { FC } from "react"
import { v4 as uuidv4 } from "uuid"
import { useStorage } from "@plasmohq/storage/dist/hook"
import { addCss, removeCss } from "~tools"
export const getStyle: PlasmoGetStyle = () => {
const style = document.createElement("style")
style.textContent = `
@@ -35,14 +38,54 @@ export const getOverlayAnchorList: PlasmoGetOverlayAnchorList = async () =>
const PlasmoOverlay: FC<PlasmoCSUIProps> = ({ anchor }) => {
const [copyCode] = useStorage("config-copyCode", true)
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [isCopy, setIsCopy] = useState(false)
useEffect(() => {
copyCodeCssFunc(copyCode)
}, [copyCode])
const onCopy = async () => {
try {
const target = anchor.element as HTMLElement
const preBlock = target.closest("pre")
const codeBlock = target.querySelector("code")
let textContent = ""
preBlock && navigator.clipboard.writeText(preBlock.textContent)
if (codeBlock) {
textContent = codeBlock.textContent
} else {
textContent = preBlock && preBlock.textContent
}
navigator.clipboard.writeText(textContent)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "CSDN",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "CSDN",
link: location.href,
tags: [],
remark: ""
}
]
)
setIsCopy(true)
setTimeout(() => {
@@ -53,6 +96,34 @@ const PlasmoOverlay: FC<PlasmoCSUIProps> = ({ anchor }) => {
}
}
function copyCodeCssFunc(copyCode) {
copyCode
? addCss(
`
#article .jb51code,
#article .code {
-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-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;
}
`,
"codebox-copyCodeCss"
)
: removeCss("codebox-copyCodeCss")
}
return (
<>
{copyCode ? (
+2 -102
View File
@@ -1,6 +1,5 @@
import type { PlasmoCSConfig } from "plasmo"
import { useEffect, useRef } from "react"
import { v4 as uuidv4 } from "uuid"
import { useMessage } from "@plasmohq/messaging/hook"
import { useStorage } from "@plasmohq/storage/hook"
@@ -20,7 +19,6 @@ const articleTitle = document.querySelector<HTMLElement>("head title").innerText
const csdn = () => {
const [cssCode, runCss] = useCssCodeHook("csdn")
const [closeAds] = useStorage<boolean>("csdn-closeAds")
const [copyCode] = useStorage<boolean>("csdn-copyCode")
const [closeFollow] = useStorage<boolean>("csdn-closeFollow")
const [closeVip] = useStorage<boolean>("csdn-closeVip")
const [autoOpenCode] = useStorage<boolean>("csdn-autoOpenCode")
@@ -34,7 +32,6 @@ const csdn = () => {
closeLog ||
console.log("CSDN status", {
closeAds,
copyCode,
autoOpenCode,
closeFollow,
closeVip,
@@ -42,28 +39,13 @@ const csdn = () => {
closeRedirectModal
})
closeAds && closeAdsFunc()
copyCode && copyCodeFunc()
autoOpenCode && autoOpenCodeFunc()
closeFollow && followFunc()
closeVip && closeVipFunc()
closeLoginModal && closeLoginModalFunc()
closeRedirectModal && closeRedirectModalFunc()
setIcon(
closeAds ||
copyCode ||
autoOpenCode ||
closeFollow ||
closeLoginModal ||
closeRedirectModal
)
}, [
closeAds,
copyCode,
autoOpenCode,
closeFollow,
closeLoginModal,
closeRedirectModal
])
setIcon(true)
}, [closeAds, autoOpenCode, closeFollow, closeLoginModal, closeRedirectModal])
useMessage(async (req, res) => {
if (req.name == "csdn-isShow") {
@@ -80,88 +62,6 @@ const csdn = () => {
}
})
/* 未登录复制代码 */
function copyCodeCssFunc() {
const css = `
#content_views pre,
#content_views pre code {
-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;
}`
addCss(css)
}
function copyCodeFunc() {
copyCodeCssFunc()
// 内容区开启复制
var content_views = document.querySelector("#content_views")
content_views.replaceWith(content_views.cloneNode(true))
// 功能一: 修改复制按钮,支持一键复制
const buttons = document.querySelectorAll<HTMLElement>(".hljs-button")
buttons.forEach((btn) => {
// 更改标题
btn.dataset.title = "复制"
// 移除点击事件
btn.setAttribute("onclick", "")
// 克隆按钮
var elClone = btn.cloneNode(true)
// 替回按钮
btn.parentNode.replaceChild(elClone, btn)
// 重新添加点击事件
elClone.addEventListener("click", (e) => {
// 实现复制
const target = e.target as HTMLElement
const parentPreBlock = target.closest("pre")
const codeBlock = parentPreBlock.querySelector("code")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "CSDN",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "CSDN",
link: location.href,
tags: [],
remark: ""
}
]
)
target.dataset.title = "复制成功"
setTimeout(() => {
target.dataset.title = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
// 关闭广告
function closeAdsFunc() {
const css = `
+3 -147
View File
@@ -1,6 +1,5 @@
import type { PlasmoCSConfig } from "plasmo"
import { useEffect } from "react"
import { v4 as uuidv4 } from "uuid"
import { useMessage } from "@plasmohq/messaging/hook"
import { useStorage } from "@plasmohq/storage/hook"
@@ -20,17 +19,15 @@ const articleTitle = document.querySelector<HTMLElement>("head title").innerText
export default function jb51() {
const [cssCode, runCss] = useCssCodeHook("jb51")
const [closeAds] = useStorage<boolean>("jb51-closeAds")
const [copyCode] = useStorage<boolean>("jb51-copyCode")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
const [content, setContent] = useContent()
useEffect(() => {
closeLog || console.log("jb51 status", { closeAds, copyCode })
closeLog || console.log("jb51 status", { closeAds })
closeAds && closeAdsFunc()
copyCode && copyCodeFunc()
setIcon(closeAds || copyCode)
}, [closeAds, copyCode])
setIcon(closeAds)
}, [closeAds])
useMessage(async (req, res) => {
if (req.name == "jb51-isShow") {
@@ -47,147 +44,6 @@ export default function jb51() {
}
})
/* 未登录复制代码 */
function copyCodeCssFunc() {
addCss(`
#article .jb51code,
#article .code {
-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;
}`)
}
function copyCodeFunc() {
copyCodeCssFunc()
// 内容区开启复制
var content_views = document.querySelector("#article")
content_views && content_views.replaceWith(content_views.cloneNode(true))
// 功能一: 修改复制按钮,支持一键复制
const buttons = document.querySelectorAll<HTMLElement>(".codetool .copy")
if (buttons.length > 0) {
buttons.forEach((btn) => {
// 更改标题
btn.innerText = "复制"
// 移除点击事件
btn.setAttribute("onclick", "")
// 克隆按钮
var elClone = btn.cloneNode(true)
// 替回按钮
btn.parentNode.replaceChild(elClone, btn)
// 重新添加点击事件
elClone.addEventListener("click", (e) => {
// 实现复制
const target = e.target as HTMLElement
const parentPreBlock = target.closest(".jb51code")
const codeBlock = parentPreBlock.querySelector<HTMLElement>(".code")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "脚本之家",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "脚本之家",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
} else {
const codes = document.querySelectorAll<HTMLElement>("article .jb51code")
codes.forEach((code) => {
const button = document.createElement("button")
button.innerText = "复制"
button.style.position = "absolute"
button.style.top = "0"
button.style.right = "0"
button.title = "一键复制代码"
button.classList.add("Button")
button.classList.add("VoteButton")
code.appendChild(button)
code.style.position = "relative"
button.addEventListener("click", (e) => {
// 实现复制
const target = e.target as HTMLElement
const parentPreBlock = target.closest(".jb51code")
const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "脚本之家",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "脚本之家",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
}
// 关闭广告
function closeAdsFunc() {
addCss(`
+3 -69
View File
@@ -1,6 +1,5 @@
import type { PlasmoCSConfig } from "plasmo"
import { useEffect, useRef } from "react"
import { v4 as uuidv4 } from "uuid"
import { useMessage } from "@plasmohq/messaging/hook"
import { useStorage } from "@plasmohq/storage/hook"
@@ -20,20 +19,16 @@ const articleTitle = document.querySelector<HTMLElement>("head title").innerText
export default function Jianshu() {
const [cssCode, runCss] = useCssCodeHook("jianshu")
const [closeLoginModal] = useStorage<boolean>("jianshu-closeLoginModal")
const [copyCode] = useStorage<boolean>("jianshu-copyCode")
const [autoOpenCode] = useStorage<boolean>("jianshu-autoOpenCode")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
const [content, setContent] = useContent()
useEffect(() => {
closeLog ||
console.log("jianshu status", { copyCode, closeLoginModal, autoOpenCode })
copyCode && copyCodeFunc()
closeLog || console.log("jianshu status", { closeLoginModal, autoOpenCode })
closeLoginModal && closeLoginModalFunc()
autoOpenCode && autoOpenCodeFunc()
setIcon(closeLoginModal || copyCode || autoOpenCode)
}, [copyCode, closeLoginModal, autoOpenCode])
setIcon(true)
}, [closeLoginModal, autoOpenCode])
useMessage(async (req, res) => {
if (req.name == "jianshu-isShow") {
@@ -50,67 +45,6 @@ export default function Jianshu() {
}
})
// 一键复制
function copyCodeFunc() {
const codes = document.querySelectorAll<HTMLElement>(".hljs")
codes.forEach((code) => {
const button = document.createElement("button")
button.innerText = "复制"
button.style.position = "absolute"
button.style.top = "0"
button.style.right = "0"
button.style.background = "#fff"
button.title = "一键复制代码"
button.classList.add("Button")
button.classList.add("VoteButton")
code.appendChild(button)
code.style.position = "relative"
button.addEventListener("click", (e) => {
const target = e.target as HTMLElement
const parentPreBlock = target.closest(".hljs")
const codeBlock = parentPreBlock.querySelector<HTMLElement>("code")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "简书",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "简书",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
// 隐藏登录弹窗
function closeLoginModalFunc() {
addCss(
+3 -138
View File
@@ -19,20 +19,16 @@ const articleTitle = document.querySelector<HTMLElement>("head title").innerText
export default function Php() {
const [cssCode, runCss] = useCssCodeHook("php")
const [copyCode] = useStorage<boolean>("php-copyCode")
const [closeLoginModal] = useStorage<boolean>("php-closeLoginModal")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
const [content, setContent] = useContent()
useEffect(() => {
closeLog || console.log("PHP status", { closeLoginModal, copyCode })
setTimeout(() => {
copyCode && copyCodeFunc()
}, 500)
closeLog || console.log("PHP status", { closeLoginModal })
closeLoginModal && closeLoginModalFunc()
setIcon(closeLoginModal || copyCode)
}, [copyCode, closeLoginModal])
setIcon(true)
}, [closeLoginModal])
useMessage(async (req, res) => {
if (req.name == "php-isShow") {
@@ -49,137 +45,6 @@ export default function Php() {
}
})
/* 未登录复制代码 */
function copyCodeCssFunc() {
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;
}`)
}
function copyCodeFunc() {
copyCodeCssFunc()
// 内容区开启复制
var content_views = document.querySelector(".php-article")
content_views && content_views.replaceWith(content_views.cloneNode(true))
// 功能一: 修改复制按钮,支持一键复制
const buttons = document.querySelectorAll<HTMLElement>(
".php-article .code .contentsignin"
)
if (buttons.length > 0) {
buttons.forEach((btn) => {
btn.innerText = "复制"
btn.setAttribute("onclick", "")
const elClone = btn.cloneNode(true)
btn.parentNode.replaceChild(elClone, btn)
elClone.addEventListener("click", (e) => {
const target = e.target as HTMLElement
const parentPreBlock = target.closest(".code")
const codeBlock = parentPreBlock.querySelector<HTMLElement>(".code")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "PHP中文网",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "PHP中文网",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
} else {
const codes = document.querySelectorAll<HTMLElement>(".nphpQianBox .code")
codes.forEach((code) => {
const button = document.createElement("button")
button.innerText = "复制"
button.style.position = "absolute"
button.style.top = "0"
button.style.right = "0"
button.title = "一键复制代码"
button.classList.add("Button")
button.classList.add("VoteButton")
code.appendChild(button)
code.style.position = "relative"
button.addEventListener("click", (e) => {
const target = e.target as HTMLElement
const codeBlock = code.querySelector<HTMLElement>(".container")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "PHP中文网",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "PHP中文网",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
}
// 关闭登录弹框
function closeLoginModalFunc() {
const css = `
+2 -73
View File
@@ -19,16 +19,12 @@ const articleTitle = document.querySelector<HTMLElement>("head title").innerText
export default function Weixin() {
const [cssCode, runCss] = useCssCodeHook("weixin")
const [copyCode] = useStorage<boolean>("weixin-copyCode")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
const [content, setContent] = useContent()
useEffect(() => {
closeLog || console.log("weixin status", { copyCode })
copyCode && copyCodeFunc()
setIcon(copyCode)
}, [copyCode])
setIcon(true)
}, [])
useMessage(async (req, res) => {
if (req.name == "weixin-isShow") {
@@ -45,73 +41,6 @@ export default function Weixin() {
}
})
function copyCodeFunc() {
const content_views = document.querySelector("#js_content")
// 功能一: 修改复制按钮,支持一键复制
const codes = content_views.querySelectorAll<HTMLElement>("code")
if (codes.length > 0) {
codes.forEach((code) => {
const pre = code.closest("pre")
if (pre) {
const button = document.createElement("button")
button.innerText = "复制"
button.style.position = "absolute"
button.style.top = "0"
button.style.right = "0"
button.title = "一键复制代码"
button.classList.add("Button")
button.classList.add("VoteButton")
pre.appendChild(button)
pre.style.position = "relative"
button.addEventListener("click", (e) => {
const target = e.target as HTMLElement
const parentPreBlock = target.closest("pre")
const codeBlock = parentPreBlock.querySelector<HTMLElement>("code")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "微信",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "微信",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
}
})
}
}
function downloadMarkdown() {
const html = document.querySelector("#img-content")
const markdown = turndownService.turndown(html)
+3 -69
View File
@@ -20,20 +20,17 @@ const articleTitle = document.querySelector<HTMLElement>("head title").innerText
export default function zhihu() {
const [cssCode, runCss] = useCssCodeHook("zhihu")
const [closeLoginModal] = useStorage<boolean>("zhihu-closeLoginModal")
const [copyCode] = useStorage<boolean>("zhihu-copyCode")
const [autoOpenCode] = useStorage<boolean>("zhihu-autoOpenCode")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)
const [content, setContent] = useContent()
useEffect(() => {
closeLog ||
console.log("zhihu status", { copyCode, closeLoginModal, autoOpenCode })
copyCode && copyCodeFunc()
closeLog || console.log("zhihu status", { closeLoginModal, autoOpenCode })
closeLoginModal && closeLoginModalFunc()
autoOpenCode && autoOpenCodeFunc()
setIcon(closeLoginModal || copyCode || autoOpenCode)
}, [copyCode, closeLoginModal, autoOpenCode])
setIcon(true)
}, [closeLoginModal, autoOpenCode])
useMessage(async (req, res) => {
if (req.name == "zhihu-isShow") {
@@ -50,69 +47,6 @@ export default function zhihu() {
}
})
// 功能一: 修改复制按钮,支持一键复制
function copyCodeFunc() {
const codes = document.querySelectorAll<HTMLElement>(
".RichContent .highlight"
)
codes.forEach((code) => {
const button = document.createElement("button")
button.innerText = "复制"
button.style.position = "absolute"
button.style.top = "0"
button.style.right = "0"
button.title = "一键复制代码"
button.classList.add("Button")
button.classList.add("VoteButton")
code.appendChild(button)
code.style.position = "relative"
button.addEventListener("click", (e) => {
// 实现复制
const target = e.target as HTMLElement
const parentPreBlock = target.closest(".highlight")
const codeBlock = parentPreBlock.querySelector<HTMLElement>("pre")
navigator.clipboard.writeText(codeBlock.innerText)
setHistory((prevData) =>
prevData
? [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "知乎",
link: location.href,
tags: [],
remark: ""
},
...prevData
]
: [
{
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "知乎",
link: location.href,
tags: [],
remark: ""
}
]
)
target.innerText = "复制成功"
setTimeout(() => {
target.innerText = "复制"
}, 1000)
e.stopPropagation()
e.preventDefault()
})
})
}
// 隐藏登录弹窗
function closeLoginModalFunc() {
addCss(`
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "code-box",
"displayName": "__MSG_extensionName__",
"version": "0.9.0",
"version": "0.9.1",
"description": "__MSG_extensionDescription__",
"author": "027xiguapi. <458813868@qq.com>",
"scripts": {