Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="display">
  <p class="explain">Click the button to begin, accept the mic permissions, and give some math commands! Try "Add 5", "Divide by 10", "Multiply by 2", "Subtract 8", "Repeat" (make sure your sound is on), "Reset to 100" or "Stop".</p>
  <button id="toggle">Start Listening</button>
  <div id="target">
    <p>loading ...</p>  
  </div>
  <p class="explain">Made with 💙 using <a target="_blank" href="https://aka.ms/L1glyq">Microsoft Cognitive Services</a></p>
  <div id="not-supported" class="takeover" style="display: none">
    <h1>Your browser doesn't support the SpeechRecognition API</h1>
    <p>We're as sad as your are. As of creation, just Chromium-based browsers do. Firefox has the feature working but they're sorting out the permissions for it. For now try it in Chrome. See <a href="https://caniuse.com/#feat=speech-recognition">here</a> to see the support table.</p>
    <p>If you'd like to just see a YouTube video of me using it, <a target="_blank" href="https://youtu.be/4f6BxSLPMT8">check here</a>.</p>
  </div>
  <div id="not-available" class="takeover" style="display: none">
    <h1>This demo ran out of API credit</h1>
    <p>This got more popular than we anticipated.</p>
    <p>If you'd like to just see a YouTube video of me using it, <a target="_blank" href="https://youtu.be/4f6BxSLPMT8">check here</a>. Otherwise try back later.</p>
  </div>
</div>
              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}

.display {
  width: 100vw;
  background: #85144b;
  color: #9CB7E2;
  font-family: 'Supermercado One', cursive;
  transition: .5s ease background-color;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  min-height: 100vh;
  position: relative;
}

.explain {
  width: 100%;
  text-align: center;
  color: #E8E599;
  font-size: 20px;
}

#target {
  font-size: 100px;
  width: 100%;
  cursor: pointer;
  text-align: center;
}

.listening {
  background-color: #3D9970;
}

.listening #toggle {
  background-color: #85144b;
}

#toggle {
  width: 150px;
  height: 150px;
  display: block;
  margin: 0 auto;
  border-radius: 50%;
  background-color: #3D9970;
  color: white;
  font-family: 'Supermercado One', cursive;
  font-size: 25px;
  cursor: pointer;
}

.takeover {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(25, 25, 25, .9);
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
}

a {
  color: white;
}

a:hover, a:active {
  color: #ddd;
}
              
            
!

JS

              
                const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const SpeechGrammarList = window.SpeechGrammarList || window.webkitSpeechGrammarList;
const SpeechRecognitionEvent = window.SpeechRecognitionEvent || window.webkitSpeechRecognitionEvent;

if (!SpeechRecognition) {
  document.getElementById('not-supported').style.display = 'flex';
}

const recognition = new SpeechRecognition();

let total = 0;
const node = document.getElementById('target');
const button = document.getElementById('toggle');
const htmlNode = document.querySelector('.display');

recognition.continuous = true;
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 1;

let listening = false;
recognition.onstart = () => { listening = true };
recognition.onend = () => { listening = false };

document.getElementById('toggle').onclick = function() {
  listening ? stop() : start();
}

function start() {
  recognition.start();
  button.textContent = "Stop Listening";
  htmlNode.classList.add('listening');
}

function stop() {
  recognition.abort();
  button.textContent = "Start Listening";
  htmlNode.classList.remove('listening');
}

function update(num) {
  node.textContent = num;
}


const synth = window.speechSynthesis;
function say(phrase) {
  const utterance = new SpeechSynthesisUtterance(phrase);
  stop();
  button.disabled = true;
  setTimeout(() => {
    start();
    button.disabled = true;
  }, 2500);
  console.log(`Saying: ${phrase}`);
  synth.speak(utterance);
}

recognition.onresult = function(event) {
  const last = event.results.length - 1;
  const phrase = event.results[last][0].transcript;
  
  requestData(phrase).then((response) => {
    console.log(response);
    
    const { intent, score } = response.data.intents[0] || { intent: "None" };
    
    if (score < .3) {
      recognition.abort();
      say("I'm not sure what you said.");
      return;
    }
  
    const { entities } = response.data;
    let entity;
    if (entities.length) {
      entity = entities[0];
    }
    
    switch (intent) {
      case "Calculator.Add":
        if (!entity) return;
        total += +entity.resolution.value;
        break;
      case "Calculator.Subtract":
        if (!entity) return;
        total -= +entity.resolution.value;
        break;
      case "Calculator.Multiply":
        if (!entity) return;
        total *= +entity.resolution.value;
        break;
      case "Calculator.Divide":
        if (!entity) return;
        total /= +entity.resolution.value;
        break;
      case "Calculator.Reset": 
        total = entity ? +entity.resolution.value : 0;
        break;
      case "App.Stop":
        stop();
        break;
      case "App.Repeat":
        say(`The current total is ${total}`);
        break;
      default:
        // should never reach here
    }
    update(total);
  }).catch(() => {
    stop();
    document.getElementById('not-available').style.display = 'flex';
  })
}

const requestData = (query) => {
  const uribase = `https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/fedda2e4-21c0-42b6-a693-126f126b2388`;

  return axios({
    method: "get",
    url: uribase,
    params: {
      verbose: true,
      timezoneOffset: 0,
      q: query
    },
    headers: {
      "Content-Type": "application/json",
      "Ocp-Apim-Subscription-Key": "dac1f04f2a85466cb25bc4154692ab91"
    }
  });
}

update(total);
              
            
!
999px

Console