diff --git a/contents/51cto.tsx b/contents/51cto.tsx index 5e08d29..cbf0edd 100644 --- a/contents/51cto.tsx +++ b/contents/51cto.tsx @@ -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("head title").innerText export default function Cto51() { const [cssCode, runCss] = useCssCodeHook("51cto") - const [copyCode] = useStorage("51cto-copyCode") const [closeLoginModal] = useStorage("51cto-closeLoginModal") - const [history, setHistory] = useStorage("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( - ".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("pre") - const codeIndex = - codeBlock.querySelector(".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( - "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(".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 = ` diff --git a/contents/baidu.tsx b/contents/baidu.tsx index 18eb8a8..ae5dc7e 100644 --- a/contents/baidu.tsx +++ b/contents/baidu.tsx @@ -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) => { diff --git a/contents/cnblogs.tsx b/contents/cnblogs.tsx index 54518e2..6812fb2 100644 --- a/contents/cnblogs.tsx +++ b/contents/cnblogs.tsx @@ -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("head title").innerText export default function cnblogs() { const [cssCode, runCss] = useCssCodeHook("cnblogs") - const [copyCode] = useStorage("cnblogs-copyCode") const [history, setHistory] = useStorage("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( - ".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( - ".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("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) diff --git a/contents/copycode.tsx b/contents/copycode.tsx index 6f7c8ec..3acf930 100644 --- a/contents/copycode.tsx +++ b/contents/copycode.tsx @@ -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 = ({ anchor }) => { const [copyCode] = useStorage("config-copyCode", true) + const [history, setHistory] = useStorage("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 = ({ 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 ? ( diff --git a/contents/csdn.tsx b/contents/csdn.tsx index 26d375f..adeeefa 100644 --- a/contents/csdn.tsx +++ b/contents/csdn.tsx @@ -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("head title").innerText const csdn = () => { const [cssCode, runCss] = useCssCodeHook("csdn") const [closeAds] = useStorage("csdn-closeAds") - const [copyCode] = useStorage("csdn-copyCode") const [closeFollow] = useStorage("csdn-closeFollow") const [closeVip] = useStorage("csdn-closeVip") const [autoOpenCode] = useStorage("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(".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 = ` diff --git a/contents/jb51.tsx b/contents/jb51.tsx index 9a3cd89..072ab2e 100644 --- a/contents/jb51.tsx +++ b/contents/jb51.tsx @@ -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("head title").innerText export default function jb51() { const [cssCode, runCss] = useCssCodeHook("jb51") const [closeAds] = useStorage("jb51-closeAds") - const [copyCode] = useStorage("jb51-copyCode") const [history, setHistory] = useStorage("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(".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(".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("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("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(` diff --git a/contents/jianshu.tsx b/contents/jianshu.tsx index 4a29542..04d10f0 100644 --- a/contents/jianshu.tsx +++ b/contents/jianshu.tsx @@ -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("head title").innerText export default function Jianshu() { const [cssCode, runCss] = useCssCodeHook("jianshu") const [closeLoginModal] = useStorage("jianshu-closeLoginModal") - const [copyCode] = useStorage("jianshu-copyCode") const [autoOpenCode] = useStorage("jianshu-autoOpenCode") - const [history, setHistory] = useStorage("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(".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("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( diff --git a/contents/php.tsx b/contents/php.tsx index 9b2c9ec..1da4e52 100644 --- a/contents/php.tsx +++ b/contents/php.tsx @@ -19,20 +19,16 @@ const articleTitle = document.querySelector("head title").innerText export default function Php() { const [cssCode, runCss] = useCssCodeHook("php") - const [copyCode] = useStorage("php-copyCode") const [closeLoginModal] = useStorage("php-closeLoginModal") const [history, setHistory] = useStorage("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( - ".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(".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(".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(".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 = ` diff --git a/contents/weixin.tsx b/contents/weixin.tsx index 38f9499..cd950a9 100644 --- a/contents/weixin.tsx +++ b/contents/weixin.tsx @@ -19,16 +19,12 @@ const articleTitle = document.querySelector("head title").innerText export default function Weixin() { const [cssCode, runCss] = useCssCodeHook("weixin") - const [copyCode] = useStorage("weixin-copyCode") const [history, setHistory] = useStorage("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("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("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) diff --git a/contents/zhihu.tsx b/contents/zhihu.tsx index 2830796..f344e1a 100644 --- a/contents/zhihu.tsx +++ b/contents/zhihu.tsx @@ -20,20 +20,17 @@ const articleTitle = document.querySelector("head title").innerText export default function zhihu() { const [cssCode, runCss] = useCssCodeHook("zhihu") const [closeLoginModal] = useStorage("zhihu-closeLoginModal") - const [copyCode] = useStorage("zhihu-copyCode") const [autoOpenCode] = useStorage("zhihu-autoOpenCode") const [history, setHistory] = useStorage("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( - ".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("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(` diff --git a/package.json b/package.json index 2cde087..4f67cc4 100644 --- a/package.json +++ b/package.json @@ -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": {