-
Notifications
You must be signed in to change notification settings - Fork 28
/
articulate.min.js
1 lines (1 loc) · 7.62 KB
/
articulate.min.js
1
!function(a){"use strict";function q(a,b){this.prepend=a,this.append=b}function r(a,b){this.name=a,this.language=b}function s(){for(var a=speechSynthesis.getVoices(),b=0;b<a.length;b++)p.push(new r(a[b].name,a[b].lang))}var i,j,k,l,b=new Array,c=new Array,d=new Array,e=new Array,f=1.1,g=1,h=1,m=f,n=g,o=h,p=new Array;if(s(),"undefined"!=typeof speechSynthesis&&void 0!==speechSynthesis.onvoiceschanged&&(speechSynthesis.onvoiceschanged=s),"speechSynthesis"in window){var t=new SpeechSynthesisUtterance;window.speechSynthesis.cancel()}var u=/chrome/i.test(navigator.userAgent),v=/edge/i.test(navigator.userAgent),w=u&&!v,x={speak:function(p){function A(a){var d,f;if(c.length>0)for(var g in c){var h=z.indexOf(c[g]);h>-1&&z.splice(h,1)}for(var g in z)jQuery(a).find(z[g]).addBack(z[g]).not("[data-articulate-recognize]").each(function(){jQuery(this).html("")});if(b.length>0)for(var g in b)jQuery(a).find(b[g]).addBack(b[g]).not("[data-articulate-recognize]").each(function(){jQuery(this).html("")});jQuery(a).find("[data-articulate-ignore]").addBack("[data-articulate-ignore]").each(function(){jQuery(this).html("")}),jQuery(a).find("[data-articulate-prepend]").addBack("[data-articulate-prepend]").each(function(){d=jQuery(this).data("articulate-prepend"),jQuery(this).prepend(d+" ")}),jQuery(a).find("[data-articulate-append]").addBack("[data-articulate-append]").each(function(){d=jQuery(this).data("articulate-append"),jQuery(this).append(" "+d)});var i=0;for(var j in y)i++,i<=4&&jQuery(a).find(j).each(function(){e[j]?(jQuery(this).prepend(e[j].prepend+" "),jQuery(this).append(" "+e[j].append)):(jQuery(this).prepend(y[j].prepend+" "),jQuery(this).append(" "+y[j].append))});return jQuery(a).find("h1,h2,h3,h4,h5,h6,li,p").addBack("h1,h2,h3,h4,h5,h6,li,p").each(function(){jQuery(this).append(". ")}),jQuery(a).find("br").each(function(){jQuery(this).after(", ")}),jQuery(a).find("figure").addBack("figure").each(function(){d=jQuery(this).find("figcaption").html(),f=e.figure?e.figure.prepend:y.figure.prepend,void 0!=d&&""!==d&&jQuery("<div>"+f+" "+d+".</div>").insertBefore(this),jQuery(this).remove()}),jQuery(a).find("img").addBack("img").each(function(){d=jQuery(this).attr("alt");var a=jQuery(this).parent(),b=a.get(0).tagName;if(f=e.img?e.img.prepend:y.img.prepend,void 0!==d&&""!=d)if("PICTURE"==b){jQuery("<div>"+f+" "+d+".</div>").insertBefore(a)}else jQuery("<div>"+f+" "+d+".</div>").insertBefore(this);jQuery(this).remove()}),jQuery(a).find("table").addBack("table").each(function(){d=jQuery(this).find("caption").text(),f=e.table?e.table.prepend:y.table.prepend,void 0!==d&&""!=d&&jQuery("<div>"+f+" "+d+".</div>").insertBefore(this),jQuery(this).remove()}),jQuery(a).find("[data-articulate-swap]").addBack("[data-articulate-swap]").each(function(){d=jQuery(this).data("articulate-swap"),jQuery(this).text(d)}),jQuery(a).find("[data-articulate-spell]").addBack("[data-articulate-spell]").each(function(){d=jQuery(this).text(),d=d.split("").join(" "),jQuery(this).text(d)}),a}function B(a){for(var b,c,f,g,h,a;a.indexOf("<!-- <articulate>")!=-1&&(b=a.indexOf("<!-- <articulate>"),c=a.indexOf("</articulate> -->",b),c!=-1);)f=a.substring(b+17,c),g=a.substring(0,b),h=a.substring(c+17),a=g+" "+f+" "+h;a=a.replace(/<!--[\s\S]*?-->/g,""),a=a.replace(/(<([^>]+)>)/gi,"");for(var k,l,i=d.length,j=0;j<i;){k=d[j],k=k.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),l=d[j+1]+" ";var m=new RegExp(k,"gi"),a=a.replace(m,l);j+=2}e.q?(a=a.replace(/“/g,e.q.prepend+" "),a=a.replace(/”/g," "+e.q.append)):(a=a.replace(/“/g,y.q.prepend+" "),a=a.replace(/”/g," "+y.q.append)),a=a.replace(/—/g,", "),a=a.replace(/--/g,", ");var n=document.createElement("textarea");return n.innerHTML=a,a=n.value,a=a.replace(/(\r\n|\n|\r)/gm,""),a=a.replace(/ +/g," "),a=a.replace(/\.\./g,"."),a=a.replace(/,,/g,","),a=a.replace(/ ,/g,",")}var u,v,x,s=(a.extend({},a.fn.articulate.defaults,p),""),y=new Array;y.q=new q("quote,",", unquote,"),y.ol=new q("Start of list.","End of list."),y.ul=new q("Start of list.","End of list."),y.blockquote=new q("Blockquote start.","Blockquote end."),y.img=new q("There's an embedded image with the description,",""),y.table=new q("There's an embedded table with the caption,",""),y.figure=new q("There's an embedded figure with the caption,","");var z=["audio","button","canvas","code","del","dialog","dl","embed","form","head","iframe","meter","nav","noscript","object","s","script","select","style","textarea","video"];return"speechSynthesis"in window?window.speechSynthesis.speaking?void 0:(this.each(function(){u=a(this).clone(),v=A(u),v=jQuery(v).html(),x=B(v),s=s+" "+x}),m=void 0!==i?i:f,n=void 0!==j?j:g,o=void 0!==k?k:h,t=new SpeechSynthesisUtterance,t.text=s,t.rate=m,t.pitch=n,t.volume=o,w&&(t.voice=speechSynthesis.getVoices().filter(function(a){return"native"==a.name})[0]),void 0!==l&&(t.voice=speechSynthesis.getVoices().filter(function(a){return a.name==l})[0]),window.speechSynthesis.speak(t),this):void alert("Sorry, this browser does not support the Web Speech API.")},pause:function(){return window.speechSynthesis.pause(),this},resume:function(){return window.speechSynthesis.resume(),this},stop:function(){return window.speechSynthesis.cancel(),this},enabled:function(){return"speechSynthesis"in window},isSpeaking:function(){return window.speechSynthesis.speaking},isPaused:function(){return window.speechSynthesis.paused},rate:function(){var a=arguments[0];return a>=.1&&a<=10?i=a:void 0===a&&(i=void 0,m=f),this},pitch:function(){var a=arguments[0];return a>=.1&&a<=2?j=a:void 0===a&&(j=void 0,n=g),this},volume:function(){var a=arguments[0];return a>=0&&a<=1?k=a:void 0===a&&(k=void 0,o=h),this},ignore:function(){var a=arguments.length;for(b.length=0;a>0;)a--,b.push(arguments[a]);return this},recognize:function(){var a=arguments.length;for(c.length=0;a>0;)a--,c.push(arguments[a]);return this},replace:function(){var a=arguments.length;d.length=0;for(var b=0;b<a&&(d.push(arguments[b],arguments[b+1]),b+=2,a-b!=1););return this},customize:function(){var a=arguments.length;if(0==a&&(e=[]),2==a){if(["img","table","figure"].indexOf(arguments[0])==-1)return void console.log("Error: When customizing, tag indicated must be either 'img', 'table', or 'figure'.");e[arguments[0].toString()]=new q(arguments[1].toString())}if(3==a){if(["q","ol","ul","blockquote"].indexOf(arguments[0])==-1)return void console.log("Error: When customizing, tag indicated must be either 'q', 'ol', 'ul' or 'blockquote'.");e[arguments[0].toString()]=new q(arguments[1].toString(),arguments[2].toString())}return this},getVoices:function(){if(0==arguments.length)return p;var a=jQuery(arguments[0]),b="Choose a Different Voice";void 0!==arguments[1]&&(b=arguments[1]),a.append(jQuery("<select id='voiceSelect'><option value='none'>"+b+"</option></select>"));for(var c=0;c<p.length;c++){var d=document.createElement("option");d.textContent=p[c].name+" ("+p[c].language+")",d.setAttribute("value",p[c].name),d.setAttribute("data-articulate-language",p[c].language),a.find("select").append(d)}return a.on("change",function(){jQuery(this).find("option:selected").each(function(){"none"!=jQuery(this).val()&&(l=jQuery(this).val())})}),this},setVoice:function(){if(arguments.length<2)return this;var a,b;if("name"==arguments[0]){a=arguments[1];for(var c=0;c<p.length;c++)p[c].name==a&&(l=a)}if("language"==arguments[0])if(b=arguments[1].toUpperCase(),2==b.length){for(var c=0;c<p.length;c++)if(p[c].language.substring(0,2).toUpperCase()==b){l=p[c].name;break}}else for(var c=0;c<p.length;c++)if(p[c].language==b){l=p[c].name;break}return this}};a.fn.articulate=function(a){return x[a]?x[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?void jQuery.error("Method "+a+" does not exist on jQuery.articulate"):x.speak.apply(this,arguments)}}(jQuery);