Documentation :: Demo :: Tech Info
Events Log:
// This or something like it is the most important.
// without it, you won't be able to detect errors

WebBarcode.onError( AttachLog("ERROR") );

// Connect listeners to other events
WebBarcode.onAccessoryConnect( AttachLog("ACC_CONN") );
WebBarcode.onBarcodeScan( AttachLog("SCAN") );
WebBarcode.Linea.onSwipe( AttachLog("SWIPE") );

// Log version when started
WebBarcode.getVersion( AttachLog("VERSION") );

function emitTone(){
    WebBarcode.Linea.emitTones([
        { tone:2000, duration:400 },
        { tone:5000, duration:250 }
    ]);
}

function vibrateDevice(){
    WebBarcode.vibrateDevice();
}

function playSound (select){
    if (select.value){
        WebBarcode.playSoundID(parseInt(select.value));
    }
    select.value = ""; // reset back to label
}

function pickAccessory(){
    WebBarcode.pickAccessory();
}

function getAccessories(){
    var log = AttachLog("ACC");
    WebBarcode.getConnectedAccessories().then(function(msg){
        log("Found " + msg.accessories.length + " accessories");
        msg.accessories.forEach(log);
        if (msg.accessories.length && msg.accessories[0].serialNumber){
            document.getElementById("printerSerial").value = msg.accessories[0].serialNumber;
        }
    });
}

function printToAccessory(){
    var log = AttachLog("PRINT");
    var options = { data: document.getElementById("printData").value },
        serial = document.getElementById("printerSerial").value,
        tcpaddr = document.getElementById("printerAddress").value;
    if (serial) {
        options.serial = serial;
    } else if (tcpaddr){
        options.networkAddress = tcpaddr;
    }
    WebBarcode.Zebra.print(options).then(function(result) {
        log(result)
    });
}

function AttachLog(type){
    return function(data){
        time = new Date();
        Log(time.toTimeString() + " : " + type + " <pre>" + JSON.stringify(data, null, '  ') + '</pre>');
    };
}

function Log(msg){
    var node = document.createElement('li'),
        container = document.querySelector('.log');
    node.innerHTML = msg;
    container.insertBefore(node, container.firstChild);
}

function reloadPage(){
    window.location.reload();
}

// log synthetic scan events
document.addEventListener('BarcodeScanned', function(ev){
    var scan = ev.data;
    Log("Scan DomEvent: " + scan.type + " : " + scan.value);
}, false);


function displayAlert(){
    alert("This test alert");
}
function displayPrompt(){
    var answer = prompt("What's the answer?", "42");
    Log("Prompt test returned " + answer);
}
function displayConfirm(){
    var result = window.confirm("true or false?");
    Log("Confirm test returned " + result);
}

// log attempts to submit our test form
var testInput = document.querySelector('#input-test input');
document.getElementById("input-test").addEventListener('submit', function(ev) {
    ev.preventDefault();
    Log("Form submission with value: " + testInput.value);
});