HTML5 Speech Input with Voice Recognition

HTML5 support speech input x-webkit-speech and this has been implemented in the newer versions of Google Chrome.

Dictation is an online speech recognition app that uses Google’s Chrome speech engine to help you convert your spoken words into text.

The JavaScript code that powers the Dictation app is here:

// Written by Amit Agarwal on 10/08/2012
// See https://dictation.io for live demo

$(document).ready(function () {
  // Check if the user's web browser supports HTML5 Speech Input API
  if (document.createElement('input').webkitSpeech == undefined) {
    $('.answer').append('We are sorry but Dictation requires Google Chrome.');
  } else {
    // Get the default locale of the user's browser (e.g. en-US, or de)
    var language = window.navigator.userLanguage || window.navigator.language;
    $('#speech').attr('lang', language).focus();

    // Make the text region editable to easily fix transcription errors
    $('.answer').click(function () {
      $('.answer').attr('contentEditable', 'true');
    });
  }

  // This is called when Chrome successfully transcribes the spoken word
  $('#speech').bind('webkitspeechchange', function (e) {
    var val = $(this).val();

    // Did the user say Delete? Then clear the canvas.
    if (val == 'delete everything') {
      $('.answer').text('');
      return;
    }

    // For "new line" commands, add double line breaks.
    if (val == 'new line') val = '<br /><br />';
    else {
      // Capitalize the first letter of the sentence.
      val = val.substr(0, 1).toUpperCase() + val.substr(1);

      // If the last letter is a alphanumeric character, add a period (full stop)
      if (val.match(/[a-zA-Z]$/)) val = val + '.';
    }

    // Append the transcribed text but set the focus to the hidden speech input.
    // This enables keyboard shortcut Ctrl+Shift+Period (.) for speech mode.
    $('.answer')
      .append(val + ' ')
      .fadeIn();
    $(this).val('').focus();
  });
});

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻