schmich / instascan

HTML5 QR code scanner using your webcam
https://schmich.github.io/instascan/
MIT License
2.97k stars 863 forks source link

Stop instascan alert to phone after QR code scan in android #262

Closed SarahBethCox closed 3 years ago

SarahBethCox commented 3 years ago

My application is working well except for every time you scan a QR code on an android phone an alert pops up with the decoded result. For example, if I generated a QR code with the text "I like bananas" then the alert would say "https://mywebsite.com says I like bananas".

Is there any way to stop this alert from showing? It doesn't happen on my laptop or tablet only my phone. Here is my code if you would like to see it:

<video id="preview" visible="true"></video>
    <script type="text/javascript">
        let scanner = new Instascan.Scanner({ video: document.getElementById('preview') });
        //When a QR code is detected
        scanner.addListener('scan', function (content) {
            //If it is a url
            if (content.match(/^http?:\/\//i)) {
                //Redirect to new page
                window.open(content);
            } else {                    
                var options = {};
                options.url = "CameraList.aspx/GetContent";
                options.type = "POST";
                options.data = JSON.stringify({ content });
                options.dataType = "json";
                options.contentType = "application/json";
                options.success = function (result) { };
                options.error = function (err) { };

                $.ajax(options);
            }
        });
    </script>

Any help is appreciated.

SarahBethCox commented 3 years ago

I figured it out based on this post: https://stackoverflow.com/questions/3848632/stop-alert-javascript-popup-in-webbrowser-c-sharp-control?noredirect=1&lq=1

I wanted to return a string from a method called GetContent and alert that to the user only if the string was not empty:

        //write a new function for alert that expects a boolean value
        var fnAlert = alert;
        alert = function (message, doshow) {
        //only if you pass true show the alert
            if (doshow === true) {
                fnAlert(message);
            }
        }
        let scanner = new Instascan.Scanner({ video: document.getElementById('preview') });
        //When a QR code is detected
        scanner.addListener('scan', function (content) {
            //If it is a url
            if (content.match(/^http?:\/\//i)) {
                //Redirect to new page
                window.open(content);
            } else {                    
                var options = {};
                options.url = "CameraList.aspx/GetContent";
                options.type = "POST";
                options.data = JSON.stringify({ content });
                options.dataType = "json";
                options.contentType = "application/json";
                options.success = function (result) {
                    //If my c# method GetContent returns a value
                    if (result != "") {
                        //then alert the user by passing true
                        alert(result, true);
                    }
                };
                options.error = function (err) { };

                $.ajax(options);
            }
        });