@@ -99,6 +99,11 @@ <h1>Online QR Code Reader</h1>
9999 </ div >
100100 < div class ="error " id ="file-error "> </ div >
101101 </ div >
102+ < div class ="form-group ">
103+ < label for ="paste-qr "> Paste QR Code:</ label >
104+ < button type ="button " class ="btn " id ="paste-btn "> Paste from Clipboard</ button >
105+ < div class ="error " id ="paste-error "> </ div >
106+ </ div >
102107 < div class ="form-group ">
103108 < div class ="decoded-info " id ="decoded-info "> </ div >
104109 < button type ="button " class ="copy-btn " id ="copy-btn "> Copy</ button >
@@ -206,6 +211,44 @@ <h1>Online QR Code Reader</h1>
206211 qrImage . src = '#' ;
207212 $ ( '#qr-file' ) . val ( '' ) ;
208213 } ) ;
214+
215+ // Paste from clipboard
216+ const pasteBtn = document . getElementById ( 'paste-btn' ) ;
217+ const pasteError = document . getElementById ( 'paste-error' ) ;
218+
219+ pasteBtn . addEventListener ( 'click' , async function ( ) {
220+ try {
221+ const clipboardItems = await navigator . clipboard . read ( ) ;
222+ for ( const clipboardItem of clipboardItems ) {
223+ for ( const type of clipboardItem . types ) {
224+ if ( type . startsWith ( 'image/' ) ) {
225+ const blob = await clipboardItem . getType ( type ) ;
226+ const imageUrl = URL . createObjectURL ( blob ) ;
227+ qrImage . src = imageUrl ;
228+ qrImage . onload = function ( ) {
229+ codeReader . decodeFromImage ( qrImage ) . then ( code => {
230+ if ( code ) {
231+ decodedInfo . textContent = code . text ;
232+ copyBtn . disabled = false ;
233+ pasteError . textContent = '' ;
234+ } else {
235+ pasteError . textContent = 'Error decoding QR code' ;
236+ }
237+ } ) . catch ( err => {
238+ console . error ( err ) ;
239+ pasteError . textContent = 'Error decoding QR code' ;
240+ } ) ;
241+ } ;
242+ return ;
243+ }
244+ }
245+ }
246+ pasteError . textContent = 'No image found in clipboard' ;
247+ } catch ( err ) {
248+ console . error ( err ) ;
249+ pasteError . textContent = 'Error accessing clipboard' ;
250+ }
251+ } ) ;
209252 } ) ;
210253 </ script >
211254</ body >
0 commit comments