3

I have an application that is using instascan to scan QR codes but 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.

Sarah Cox
  • 316
  • 1
  • 11

1 Answers1

0

I figured it out based on this post: Stop alert javascript popup in webbrowser c# control

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);
            }
        });
Sarah Cox
  • 316
  • 1
  • 11