-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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