Skip to content

Instantly share code, notes, and snippets.

@brayhoward
Forked from dotproto/tts.js
Created August 4, 2017 12:55
Show Gist options
  • Save brayhoward/3f53099612b53a443ff2f3cdd4eba827 to your computer and use it in GitHub Desktop.
Save brayhoward/3f53099612b53a443ff2f3cdd4eba827 to your computer and use it in GitHub Desktop.
ES2015 Text to Speech bookmarklet (Ctrl+S). Set up: Copy and paste the contents of this gist into a new bookmark. Use: Select some text and click the bookmarklet to start speaking. Once activated on a page, you can use Ctrl+S to speak the selected text. Speech Synthesis API Info: https://developers.google.com/web/updates/2014/01/Web-apps-that-ta…
javascript: {
/* Adjust voice speed. Default = 1 */
var speed = 2.5;
if (window.runTTS === undefined) {
/* Text to Speech function. Adjust the value of msg.rate to increase/decrease the playback speed. */
window.runTTS = () => {
const text = window.getSelection().toString();
/* Stop any currently playing TTS and empty the queue */
speechSynthesis.cancel();
/* Only continue if the user has text selected */
if (!text) { return }
/* Start speaking the currently selected text at the user-define speed*/
const msg = new SpeechSynthesisUtterance();
msg.rate = speed || 1;
msg.text = text;
speechSynthesis.speak(msg);
};
/* Speak the current seleciton when the user presses Ctrl+S */
document.addEventListener('keydown', e => {
if (e.key === 's' && e.ctrlKey) {
e.preventDefault();
window.runTTS();
}
});
}
/* Start speaking the current selection when the bookmarklet is called */
window.runTTS();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment