Skip to content

Commit

Permalink
增加微信一键复制代码
Browse files Browse the repository at this point in the history
  • Loading branch information
027xiguapi committed Sep 4, 2024
1 parent 8635835 commit 81bce11
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## code-box
## codebox-一键复制代码/下载文章

## 代码

https://github.com/027xiguapi/code-box

## 介绍

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

## 安装

Expand Down Expand Up @@ -59,11 +59,11 @@ https://github.com/027xiguapi/code-box

## 功能

![img-1](https://raw.githubusercontent.com/027xiguapi/code-box/main/public/option.jpg)
![img-1](https://raw.githubusercontent.com/027xiguapi/code-box/main/public/config.jpg)

### 自定义

1. 插入自定义样式(css代码)
- 插入自定义样式(css代码)

### CSDN

Expand Down
19 changes: 18 additions & 1 deletion component/weixin.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { DownloadOutlined } from "@ant-design/icons"

import { sendToContentScript } from "@plasmohq/messaging"
import { useStorage } from "@plasmohq/storage/dist/hook"

import { i18n } from "~tools"

export default function Weixin() {
const [copyCode, setCopyCode] = useStorage("weixin-copyCode", (v) =>
v === undefined ? true : v
)

function downloadMarkdown() {
sendToContentScript({
name: "weixin-downloadMarkdown"
Expand All @@ -19,7 +24,19 @@ export default function Weixin() {

return (
<fieldset>
<legend>{i18n("oschinaConfig")}</legend>
<legend>{i18n("weixinConfig")}</legend>
<div className="item">
<span>{i18n("copyCode")}</span>
<input
type="checkbox"
id="weixin-copyCode"
name="weixin-copyCode"
className="codebox-offscreen"
checked={copyCode}
onChange={(e) => setCopyCode(e.target.checked)}
/>
<label className="codebox-switch" htmlFor="weixin-copyCode"></label>
</div>
<div className="item download" onClick={downloadMarkdown}>
{i18n("downloadMarkdown")}
<DownloadOutlined style={{ color: "#52c41a", fontSize: "16px" }} />
Expand Down
4 changes: 2 additions & 2 deletions contents/51cto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default function Cto51() {
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "CSDN",
from: "51CTO",
link: location.href,
tags: [],
remark: ""
Expand All @@ -114,7 +114,7 @@ export default function Cto51() {
id: uuidv4(),
value: codeBlock.innerText,
createdAt: new Date(),
from: "CSDN",
from: "51CTO",
link: location.href,
tags: [],
remark: ""
Expand Down
79 changes: 77 additions & 2 deletions contents/weixin.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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/dist/hook"

import { Readability } from "~node_modules/@mozilla/readability"
import {
Expand All @@ -25,9 +27,15 @@ const authorLink = getMetaContentByProperty("article:author")
const domain = window.location.hostname

export default function Weixin() {
const [copyCode] = useStorage<boolean>("51cto-copyCode")
const [history, setHistory] = useStorage<any[]>("codebox-history")
const [closeLog] = useStorage("config-closeLog", true)

useEffect(() => {
setIcon(true)
}, [])
closeLog || console.log("weixin status", { copyCode })
copyCode && copyCodeFunc()
setIcon(copyCode)
}, [copyCode])

useMessage(async (req, res) => {
if (req.name == "weixin-isShow") {
Expand All @@ -41,6 +49,73 @@ export default function Weixin() {
}
})

function copyCodeFunc() {
const content_views = document.querySelector("#js_content")

// 功能一: 修改复制按钮,支持一键复制
const codes = content_views.querySelectorAll<HTMLElement>("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<HTMLElement>("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)
Expand Down
3 changes: 3 additions & 0 deletions locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
"downloadHtml": {
"message": "Download HTML"
},
"copyCode": {
"message": "Copy Code"
},
"juejinConfig": {
"message": "Juejin Config"
},
Expand Down
5 changes: 4 additions & 1 deletion locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"message": "codebox-一键复制代码/下载文章"
},
"extensionDescription": {
"message": "实现一键下载文章成html或markdown文件;实现无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗;页面自定义样式."
"message": "实现一键下载文章成html或markdown文件;无需登录一键复制代码;支持选中代码;或者代码右上角按钮的一键复制;解除关注博主即可阅读全文提示;去除登录弹窗;去除跳转APP弹窗;页面自定义样式."
},
"popupDescription": {
"message": "免登录一键复制代码"
Expand Down Expand Up @@ -131,6 +131,9 @@
"downloadHtml": {
"message": "下载 HTML"
},
"copyCode": {
"message": "一键复制代码"
},
"juejinConfig": {
"message": "掘金设置"
},
Expand Down
22 changes: 11 additions & 11 deletions options/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,46 @@ export default function IndexOptions() {
},
{
id: "5",
value: "weixin",
label: "微信",
isShow: true
},
{
id: "6",
value: "jb51",
label: "脚本之家",
isShow: true
},
{
id: "6",
id: "7",
value: "cnblogs",
label: "博客园",
isShow: true
},
{
id: "7",
id: "8",
value: "51cto",
label: "51CTO",
isShow: true
},
{
id: "8",
id: "9",
value: "juejin",
label: "掘金",
isShow: true
},
{
id: "9",
id: "10",
value: "oschina",
label: "oschina",
isShow: true
},
{
id: "10",
id: "11",
value: "segmentfault",
label: "思否",
isShow: true
},
{
id: "11",
value: "weixin",
label: "微信",
isShow: true
},
{
id: "12",
value: "custom",
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "code-box",
"displayName": "__MSG_extensionName__",
"version": "0.5.10",
"version": "0.5.11",
"description": "__MSG_extensionDescription__",
"author": "027xiguapi. <458813868@qq.com>",
"scripts": {
"dev": "plasmo dev",
"build": "rimraf build && plasmo build",
"build:firefox": "rimraf build && plasmo build --target=firefox-mv2",
"build": "plasmo build",
"build:firefox": "plasmo build --target=firefox-mv2",
"build:all": "rimraf build && plasmo build && plasmo build --target=firefox-mv2",
"package": "plasmo package",
"package:firefox": "plasmo package --target=firefox-mv2",
"package:all": "plasmo package && plasmo package --target=firefox-mv2",
"package:all": "npm run build:all && plasmo package && plasmo package --target=firefox-mv2",
"clean": "rimraf build"
},
"repository": {
Expand Down
Binary file added public/128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/1400x560.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed public/1400x560.png
Binary file not shown.
Binary file modified public/440x280.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://github.com/027xiguapi/code-box

## 描述

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

### CSDN
1. 一键下载文章html或markdown文件
Expand Down
2 changes: 1 addition & 1 deletion public/app.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
描述

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

注意:由于审核原因,新版一般会晚几天上架,请耐心等候。

Expand Down
Binary file added public/config.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed public/option.jpg
Binary file not shown.

0 comments on commit 81bce11

Please sign in to comment.