Skip to content

Commit 62b2adf

Browse files
committed
feat: ✨ 新增文字生成二维码功能
1 parent fb36947 commit 62b2adf

8 files changed

Lines changed: 171 additions & 10 deletions

File tree

apps/common/qr-code/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# common-qr-code
22

3+
## 1.5.0
4+
5+
### Minor Changes
6+
7+
- 新增文字生成二维码功能
8+
39
## 1.4.0
410

511
### Minor Changes

apps/common/qr-code/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# 图片二维码识别(Common QR Code)
1+
# 图片二维码识别/生成工具(Common QR Code)
22

3-
**右键图片,识别二维码并复制到剪贴板。**
3+
**右键图片,识别二维码并复制到剪贴板。右键文字,生成二维码并展示。**
44

55
## 📋 更新日志
66

@@ -9,3 +9,5 @@
99
## 📖 使用方式
1010

1111
![](https://raw.githubusercontent.com/xiaohuohumax/userscripts/main/apps/common/qr-code/images/use.gif)
12+
13+
![](https://raw.githubusercontent.com/xiaohuohumax/userscripts/main/apps/common/qr-code/images/encdoe.gif)
1.23 MB
Loading

apps/common/qr-code/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "common-qr-code",
33
"type": "module",
4-
"version": "1.4.0",
4+
"version": "1.5.0",
55
"private": true,
6-
"description": "图片二维码识别(Common QR Code)-- 右键图片,识别二维码并复制到剪贴板。",
6+
"description": "图片二维码识别/生成工具(Common QR Code)-- 右键图片,识别二维码并复制到剪贴板。右键文字,生成二维码并展示",
77
"author": {
88
"name": "xiaohuohumax",
99
"url": "https://github.com/xiaohuohumax"
@@ -26,9 +26,11 @@
2626
"dependencies": {
2727
"jsqr": "^1.4.0",
2828
"notiflix": "^3.2.8",
29+
"qrcode": "^1.5.4",
2930
"sweetalert": "^2.1.2"
3031
},
3132
"devDependencies": {
33+
"@types/qrcode": "^1.5.6",
3234
"vite-plugin-meta": "workspace:^"
3335
}
3436
}

apps/common/qr-code/src/index.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import type { ImageElement } from './utils'
2-
import { GM_registerMenuCommand, GM_setClipboard } from '$'
2+
import { GM_download, GM_registerMenuCommand, GM_setClipboard } from '$'
33
import { Notify } from 'notiflix/build/notiflix-notify-aio'
4+
import QRCode from 'qrcode'
45
import Swal from 'sweetalert'
56
import { ID, VERSION } from 'virtual:meta'
7+
68
import { decodeQrCode, isUrl } from './utils'
79

810
console.log(`${ID}(v${VERSION})`)
911

1012
let image: ImageElement | null = null
13+
let selection: string | null = null
1114

12-
GM_registerMenuCommand('Decode QR Code', () => {
15+
function handleDecodeQrCodeMenuClick() {
1316
if (!image) {
1417
return Notify.warning('未选择图片, 请先右键选择图片')
1518
}
@@ -87,10 +90,46 @@ GM_registerMenuCommand('Decode QR Code', () => {
8790
Notify.failure('识别失败, 请检查图片是否有效')
8891
console.error(error)
8992
}).finally(() => (image = null))
90-
})
93+
}
94+
95+
async function handleEncodeQrCodeMenuClick() {
96+
if (selection === null) {
97+
return Notify.warning('未选择文字, 请先右键选择文字')
98+
}
99+
100+
const dataUrl = await QRCode.toDataURL(selection)
101+
const element = document.createElement('img')
102+
element.src = dataUrl
103+
element.style.margin = '0 auto'
104+
105+
Swal({
106+
icon: 'success',
107+
title: '生成二维码成功',
108+
content: {
109+
element,
110+
},
111+
buttons: {
112+
confirm: {
113+
text: '保存到本地',
114+
value: 'save',
115+
},
116+
},
117+
}).then((result) => {
118+
if (result === 'save') {
119+
GM_download({ name: 'qrcode.png', url: dataUrl, saveAs: true })
120+
}
121+
}).finally(() => (selection = null))
122+
}
123+
124+
GM_registerMenuCommand('Decode QR Code', handleDecodeQrCodeMenuClick)
125+
GM_registerMenuCommand('Encode QR Code', handleEncodeQrCodeMenuClick)
91126

92127
document.addEventListener('contextmenu', (event) => {
93128
if (event.target instanceof HTMLImageElement || event.target instanceof HTMLCanvasElement) {
94129
image = event.target
95130
}
96131
})
132+
133+
document.addEventListener('selectionchange', () => {
134+
selection = document.getSelection()?.toString() || null
135+
})

apps/common/qr-code/src/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { GM_xmlhttpRequest } from '$'
2-
32
import jsQR from 'jsqr'
43

54
export type ImageElement = HTMLImageElement | HTMLCanvasElement

apps/common/qr-code/vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export default defineConfig({
1717
},
1818
},
1919
userscript: {
20-
'name': '图片二维码识别(Common QR Code)',
21-
'description': '右键图片,识别二维码并复制到剪贴板。',
20+
'name': '图片二维码识别/生成工具(Common QR Code)',
21+
'description': '右键图片,识别二维码并复制到剪贴板。右键文字,生成二维码并展示。',
2222
'icon': 'https://raw.githubusercontent.com/xiaohuohumax/logo/refs/heads/main/logos/logo.svg',
2323
'namespace': 'xiaohuohumax/userscripts/common-qr-code',
2424
'license': 'MIT',

0 commit comments

Comments
 (0)