const sendBtn       = document.getElementById(`sendBtn`)
const input         = document.getElementById(`input`)
const initSpeech    = document.getElementById(`initSpeech`)
const speechOutput  = document.getElementById(`speechOutput`)
let transcript      = null

function postToServer(message) {
    console.log(`Sending this: ` + message)
    
    let request = new Request(`/api`, {
        method: `POST`,
        mode: `same-origin`, // alt. `cors`
        redirect: `follow`,
        body: JSON.stringify({
            message: message
        }),
        headers: new Headers({
            "Content-Type": `application/json`
        })
    })

    fetch(request)
        .then((response) => {
            return response.json()
        })
        .then((response) => {
            console.log(`Server: ` + JSON.stringify(response))
        })
}

function recordSpeech(event) {
    let recognition = new webkitSpeechRecognition();

    // Speech recognition config
    recognition.lang = `sv` // Swedish for best recognition of swedish accents
    // Consider enabling for richer feedback to user
    // recognition.interimResults = true

    recognition.onresult = (event) => {
        console.log(event.results[0][0].transcript)
        
        transcript = event.results[0][0].transcript
        speechOutput.value = transcript


        postToServer(transcript)
    }

    recognition.start()
}
initSpeech.addEventListener(`mouseup`, recordSpeech,false)

// FOR DEBUG
function sendPost(event) {
    event.preventDefault()
    var transcript = input.value
    console.log(`Speech transcipt: ` + transcript)
    postToServer(transcript)
}
sendBtn.addEventListener(`click`, sendPost, false)
@@ -287,24 +287,27 @@
 ssage) %7B%0A   
+ //
  console.log
@@ -322,16 +322,18 @@
 g this: 
+%5Cn
 %60 + mess
@@ -805,16 +805,18 @@
 Server: 
+%5Cn
 %60 + JSON
@@ -1567,16 +1567,19 @@
 alue%0A   
+ //
  console
@@ -1602,16 +1602,18 @@
 nscipt: 
+%5Cn
 %60 + tran

Disable client console logging