n-speech
Version:
nSpeech is screen reader javascript library (TTS).
16 lines (15 loc) • 3.83 kB
JavaScript
var nSpeech=function(){"use strict";/*!
* nSpeech
* nSpeech is screen reader library.
*
* https://github.com/nkhr7/nSpeech/
*
* Copyright 2017, Koichi Yoshimoto
*
* Version: 1.0.7
*
* Licensed under MIT
*
* Released on: 2017.10.19
*/
function e(n,t){if("undefined"==typeof SpeechSynthesisUtterance)return void alert("音声読み上げに未対応なブラウザです。\n\n音声読み上げ可能ブラウザ\nGoogle Chrome、Safari、Opera");const o=Object.create(e.prototype);let c=void 0!==n?n:".speech",u=[];const i=window.speechSynthesis;"object"==typeof n&&(t=c,c=".speech"),o.options={lang:"",voice:null,volume:1,rate:1,pitch:1,text:"",selectId:"",selectElement:"option",onboundary:function(){},onend:function(){v()},onerror:function(){},onmark:function(){},onpause:function(){},onresume:function(){},onstart:function(){},debug:!1},o.elements=[],o.utterance=new window.SpeechSynthesisUtterance;let r="";const a=function(){o.elements=s(c),f(),l(t),u.length>0&&(d(),m(),g(),b())},s=function(e){return document.querySelectorAll(e)},f=function(){u=i.getVoices()},l=function(e){"object"==typeof e&&Object.keys(e).forEach(function(n){o.options[n]=e[n]})},p=function(e){let n={};if(u.length>0)for(const t in u){if(u[t].lang===e)return u[t];!0===u[t].default&&(n=u[t])}return n},d=function(){o.options.voice=p(o.options.lang)},m=function(){let e=h(o.options.text);const n=o.elements.length;for(let t=0;t<n;++t){let n;n="INPUT"===o.elements[t].tagName||"TEXTAREA"===o.elements[t].tagName?o.elements[t].value:o.elements[t].textContent,e+=h(n)+" "}o.options.text=e},g=function(){o.utterance.voice=o.options.voice,o.utterance.volume=o.options.volume,o.utterance.rate=o.options.rate,o.utterance.pitch=o.options.pitch,o.utterance.text=o.options.text,o.utterance.onboundary=o.options.onboundary,o.utterance.onend=o.options.onend,o.utterance.onerror=o.options.onerror,o.utterance.onmark=o.options.onmark,o.utterance.onpause=o.options.onpause,o.utterance.onresume=o.options.onresume,o.utterance.onstart=o.options.onstart,o.options.debug&&console.log(o.utterance)},h=function(e){let n="";const t=/[\r]+|[\n]+|[\n\r]+|[\s]{2,}/g;let c=/[. ]{2,}/g,u=". ";return"ja-JP"===o.options.voice.lang&&(c=/。{2,}/g,u="。"),void 0!==e&&"string"==typeof e&&(n=(n=e.replace(t,u).trim()).replace(c,u).trim()),n},y=function(){let e="";return""!==(e=window.getSelection?window.getSelection().toString():document.selection.createRange().text)&&(r=o.utterance.text,o.utterance.text=h(e),!0)},v=function(){return""!==r&&(o.utterance.text=r,!0)},b=function(){if(document.getElementById(o.options.selectId))for(let e=0;e<u.length;e++){const n=document.createElement(o.options.selectElement);n.textContent=u[e].name+" ("+u[e].lang+")",n.setAttribute("value",u[e].lang),document.getElementById(o.options.selectId).appendChild(n)}};return o.play=function(){null===o.options.voice&&a(),y(),i.speak(o.utterance),o.options.debug&&console.log(o.utterance)},o.pause=function(){i.pause(o.utterance)},o.resume=function(){i.resume(o.utterance)},o.stop=function(){v(),i.cancel()},o.replaceText=function(e){o.utterance.text=e},o.changeVoice=function(e){o.utterance.voice=p(e)},o.changeVolume=function(e){o.utterance.volume=e},o.changeRate=function(e){o.utterance.rate=e},o.changePitch=function(e){o.utterance.pitch=e},o.onboundary=function(e){"function"==typeof e&&l({onboundary:e})},o.onend=function(e){v(),"function"==typeof e&&l({onend:e})},o.onerror=function(e){"function"==typeof e&&l({onerror:e})},o.onmark=function(e){"function"==typeof e&&l({onmark:e})},o.onpause=function(e){"function"==typeof e&&l({onpause:e})},o.onresume=function(e){"function"==typeof e&&l({onresume:e})},o.onstart=function(e){"function"==typeof e&&l({onstart:e})},window.onbeforeunload=function(){o.stop()},void 0!==i.onvoiceschanged&&(i.onvoiceschanged=a),a(),o}return e}();