HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="wrapper">
<h1>Simple Text To Speech</h1>
<h2>Making the 'Speak' button work and more...</h2>
<p id="warning">Sorry, your browser does not support the Web Speech API.</p>
<textarea id="txtFld" placeholder="type in some text here, or double click for a random message!"></textarea>
<label for="txtFld" id="lbl">Type in text above. Then click the Speak button below.</label>
<div>
<button type="button" id="speakBtn">Speak</button>
<br>
<p>Note: For best results on a Mac, use the latest version of Chrome, Safari, or FireFox. On Windows, use Chrome.</p>
<p>Sadly, it does not seem to work correctly in Mobile Safari. This though checking in JS shows that the APIs exist!
You can call the: <b>speechSynthesis.speak()</b> method and you won't get any errors... But you won't get any audible
speech being generated either! That sucks!
<small>(Friday, Dec 20, 2019)</small>
</p>
<p>This pen was forked from:
<a target="_top" href="https://codepen.io/chriscoyier/pen/eoKBBV">https://codepen.io/chriscoyier/pen/eoKBBV</a>
</p>
<p>Which in turn was forked from:
<a target="_top" href="https://codepen.io/sgestrella/pen/MxJLXm">https://codepen.io/sgestrella/pen/MxJLXm</a>
</p>
</div>
</div>
@charset "UTF-8";
*, *:before, *:after {box-sizing: border-box;}
:root {
--textcolor:black;
--accentcolor:#269;
--lightaccentcolor:#9df;
--reversecolor:white;
}
html {
font-size: 16px;
font-family: Trebuchet,sans-serif;
}
body {
background: var(--accentcolor);
color: var(--reversecolor);
margin: 0;
padding: 0;
line-height: 1.5;
}
strong, b {font-weight: bold;}
#wrapper {
margin: 0 auto;
padding: 1.2rem;
width: 100%;
height: auto;
min-width: 320px;
max-width: 960px;
background: var(--reversecolor);
color: var(--textcolor);
text-align: left;
border:2px solid var(--accentcolor);
border-radius:0 0 16px 16px;
box-shadow:4px 4px 16px #333;
}
h1 {
color: var(--accentcolor);
font-size: 3rem;
margin: 0;
}
p {
display:block;
font-size: 1rem;
margin: 1rem 0 1rem 0;
}
a:link, a:visited {color: var(--accentcolor);}
a:hover {text-decoration: none;}
#numFld {
font-size: 1.2rem;
width: 4rem;
text-align: right;
padding: 0;
margin: 0 0 0 4px;
}
#txtFld {
width: 100%;
height: 4.4rem;
font-size: 1.6rem;
font-family: inherit;
color:red;
}
button {
-webkit-appearance: none;
font-size: 1.4rem;
font-weight: normal;
background: var(--lightaccentcolor);
border-radius: 12px;
border: 2px solid var(--accentcolor);
color: var(--textcolor);
padding: 0.3rem 0.5rem 0.3rem 0.5rem;
cursor: pointer;
margin: 10px 20px 10px 20px;
}
button:hover {
color: var(--reversecolor);
background: var(--accentcolor);
}
button:disabled{
opacity:0.3;
cursor: not-allowed;
}
#warning {
color: red;
font-size: 1.4rem;
display: none;
}
/*
TEXT TO SPEECH...
SpeechSynthesisUtterance() documentation:
https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance
speechSynthesis() documentation:
https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis
*/
let u;
let bSpeaking = false;
const stuffToSay = [];
let nWaitBetween = 900;
function pageSetup(evt) {
//console.log("pageSetup() run")
if (!SpeechSynthesisUtterance || !speechSynthesis) {
const speakBtnNd = document.getElementById("speakBtn");
const warningNd = document.getElementById("warning");
const txtFldNd = document.getElementById("txtFld");
const lblNd = document.getElementById("lbl");
warningNd.style.display = "block";
speakBtnNd.style.display = "none";
txtFldNd.style.display = "none";
lblNd.style.display = "none";
return;
} // end if
u = new SpeechSynthesisUtterance();
u.addEventListener('end', doneSpeaking);
u.addEventListener('boundary', wordChange);
console.log("utterance set up")
sayIt("Hello there, Code pen user! How are you doing?");
sayIt("This pen demonstrates taking text, and speaking it using a built in speech synthesizer.");
sayIt("It uses the speech Synthesis and the Speech Synthesis Utterance APIs built into this web browser.");
sayIt("This pen was forked from another pen created by Chris Coyier.");
sayIt("Which in turn.");
sayIt("Was forked from a pen by Stephen, Estrella.");
sayIt("You can type in some text into the text box on this screen.");
sayIt("Then, simply press the Speak button, and I will read the text back to you!");
sayIt("Isn't that really cool?");
const speakBtnNd = document.getElementById("speakBtn");
const txtFldNd = document.getElementById("txtFld");
txtFldNd.addEventListener('dblclick', genRandomMsg);
speakBtnNd.addEventListener('click', sayText);
console.log("speak button event listener set up")
} // end of function pageSetup()
function sayIt(sText) {
const cmd = {};
cmd.cmd = "say";
cmd.text = sText;
stuffToSay.push(cmd);
sayIt2();
} // end of function sayIt()
function sayIt2() {
if (bSpeaking) return;
if (stuffToSay.length === 0) {
return;
} // end if
const cmd = stuffToSay.shift();
if (cmd.cmd === "say") {
bSpeaking = true;
let sText = cmd.text;
const speakBtnNd = document.getElementById("speakBtn");
speakBtnNd.innerHTML = "Speaking...";
u.text = sText;
speechSynthesis.speak(u);
} // end if
} // end of function sayIt()
/* called when Speak button if pressed: */
function sayText(evt) {
const txtFldNd = document.getElementById("txtFld");
let sText = txtFldNd.value;
if (sText === "") {
sayIt("There is no text in the text box!");
sayIt("Type in some text into the text box above.");
sayIt("Then.");
sayIt("You can press the Speak Button!");
sayIt("Then it will work!");
} // end if
sayIt(sText);
} // end of function sayText()
function doneSpeaking(evt) {
bSpeaking = false;
const speakBtnNd = document.getElementById("speakBtn");
speakBtnNd.innerHTML = "Speak";
if (stuffToSay.length > 0) {
setTimeout(sayIt2, nWaitBetween);
} // end if
} // end of function doneSpeaking()
function genRandomMsg() {
const msgs = [];
//msgs.push("");
msgs.push("I love the sound of my computer-generated voice.");
msgs.push("She sells sea shells, down by the sea shore.");
msgs.push("Code Pen Rocks!");
msgs.push("I'm sorry Dave, but I'm afraid I can't do that!");
msgs.push("I have mate in 12 moves!");
msgs.push("Warning! Alien approaching!");
msgs.push("Warning! Will Robinson!");
msgs.push("What you are suggesting is not logical!");
msgs.push("Will you Heart this pen?");
msgs.push("Was it apartment 2-A, or 2-B? To be or not to be, that is the question!");
msgs.push("Thank you for double clicking the text box!");
msgs.push("Don't forget to do that thing... that thing... what was it again?");
msgs.push("Computing! Please wait!");
msgs.push("We should reach the planet in 37 earth, days!");
msgs.push("Thank you for calling our no-help-whatsoever center. Dial zero, and you will not get a person, but will be dumped somewhere in the self-service call tree! Isn't that wonderful?");
msgs.push("JavaScript is a really expressive computer language. Don't you agree?");
msgs.push("You keep using that word. I don't think it means what you think it means!");
msgs.push("Too bad I don't work on iOS!");
msgs.push("Inconceivable!");
msgs.push("Have you seen a six fingered man?");
msgs.push("There will be blood, tonight!!");
msgs.push("I keep getting my merds, wixed!!");
msgs.push("I keep getting my Tang all Tongoled up!");
msgs.push("The ROOT. of the word is greek!");
msgs.push("Great Scott!!");
msgs.push("I know Kung Fu!!");
msgs.push("There is no spoon!");
const nPick = Math.floor(Math.random() * msgs.length);
const txtFldNd = document.getElementById("txtFld");
txtFldNd.value = msgs[nPick];
sayText(); // same as clicking "Speak" button!
} // end of function genRandomMsg()
let nTimerId = -1;
/*
called on "boundry" event.
See:
https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance/boundary_event
*/
function wordChange(evt) {
if (nTimerId > -1) {
try {
clearTimeout(nTimerId);
} catch(err) {
// do nothing if there is an error
} // end of try/catch
} // end if
const speakBtnNd = document.getElementById("speakBtn");
speakBtnNd.style.background = "yellow";
nTimerId = setTimeout(restoreBtnColor,50);
} // end of function wordChange()
function restoreBtnColor() {
nTimerId = -1;
const speakBtnNd = document.getElementById("speakBtn");
speakBtnNd.style.background = "#9df";
} // end of function restoreBtnColor()
window.addEventListener('load', pageSetup);
Also see: Tab Triggers