Skip to content

Instantly share code, notes, and snippets.

@getflourish
Forked from jeffehobbs/speaker.js
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getflourish/701eed32e30bec67eab5 to your computer and use it in GitHub Desktop.
Save getflourish/701eed32e30bec67eab5 to your computer and use it in GitHub Desktop.
Bookmarklet to scrub and speak current URL via SpeechSynthesisUtterance. (Please use your own Readability API token). Added chunking.
javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"2.0.3",function($,L){
// speak current webpage
// jhobbs 2013
// chunking by getflourish 2015
function stop(){
speechSynthesis.cancel();
}
function pause(){
speechSynthesis.pause();
}
function resume(){
speechSynthesis.resume();
}
function getVoices(){
var voices = speechSynthesis.getVoices();
for(var i = 0; i < voices.length; i++ ) {
console.log("Voice " + i.toString() + ' ' + voices[i].name);
}
}
function play(articleText){
if((navigator.userAgent.match(/iPhone/i)) ||
(navigator.userAgent.match(/iPod/i)) ||
(navigator.userAgent.match(/iPad/i))) {
rate = 0.66;
} else {
rate = 1;
}
var speech = new SpeechSynthesisUtterance();
speech.rate = rate;
speech.pitch = 1;
speech.text = articleText;
speech.volume = 1;
speech.lang = "en-US";
speech.voiceURI = "Tom";
speech.onstart = function(event) { console.log('Started speaking.'); };
speech.onend = function(event) { console.log('Stopped speaking.'); };
speech.onpause = function(event) { console.log('Paused.'); };
speech.onresume = function(event) { console.log('Resumed.'); };
speech.onmark = function(event) { console.log('Reached mark.'); };
speech.onboundary = function(event) { console.log('Reached boundary.'); };
var chunks = articleText.split(/[\.!;\–\?\:]+[\s]+/g);
if (chunks != null) {
// add the last chunk if it has no sentence end
var lastChunk = chunks[chunks.length];
if (articleText.indexOf(lastChunk) == articleText.length - chunks.length) {
chunks.push(what.substr(articleText.indexOf(lastChunk), articleText.length));
}
for (var i = 0; i < chunks.length; i++) {
speech = new SpeechSynthesisUtterance(chunks[i]);
window.speechSynthesis.speak(speech);
}
} else {
speech = new SpeechSynthesisUtterance(articleText);
window.speechSynthesis.speak(speech);
}
speechSynthesis.speak(speech);
var isPlaying = true;
}
var url = document.location.href.match(/(^[^#]*)/)[0];
var readabilityToken = "YOUR_READABILITY_API_TOKEN";
var apicall = "https://readability.com/api/content/v1/parser?url="+url+"&token="+readabilityToken+"&callback=?";
console.log("Readability apicall: " + apicall);
$.getJSON(apicall, function(data) {
$("body").html("");
$("body").css("padding","5% 25%");
$("body").html("<div id='article-text'></div>");
$("#article-text").css("width","50%");
$("#article-text").css("font-family","Georgia, serif");
$("#article-text").append("<p><a href='#' onclick='play(articleText);'>Play</a> | <a href='#' onclick='stop();'>Stop</a> | <a href='#' onclick='pause();'>Pause</a> | <a href='#' onclick='resume();'>Resume</a></p>");
$("#article-text").append("<h2 id='article-headline'><strong>"+data.title+"</strong></h2>");
if(data.author){
$("#article-text").append("<p><strong>"+data.author+"</strong></p>");
}
if(data.date_published){
var pubDate = data.date_published;
$("#article-text").append("<p><strong>"+pubDate+"</strong></p>");
}
if(data.lead_image_url){
$("#article-text").append("<div id='lead_image_url'><img src='"+data.lead_image_url+"'/></div>");
$("#lead_image_url").css("display","inline");
$("#lead_image_url").css("float","right");
$("#lead_image_url").css("padding","0px 0px 8px 8px");
}
$("#article-text").append(data.content);
rawContent = data.content;
var scrubbedRawContent = rawContent.replace(/<(?:.|\n)*?>/gm, '');
articleText = data.title + $(data.content).text();
console.log(articleText);
getVoices();
play(articleText);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment