Skip to content

Commit

Permalink
php中文网基本功能完成
Browse files Browse the repository at this point in the history
  • Loading branch information
027xiguapi committed Jul 24, 2024
1 parent d33d1c6 commit 6041615
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 13 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://github.com/027xiguapi/code-box

## 介绍

本浏览器插件可以用于CSDN/知乎/脚本之家/博客园等网站,实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗.
本浏览器插件可以用于CSDN/知乎/脚本之家/博客园/博客园/51CTO博客/php中文网等网站,实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗.

## 安装

Expand All @@ -25,10 +25,9 @@ https://github.com/027xiguapi/code-box
### 安装方式二:直接安装

1. 安装地址:
- [前往 Chrome 扩展商店](https://chrome.google.com/webstore/detail/acnnhjllgegbndgknlliobjlekgilbdf)
- [前往 火狐 扩展商店](https://addons.mozilla.org/zh-CN/firefox/addon/code-box/)
- [前往 Edge 扩展商店](https://microsoftedge.microsoft.com/addons/detail/code-box/cfpdbfmncaampihkmejogihjkenkonbn)
- [前往 Chrome 扩展商店](#)


### 安装方式三:源码安装

Expand Down Expand Up @@ -80,7 +79,7 @@ https://github.com/027xiguapi/code-box
1. 打开任意一个`脚本之家`博客即可开始复制代码
2. 未登录`脚本之家`状态下,支持选中代码
3. 屏蔽广告
4. 移动端一键复制
4. 移动端未登录`脚本之家`状态下,代码右上角按钮一键复制

### 博客园

Expand All @@ -91,11 +90,15 @@ https://github.com/027xiguapi/code-box
1. 未登录`51CTO博客`状态下,支持选中代码
2. 未登录`51CTO博客`状态下,代码右上角按钮一键复制
3. 未登录`51CTO博客`状态下,不会再出现强制登录弹窗

## 计划
4. 移动端未登录`51CTO博客`状态下,代码右上角按钮一键复制

### php中文网

1. 未登录`php中文网`状态下,支持选中代码
2. 未登录`php中文网`状态下,代码右上角按钮一键复制
3. 未登录`php中文网`状态下,不会再出现强制登录弹窗
4. 未登录`php中文网`状态下,移动端代码右上角按钮一键复制

## 参考

1. [copy-csdn](https://github.com/openHacking/copy-csdn)
Expand Down
43 changes: 43 additions & 0 deletions component/php.tsx
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>
)
}
2 changes: 2 additions & 0 deletions component/sortableItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Jianshu from "~component/jianshu"
import Jb51 from "~component/jb51"
import Cnblogs from "~component/cnblogs"
import Cto51 from "~component/51cto"
import Php from "~component/php"
import Custom from "~component/custom"

import "./sortableItem.css"
Expand All @@ -19,6 +20,7 @@ const itemMap = {
jb51: <Jb51 />,
cnblogs: <Cnblogs />,
"51cto": <Cto51 />,
php: <Php />,
custom: <Custom />
}

Expand Down
8 changes: 3 additions & 5 deletions contents/51cto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const config: PlasmoCSConfig = {
}

window.addEventListener("load", () => {
console.log("脚本之家加载完成,执行代码", document.querySelectorAll<HTMLElement>(".hljs-cto .operation_box .copy_btn"))
console.log("51CTO加载完成,执行代码")
})

export default function Cto51() {
Expand Down Expand Up @@ -76,7 +76,6 @@ export default function Cto51() {
} else {
const codes = document.querySelectorAll<HTMLElement>("article .has-pre-numbering")


codes.forEach((code) => {
const codeBlock = code.closest("div")
const button = document.createElement("button")
Expand All @@ -88,7 +87,6 @@ export default function Cto51() {
button.title = "一键复制代码"
button.classList.add("Button")
button.classList.add("VoteButton")

codeBlock.appendChild(button)
codeBlock.style.position = "relative"

Expand All @@ -110,7 +108,7 @@ export default function Cto51() {
}
}

// 关闭广告
// 关闭登录弹框
function closeLoginModalFunc() {
const css = `
#login_iframe_mask {
Expand All @@ -120,4 +118,4 @@ export default function Cto51() {
}

return <div style={{ display: "none" }}></div>
}
}
119 changes: 119 additions & 0 deletions contents/php.tsx
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>
}
6 changes: 6 additions & 0 deletions options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export default function IndexOptions() {
},
{
id: "7",
value: "php",
label: "php中文网",
isShow: true,
},
{
id: "8",
value: "custom",
label: "自定义",
isShow: true,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "code-box",
"displayName": "codebox-免登录一键复制代码(CSDN/知乎/脚本之家/博客园)",
"version": "0.2.2",
"description": "本浏览器插件可以用于CSDN/知乎/脚本之家/博客园等网站,实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗;页面自定义样式.",
"version": "0.2.3",
"description": "本浏览器插件可以用于CSDN/知乎/脚本之家/博客园/51CTO博客/php中文等网站,实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗;页面自定义样式.",
"author": "027xiguapi. <458813868@qq.com>",
"scripts": {
"dev": "plasmo dev",
Expand Down

0 comments on commit 6041615

Please sign in to comment.