forked from 027xiguapi/code-box
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65f0ba0
commit 6a36c51
Showing
8 changed files
with
125 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useStorage } from "@plasmohq/storage/hook" | ||
|
||
export default function Cnblogs() { | ||
const [copyCode, setCopyCode] = useStorage("cnblogs-copyCode", (v) => | ||
v === undefined ? true : v | ||
) | ||
|
||
return ( | ||
<fieldset> | ||
<legend>博客园设置</legend> | ||
<div> | ||
<input | ||
type="checkbox" | ||
id="cnblogs-copyCode" | ||
name="cnblogs-copyCode" | ||
checked={copyCode} | ||
onChange={(e) => setCopyCode(e.target.checked)} | ||
/> | ||
<label htmlFor="cnblogs-copyCode">一键复制代码</label> | ||
</div> | ||
</fieldset> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { PlasmoCSConfig } from "plasmo" | ||
import { useEffect, useRef } from "react" | ||
|
||
import { useStorage } from "@plasmohq/storage/hook" | ||
|
||
export const config: PlasmoCSConfig = { | ||
matches: ["https://*.cnblogs.com/*"], | ||
all_frames: true | ||
} | ||
|
||
window.addEventListener("load", () => { | ||
console.log("博客园加载完成,执行代码") | ||
}) | ||
|
||
export default function cnblogs() { | ||
const [copyCode] = useStorage<boolean>("cnblogs-copyCode") | ||
|
||
useEffect(() => { | ||
console.log("cnblogs copyCode", copyCode) | ||
copyCode && copyCodeFunc() | ||
}, [copyCode]) | ||
|
||
function copyCodeFunc() { | ||
// 功能一: 修改复制按钮,支持一键复制 | ||
const toolbars = document.querySelectorAll<HTMLElement>( | ||
".cnblogs_code_toolbar" | ||
) | ||
|
||
toolbars.forEach((toolbar) => { | ||
const button = document.createElement("button") | ||
button.innerHTML = "复制" | ||
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) | ||
|
||
target.innerText = "复制成功" | ||
setTimeout(() => { | ||
target.innerText = "复制" | ||
}, 1000) | ||
e.stopPropagation() | ||
e.preventDefault() | ||
}) | ||
}) | ||
} | ||
|
||
return <div style={{ display: "none" }}></div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters