Skip to content

Instantly share code, notes, and snippets.

@amnuts
Last active March 13, 2020 12:58
Show Gist options
  • Save amnuts/b09e3d95085fa4aacddc to your computer and use it in GitHub Desktop.
Save amnuts/b09e3d95085fa4aacddc to your computer and use it in GitHub Desktop.
Add a link to read text aloud including the option of swapping to any available voices.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script
src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8="
crossorigin="anonymous"></script>
</head>
<body>
<main id="content">
<p>CHAPTER 1. Loomings.</p>
<p>Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.</p>
<p>There now is your insular city of the Manhattoes, belted round by wharves as Indian isles by coral reefs—commerce surrounds it with her surf. Right and left, the streets take you waterward. Its extreme downtown is the battery, where that noble mole is washed by waves, and cooled by breezes, which a few hours previous were out of sight of land. Look at the crowds of water-gazers there.</p>
<p>Circumambulate the city of a dreamy Sabbath afternoon. Go from Corlears Hook to Coenties Slip, and from thence, by Whitehall, northward. What do you see?—Posted like silent sentinels all around the town, stand thousands upon thousands of mortal men fixed in ocean reveries. Some leaning against the spiles; some seated upon the pier-heads; some looking over the bulwarks of ships from China; some high aloft in the rigging, as if striving to get a still better seaward peep. But these are all landsmen; of week days pent up in lath and plaster—tied to counters, nailed to benches, clinched to desks. How then is this? Are the green fields gone? What do they here?</p>
</main>
<script>
if ('speechSynthesis' in window) {
var createdVoiceList = false;
$('#content').append(
$('<div/>', {'class':'speechUI'}).append('<a href="#" class="speakContent">read text aloud</a> &nbsp; ')
);
var addVoices = function() {
var voices = window.speechSynthesis.getVoices();
if (voices.length && !createdVoiceList) {
var select = $('<select/>', {'class':'speakContentVoice'});
voices.forEach(function(v) {
select.append($('<option/>').val(v.name).text(v.name + ' (' + v.lang + ')').prop('selected', v.default));
});
$('div.speechUI').append(select);
createdVoiceList = true;
}
};
// Chrome loads async
if ('onvoiceschanged' in window.speechSynthesis) {
window.speechSynthesis.onvoiceschanged = function(e) {
addVoices();
};
} else {
addVoices();
}
var readText = $('#content').text();
$(document).on('click', 'a.speakContent', function(e){
e.preventDefault();
var link = $(this);
var useVoice = $('select.speakContentVoice').val();
var msg = new SpeechSynthesisUtterance();
msg.text = readText;
if (useVoice) {
msg.voice = speechSynthesis.getVoices().filter(function(voice){
return voice.name == useVoice;
})[0];
}
link.addClass('cancel').text('stop reading text');
$('select.speakContentVoice').hide();
msg.onend = function(e) {
link.removeClass('cancel').text('read aloud content');
$('select.speakContentVoice').show();
window.speechSynthesis.cancel();
};
window.speechSynthesis.speak(msg);
return false;
});
$(document).on('click', 'a.speakContent.cancel', function(e){
$(this).removeClass('cancel').text('read aloud content');
window.speechSynthesis.cancel();
$('select.speakContentVoice').show();
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment