Skip to content

Commit 5c48085

Browse files
committed
feat: ✨ 支持识别图片链接中的二维码信息
1 parent 2636ba8 commit 5c48085

8 files changed

Lines changed: 527 additions & 38 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.6.0
4+
5+
### Minor Changes
6+
7+
- 支持识别图片链接中的二维码信息
8+
39
## 1.5.0
410

511
### Minor Changes

apps/common/qr-code/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
![](https://raw.githubusercontent.com/xiaohuohumax/userscripts/main/apps/common/qr-code/images/use.gif)
2121

22+
![](https://raw.githubusercontent.com/xiaohuohumax/userscripts/main/apps/common/qr-code/images/selection.gif)
23+
2224
![](https://raw.githubusercontent.com/xiaohuohumax/userscripts/main/apps/common/qr-code/images/encdoe.gif)
2325

2426
## 🚨 免责声明
1.05 MB
Loading

apps/common/qr-code/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "common-qr-code",
33
"type": "module",
4-
"version": "1.5.0",
4+
"version": "1.6.0",
55
"private": true,
66
"description": "图片二维码识别/生成工具(Common QR Code)-- 右键图片,识别二维码并复制到剪贴板。右键文字,生成二维码并展示。",
77
"author": {
@@ -24,6 +24,7 @@
2424
"build": "vite build --emptyOutDir=false"
2525
},
2626
"dependencies": {
27+
"get-urls": "^12.1.0",
2728
"jsqr": "^1.4.0",
2829
"notiflix": "^3.2.8",
2930
"qrcode": "^1.5.4",

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
import type { ImageElement } from './utils'
1+
import type { Target } from './utils'
22
import { GM_download, GM_registerMenuCommand, GM_setClipboard } from '$'
33
import { Notify } from 'notiflix/build/notiflix-notify-aio'
44
import QRCode from 'qrcode'
55
import Swal from 'sweetalert'
66
import { ID, VERSION } from 'virtual:meta'
77

8-
import { decodeQrCode, isUrl } from './utils'
8+
import { decodeQrCode, isUrl, tryGetUrls } from './utils'
99

1010
console.log(`${ID}(v${VERSION})`)
1111

12-
let image: ImageElement | null = null
12+
let target: Target | null = null
1313
let selection: string | null = null
1414

1515
function handleDecodeQrCodeMenuClick() {
16-
if (!image) {
17-
return Notify.warning('未选择图片, 请先右键选择图片')
16+
if (selection !== null) {
17+
const urls = tryGetUrls(selection)
18+
if (urls.length === 0) {
19+
return Notify.warning('选择的文本中未找到有效的链接, 请确认文本是否有效')
20+
}
21+
target = urls[0]
22+
}
23+
24+
if (target === null) {
25+
return Notify.warning('未选择图片或图片链接, 请先右键选择图片或图片链接')
1826
}
1927

20-
decodeQrCode(image).then((results) => {
28+
decodeQrCode(target).then((results) => {
2129
if (results.length === 0) {
2230
return Notify.warning('未识别到二维码, 请确认图片是否有效')
2331
}
@@ -89,7 +97,7 @@ function handleDecodeQrCodeMenuClick() {
8997
}).catch((error) => {
9098
Notify.failure('识别失败, 请检查图片是否有效')
9199
console.error(error)
92-
}).finally(() => (image = null))
100+
}).finally(() => (target = null))
93101
}
94102

95103
async function handleEncodeQrCodeMenuClick() {
@@ -126,7 +134,7 @@ GM_registerMenuCommand('Encode QR Code', handleEncodeQrCodeMenuClick)
126134

127135
document.addEventListener('contextmenu', (event) => {
128136
if (event.target instanceof HTMLImageElement || event.target instanceof HTMLCanvasElement) {
129-
image = event.target
137+
target = event.target
130138
}
131139
})
132140

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { GM_xmlhttpRequest } from '$'
2+
import getUrls from 'get-urls'
23
import jsQR from 'jsqr'
34

4-
export type ImageElement = HTMLImageElement | HTMLCanvasElement
5+
export type Target = HTMLImageElement | HTMLCanvasElement | URL
56

6-
export async function decodeQrCode(element: ImageElement): Promise<string[]> {
7+
export async function decodeQrCode(target: Target): Promise<string[]> {
78
return new Promise((resolve, reject) => {
89
const image = new Image()
910

@@ -43,10 +44,10 @@ export async function decodeQrCode(element: ImageElement): Promise<string[]> {
4344

4445
image.onerror = reject
4546

46-
if (element instanceof HTMLImageElement) { // 静态图片
47+
if (target instanceof HTMLImageElement || target instanceof URL) { // 静态图片 或者 URL 图片
4748
GM_xmlhttpRequest({
4849
method: 'GET',
49-
url: element.src,
50+
url: target instanceof URL ? target.toString() : target.src,
5051
responseType: 'blob',
5152
onload: (response) => {
5253
if (response.status !== 200) {
@@ -58,12 +59,16 @@ export async function decodeQrCode(element: ImageElement): Promise<string[]> {
5859
onerror: reject,
5960
})
6061
}
61-
else if (element instanceof HTMLCanvasElement) { // Canvas 图片
62-
image.src = element.toDataURL()
62+
else if (target instanceof HTMLCanvasElement) { // Canvas 图片
63+
image.src = target.toDataURL()
6364
}
6465
})
6566
}
6667

68+
export function tryGetUrls(text: string): URL[] {
69+
return Array.from(getUrls(text)).map(url => new URL(url))
70+
}
71+
6772
export function isUrl(url: string): boolean {
6873
return /^https?:\/\//.test(url.trimStart())
6974
}

dist/common-qr-code.user.js

Lines changed: 487 additions & 23 deletions
Large diffs are not rendered by default.

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)