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
cbe0411
commit b398f6e
Showing
9 changed files
with
177 additions
and
10 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,61 @@ | ||
import { useStorage } from "@plasmohq/storage/hook" | ||
|
||
export default function Jianshu() { | ||
const [copyCode, setCopyCode] = useStorage("jianshu-copyCode", (v) => | ||
v === undefined ? true : v | ||
) | ||
const [closeLoginModal, setCloseLoginModal] = useStorage( | ||
"jianshu-closeLoginModal", | ||
(v) => (v === undefined ? true : v) | ||
) | ||
const [autoOpenCode, setAutoOpenCode] = useStorage( | ||
"jianshu-autoOpenCode", | ||
(v) => (v === undefined ? true : v) | ||
) | ||
|
||
return ( | ||
<fieldset> | ||
<legend>简书设置</legend> | ||
<div className="item"> | ||
<span>一键复制代码</span> | ||
<input | ||
type="checkbox" | ||
id="jianshu-copyCode" | ||
name="jianshu-copyCode" | ||
className="codebox-offscreen" | ||
checked={copyCode} | ||
onChange={(e) => setCopyCode(e.target.checked)} | ||
/> | ||
<label className="codebox-switch" htmlFor="jianshu-copyCode"></label> | ||
</div> | ||
<div className="item"> | ||
<span>关闭登录弹窗</span> | ||
<input | ||
type="checkbox" | ||
id="jianshu-closeLoginModal" | ||
name="jianshu-closeLoginModal" | ||
className="codebox-offscreen" | ||
checked={closeLoginModal} | ||
onChange={(e) => setCloseLoginModal(e.target.checked)} | ||
/> | ||
<label | ||
className="codebox-switch" | ||
htmlFor="jianshu-closeLoginModal"></label> | ||
</div> | ||
<div className="item"> | ||
<span>自动展开全文</span> | ||
<input | ||
type="checkbox" | ||
id="jianshu-autoOpenCode" | ||
name="jianshu-autoOpenCode" | ||
className="codebox-offscreen" | ||
checked={autoOpenCode} | ||
onChange={(e) => setAutoOpenCode(e.target.checked)} | ||
/> | ||
<label | ||
className="codebox-switch" | ||
htmlFor="jianshu-autoOpenCode"></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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type { PlasmoCSConfig } from "plasmo" | ||
import { useEffect, useRef } from "react" | ||
|
||
import { useStorage } from "@plasmohq/storage/hook" | ||
|
||
import { addCss } from "~tools" | ||
|
||
export const config: PlasmoCSConfig = { | ||
matches: ["https://*.jianshu.com/*"] | ||
} | ||
|
||
window.addEventListener("load", () => { | ||
console.log("简书加载完成,执行代码") | ||
}) | ||
|
||
export default function Jianshu() { | ||
const [closeLoginModal] = useStorage<boolean>("jianshu-closeLoginModal") | ||
const [copyCode] = useStorage<boolean>("jianshu-copyCode") | ||
const [autoOpenCode] = useStorage<boolean>("jianshu-autoOpenCode") | ||
|
||
useEffect(() => { | ||
console.log("jianshu status", { copyCode, closeLoginModal, autoOpenCode }) | ||
copyCode && copyCodeFunc() | ||
closeLoginModal && closeLoginModalFunc() | ||
autoOpenCode && autoOpenCodeFunc() | ||
}, [copyCode, closeLoginModal, autoOpenCode]) | ||
|
||
// 一键复制 | ||
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) | ||
|
||
target.innerText = "复制成功" | ||
setTimeout(() => { | ||
target.innerText = "复制" | ||
}, 1000) | ||
e.stopPropagation() | ||
e.preventDefault() | ||
}) | ||
}) | ||
} | ||
|
||
// 隐藏登录弹窗 | ||
function closeLoginModalFunc() { | ||
const modal = document.querySelector(".open-app-modal") | ||
const dialog = modal.closest("div[class^='dialog-']") as HTMLElement | ||
const className = dialog.className | ||
addCss(`.${className} { display:none !important; }`) | ||
} | ||
|
||
// 自动展开全文 | ||
function autoOpenCodeFunc() { | ||
const tips = document.querySelector(".collapse-tips") | ||
if (tips) { | ||
const css = ` | ||
.collapse-free-content{ | ||
height: auto !important; | ||
} | ||
.collapse-tips { | ||
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.