-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathext.min.js
1 lines (1 loc) · 1.77 KB
/
ext.min.js
1
var pageToSpeech={data:{highlightedText:"",speechInProgress:!1,fallbackAudio:null},initialize:function(){pageToSpeech.hasText()&&!this.data.speechInProgress&&(pageToSpeech.trySpeechSynthesizer()||pageToSpeech.trySpeechApi())},hasText:function(){if(this.data.highlightedText=window.getSelection().toString(),!this.data.highlightedText){var e=document.createElement("input");e.setAttribute("type","text"),e.id="sandbox",document.getElementsByTagName("body")[0].appendChild(e);var t=document.getElementById("sandbox");t.value="",t.style.opacity=0,t.focus(),document.execCommand("paste")&&(this.data.highlightedText=t.value),t.value=""}return this.data.highlightedText},trySpeechSynthesizer:function(){if(window.speechSynthesis){this.data.speechInProgress&&(polyfills.speechUtteranceChunker.cancel=!0),this.data.speechInProgress=!0;var e=new SpeechSynthesisUtterance(this.data.highlightedText);return polyfills.speechUtteranceChunker(e,{chunkLength:120},function(){pageToSpeech.data.speechInProgress=!1}),!0}return!1},trySpeechApi:function(){this.data.speechInProgress&&this.data.fallbackAudio.pause(),this.data.speechInProgress=!0,this.data.fallbackAudio=new Audio("http://api.voicerss.org/?key=20ae15bb063e44beb4797449de15174c&src="+this.data.highlightedText),this.data.fallbackAudio.addEventListener("error",function(e){alert("Sorry, we cannot produce speech right now. Try upgrading your Chrome browser!")}),this.data.fallbackAudio.play(),this.data.fallbackAudio.onended=function(){pageToSpeech.data.speechInProgress=!1}},addHotkeys:function(){var e=[];onkeydown=onkeyup=function(t){var a=t||event;e[a.keyCode]="keydown"==a.type,e[16]&&e[17]&&e[89]&&pageToSpeech.initialize()}}};chrome.extension.onMessage.addListener(function(e,t,a){"pageToSpeech"==e.action&&pageToSpeech.initialize()}),pageToSpeech.addHotkeys();