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
d33d1c6
commit 6041615
Showing
7 changed files
with
184 additions
and
13 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,43 @@ | ||
import { useStorage } from "@plasmohq/storage/hook" | ||
|
||
export default function Php() { | ||
const [copyCode, setCopyCode] = useStorage("php-copyCode", (v) => | ||
v === undefined ? true : v | ||
) | ||
const [closeLoginModal, setCloseLoginModal] = useStorage( | ||
"php-closeLoginModal", | ||
(v) => (v === undefined ? true : v) | ||
) | ||
|
||
return ( | ||
<fieldset> | ||
<legend>php中文网设置</legend> | ||
<div className="item"> | ||
<span>一键复制代码</span> | ||
<input | ||
type="checkbox" | ||
id="php-copyCode" | ||
name="php-copyCode" | ||
className="codebox-offscreen" | ||
checked={copyCode} | ||
onChange={(e) => setCopyCode(e.target.checked)} | ||
/> | ||
<label className="codebox-switch" htmlFor="php-copyCode"></label> | ||
</div> | ||
<div className="item"> | ||
<span>关闭登录弹窗</span> | ||
<input | ||
type="checkbox" | ||
id="php-closeLoginModal" | ||
name="php-closeLoginModal" | ||
className="codebox-offscreen" | ||
checked={closeLoginModal} | ||
onChange={(e) => setCloseLoginModal(e.target.checked)} | ||
/> | ||
<label | ||
htmlFor="php-closeLoginModal" | ||
className="codebox-switch"></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,119 @@ | ||
import type { PlasmoCSConfig } from "plasmo" | ||
import { useEffect } from "react" | ||
|
||
import { useStorage } from "@plasmohq/storage/hook" | ||
|
||
import { addCss } from "~tools" | ||
|
||
export const config: PlasmoCSConfig = { | ||
matches: ["https://*.php.cn/*"] | ||
} | ||
|
||
window.addEventListener("load", () => { | ||
console.log("php中文网加载完成,执行代码") | ||
}) | ||
|
||
export default function Php() { | ||
|
||
const [copyCode] = useStorage<boolean>("php-copyCode") | ||
const [closeLoginModal] = useStorage<boolean>("php-closeLoginModal") | ||
|
||
useEffect(() => { | ||
console.log("PHP status", { closeLoginModal, copyCode }) | ||
setTimeout(()=>{ | ||
copyCode && copyCodeFunc() | ||
}, 500) | ||
closeLoginModal && closeLoginModalFunc() | ||
}, [copyCode, closeLoginModal]) | ||
|
||
/* 未登录复制代码 */ | ||
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) | ||
|
||
target.innerText = "复制成功" | ||
setTimeout(() => { | ||
target.innerText = "复制" | ||
}, 1000) | ||
e.stopPropagation() | ||
e.preventDefault() | ||
}) | ||
}) | ||
} else { | ||
const codes = document.querySelectorAll<HTMLElement>(".nphpQianBox .code") | ||
|
||
console.log(codes) | ||
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) | ||
target.innerText = "复制成功" | ||
setTimeout(() => { | ||
target.innerText = "复制" | ||
}, 1000) | ||
e.stopPropagation() | ||
e.preventDefault() | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
// 关闭登录弹框 | ||
function closeLoginModalFunc() { | ||
const css = ` | ||
.layui-layer-shade, | ||
.layui-layer-iframe { | ||
display:none !important; | ||
}` | ||
addCss(css) | ||
} | ||
|
||
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