Skip to content

Instantly share code, notes, and snippets.

@Cmaury
Last active August 29, 2015 14:00
Show Gist options
  • Save Cmaury/3abb0470491ef1af63d0 to your computer and use it in GitHub Desktop.
Save Cmaury/3abb0470491ef1af63d0 to your computer and use it in GitHub Desktop.
AudioBlur: Text To Speech headline reader for NewsBlue
textToBeSpoken = new SpeechSynthesisUtterance();
textToBeSpoken.volume = 1; // 0 to 1
textToBeSpoken.rate = 10; // 0.1 to 10
textToBeSpoken.pitch = 2; //0 to 2
textToBeSpoken.lang = 'en-US';
function checkForChanges() {
currentlySelectedItem = document.getElementsByClassName('NB-feed-story NB-river-story read NB-selected');
if (currentlySelectedItem.length == 0) {
setTimeout(checkForChanges, 500);
}
else {
//console.log('this article has already been read');
//console.log(currentlySelectedItem[0].getElementsByClassName('NB-feed-story-title')[0].innerHTML);
if (currentlySelectedItem[0].id == 'spoken') {
setTimeout(checkForChanges,1000)
}
else {
speechSynthesis.cancel();
console.log('the app should be speaking')
currentlySelectedItem[0].id = 'spoken';
console.log(currentlySelectedItem[0].id)
//extract the headline
currentlySelectedHeadline = currentlySelectedItem[0].getElementsByClassName('NB-feed-story-title')[0].innerHTML;
//extract the article body
currentlySelectedArticle = currentlySelectedItem[0].getElementsByClassName('NB-feed-story-content')[0].textContent;
wordArray = currentlySelectedArticle.match(/\S+\s*/g);
//break article body into blocks of text
phraseArray = [''];
var y = 0;
var i = 1;
length = wordArray.length //set once since we are shifting array values
while (i <= length) {
while (i % 25 != 0) {
phraseArray[y] += wordArray.shift();
i++
if (i == length) break;
}
i++;
y++;
phraseArray[y] = '';
};
console.log(currentlySelectedHeadline + currentlySelectedArticle);
//speak headline
textToBeSpoken.text = currentlySelectedHeadline;
speechSynthesis.speak(textToBeSpoken);
setTimeout(checkForChanges,500)
}
};
};
checkForChanges();
//add hotkey to repeat last utterance - Hotkey is Q
window.onkeypress = function (e) {
char = e.keyCode;
console.log(char);
if (char == 113) {
speechSynthesis.cancel();
console.log("success!");
textToBeSpoken.text = currentlySelectedHeadline;
speechSynthesis.speak(textToBeSpoken);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment