File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# common-qr-code
22
3+ ## 1.3.0
4+
5+ ### Minor Changes
6+
7+ - 支持从 Canvas 中识别二维码
8+
39## 1.2.0
410
511### Minor Changes
Original file line number Diff line number Diff line change 11{
22 "name" : " common-qr-code" ,
33 "type" : " module" ,
4- "version" : " 1.2 .0" ,
4+ "version" : " 1.3 .0" ,
55 "private" : true ,
66 "description" : " 图片二维码识别(Common QR Code)-- 右键图片,识别二维码并复制到剪贴板。" ,
77 "author" : {
Original file line number Diff line number Diff line change 1+ import type { ImageElement } from './utils'
12import { GM_registerMenuCommand , GM_setClipboard } from '$'
23import { Notify } from 'notiflix/build/notiflix-notify-aio'
34import Swal from 'sweetalert'
@@ -6,13 +7,14 @@ import { decodeQrCode } from './utils'
67
78console . log ( `${ ID } (v${ VERSION } )` )
89
9- let image : HTMLImageElement | null = null
10+ let image : ImageElement | null = null
1011
1112GM_registerMenuCommand ( 'Decode QR Code' , ( ) => {
1213 if ( ! image ) {
1314 return Notify . warning ( '未选择图片, 请先右键选择图片' )
1415 }
15- decodeQrCode ( image . src ) . then ( ( results ) => {
16+
17+ decodeQrCode ( image ) . then ( ( results ) => {
1618 if ( results . length === 0 ) {
1719 return Notify . warning ( '未识别到二维码, 请确认图片是否有效' )
1820 }
@@ -68,7 +70,7 @@ GM_registerMenuCommand('Decode QR Code', () => {
6870} )
6971
7072document . addEventListener ( 'contextmenu' , ( event ) => {
71- if ( event . target instanceof HTMLImageElement ) {
73+ if ( event . target instanceof HTMLImageElement || event . target instanceof HTMLCanvasElement ) {
7274 image = event . target
7375 }
7476} )
Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ import { GM_xmlhttpRequest } from '$'
22
33import jsQR from 'jsqr'
44
5- export async function decodeQrCode ( url : string ) : Promise < string [ ] > {
5+ export type ImageElement = HTMLImageElement | HTMLCanvasElement
6+
7+ export async function decodeQrCode ( element : ImageElement ) : Promise < string [ ] > {
68 return new Promise ( ( resolve , reject ) => {
79 const image = new Image ( )
810
@@ -42,18 +44,23 @@ export async function decodeQrCode(url: string): Promise<string[]> {
4244
4345 image . onerror = reject
4446
45- GM_xmlhttpRequest ( {
46- method : 'GET' ,
47- url,
48- responseType : 'blob' ,
49- onload : ( response ) => {
50- if ( response . status !== 200 ) {
51- reject ( new Error ( `Failed to load image: ${ response . status } ${ response . statusText } ` ) )
52- return
53- }
54- image . src = URL . createObjectURL ( response . response )
55- } ,
56- onerror : reject ,
57- } )
47+ if ( element instanceof HTMLImageElement ) { // 静态图片
48+ GM_xmlhttpRequest ( {
49+ method : 'GET' ,
50+ url : element . src ,
51+ responseType : 'blob' ,
52+ onload : ( response ) => {
53+ if ( response . status !== 200 ) {
54+ reject ( new Error ( `Failed to load image: ${ response . status } ${ response . statusText } ` ) )
55+ return
56+ }
57+ image . src = URL . createObjectURL ( response . response )
58+ } ,
59+ onerror : reject ,
60+ } )
61+ }
62+ else if ( element instanceof HTMLCanvasElement ) { // Canvas 图片
63+ image . src = element . toDataURL ( )
64+ }
5865 } )
5966}
You can’t perform that action at this time.
0 commit comments