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

              
                // fork by https://codepen.io/PJCHENder/pen/mMzYgO?editors=0010 @PJCHEN
.control-panel
    button#start(style="float:left") Start Recognition
    button#stop(style="float:right") Stop Recognition
.words(contenteditable="true")
              
            
!

CSS

              
                html {
    font-size: 10px;
}

body {
    background: #ffc600;
    font-family: 'helvetica neue';
    font-weight: 200;
    font-size: 20px;
}

.words {
    max-width: 500px;
    margin: 50px auto;
    background: white;
    border-radius: 5px;
    box-shadow: 10px 10px 0 rgba(0, 0, 0, 0.1);
    padding: 1rem 2rem 1rem 3rem;
    background: -webkit-gradient(
            linear,
            0 0,
            0 100%,
            from(#d9eaf3),
            color-stop(4%, #fff)
        )
        0
        4px;
    background-size: 100% 3rem;
    position: relative;
    line-height: 3rem;
    min-height: 30rem;
}
p {
    margin: 0;
}

.words:before {
    content: '';
    position: absolute;
    width: 4px;
    top: 0;
    left: 30px;
    bottom: 0;
    border: 1px solid;
    border-color: transparent #efe4e4;
}
.control-panel {
    max-width: 580px;
    margin: 60px auto 0;
    overflow: auto;
}
button {
    color: #111;
    cursor: pointer;
    background-color: white;
    border-color: #f8f9fa;
    display: inline-block;
    font-weight: 400;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    user-select: none;
    border: 1px solid transparent;
    padding: .5rem .75rem;
    font-size: 1rem;
    line-height: 1.25;
    border-radius: .25rem;
    transition: all .15s ease-in-out;
    &:hover {
        color: #111;
        background-color: #e2e6ea;
        border-color: #dae0e5;
    }
}

              
            
!

JS

              
                let recognition = new webkitSpeechRecognition()

recognition.interinResults = true // 講話時即辨識
recognition.lang="zh-TW" // 辨識的語言
recognition.continuous = false // 持續辨識,不自動結束

let recognizing = false

/**
 * 開始使用語音辨識 API
 **/
const words = document.querySelector('.words')
let p = document.createElement('p')
words.appendChild(p)

recognition.addEventListener("result", e=>{
    p.textContent = e.results[0][0].transcript
    if(e.results[0].isFinal){
        p = document.createElement('p')
        words.appendChild(p)
    }
})


//  // 對結果處理
//   recognition.addEventListener('result', e => {
//     console.log('result', e);
//     const transcript = Array.from(e.results)
//       .map(result => result[0])
//       .map(result => result.transcript)
//       .join('');

//       // 處理敏感詞
//       const poopScript = transcript.replace(/poop|poo|shit|dump/gi, '💩');
//       console.log(poopScript);
//       p.textContent = poopScript;
      
//       // 如果語音停頓的話,就重新開一個段落等待輸入
//       if (e.results[0].isFinal) {
//         p = document.createElement('p');
//         words.appendChild(p);
//       }
//   });

/**
 * 因為講完一段話後辨識會自動結束,因此當觸發結束事件時,我們要在啟動語音辨識
 **/
recognition.addEventListener('end', e=>{
    recognizing? recognition.start():recognition.stop()
})

// 開始語音辨識
document.querySelector("#start").addEventListener("click", e => {
    e.preventDefault();
    e.stopPropagation();
    recognizing = true;
    recognition.start();
});

document.querySelector('#stop').addEventListener('click', e=>{
    e.preventDefault()
    e.stopPropagation()
    recognizing = false
    recognition.stop()
})
              
            
!
999px

Console