Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Scan QR code from clipboard #3

Merged
merged 3 commits into from Sep 15, 2021

Conversation

raffifu
Copy link
Contributor

@raffifu raffifu commented Sep 12, 2021

this PR close #2

The idea is to use paste event and then grab the files object from event.clipboardData

[praesentia.js]
....
const target = document.querySelector('body');

target.addEventListener('paste', (event) => {
    let paste = event.clipboardData || window.clipboardData;

    if(paste.files.length > 0) {
        html5QrcodeScanner.html5Qrcode.scanFile(paste.files[0], true)
        .then(decodedText => {
            processingDecodedText(decodedText)
        })
        .catch(err => {
            swal.fire('Error', 'Cannot read QR Code, Please make sure paste only the QR Code', 'error');
        });
    }
});
....

When the file object exist, call method scanFile from class html5Qrcode. it will return decodedText if successful and when fail will call swal.fire

CAUTION

html5Qrcode instance is private field from html5QrcodeScanner see this file on line (87). the problem can be solved by creating html5Qrcode directly (use Pro Mode as it says on the documentation)

For demo visit this link

@ttycelery ttycelery self-requested a review September 14, 2021 00:39
@ttycelery
Copy link
Owner

ttycelery commented Sep 14, 2021

Thank you for your PR. I really appreciate that.

Apologies, I messed up with the review. Haven't reviewed a pull request. I believe this is my first time. This PR is ready to merge, but I am still unsure about whether or not to use jQuery. I think it should be consistent. What do you think?

@raffifu
Copy link
Contributor Author

raffifu commented Sep 14, 2021

I think so, this project can be developed further. So, the code must be consistent. I will fix that in the next commit

@ttycelery
Copy link
Owner

Thanks. I will merge after that commit.

@ttycelery ttycelery added the enhancement New feature or request label Sep 14, 2021
@raffifu
Copy link
Contributor Author

raffifu commented Sep 15, 2021

74a669f Now this PR follow jQuery format

[praesentia.js]
....
$('body').on('paste', event => {
    let paste = event.originalEvent.clipboardData || window.clipboardData;

    if(paste.files.length > 0) {
        html5QrcodeScanner.html5Qrcode.scanFile(paste.files[0], true)
        .then(decodedText => {
            processDecodedText(decodedText)
        })
        .catch(err => {
            swal.fire('Error', 'Cannot read QR code. Please make sure to only paste a QR code image.', 'error');
        });
    }

    event.preventDefault()
});
....

I also added event.preventDefault() as said on this page

@ttycelery
Copy link
Owner

I have just tested it and it worked really well just as expected. Before merging, let me just add a little label to the interface, so users can notice this feature.

@ttycelery
Copy link
Owner

Fixes: #2

@ttycelery ttycelery merged commit c73cf2a into ttycelery:main Sep 15, 2021
@ttycelery
Copy link
Owner

Thanks, @raffifu.

ttycelery added a commit that referenced this pull request Nov 2, 2023
Add scan QR code from clipboard feature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scan QR code from clipboard
2 participants