-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27eb35d
commit 70ca77c
Showing
4 changed files
with
60 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,47 @@ | ||
// Get the button and audio elements from the HTML | ||
const button = document.getElementById('button'); | ||
const audioElement = document.getElementById('audio'); | ||
|
||
function test() { | ||
// Function to disable/enable the button | ||
function toggleButton() { | ||
button.disabled = !button.disabled; // Toggle the disabled property of the button | ||
} | ||
|
||
// Function to pass the joke to the VoiceRSS API for text-to-speech | ||
function tellMe(joke) { | ||
const jokeString = joke.trim().replace(/ /g, '%20'); // Prepare the joke string for the API | ||
VoiceRSS.speech({ | ||
key: '83cad11a89d34667a4e3e59968eb945b', | ||
src: 'Hello, world!', | ||
hl: 'en-us', | ||
v: 'Linda', | ||
r: 0, | ||
c: 'mp3', | ||
f: '44khz_16bit_stereo', | ||
ssml: false | ||
key: '83cad11a89d34667a4e3e59968eb945b', // API key for VoiceRSS | ||
src: jokeString, // The joke to be spoken | ||
hl: 'en-us', // Language of the speech | ||
v: 'Linda', // Voice selection | ||
r: 0, // Speech rate | ||
c: 'mp3', // Audio codec | ||
f: '44khz_16bit_stereo', // Audio format | ||
ssml: false // SSML support | ||
}); | ||
} | ||
test(); | ||
|
||
// Asynchronous function to fetch jokes from the Joke API | ||
async function getJokes() { | ||
let joke = ''; // Variable to store the joke | ||
const apiUrl = 'https://v2.jokeapi.dev/joke/Programming?blacklistFlags=nsfw,religious,political,racist,sexist,explicit'; // URL of the Joke API | ||
try { | ||
const response = await fetch(apiUrl); // Fetch data from the API | ||
const data = await response.json(); // Convert response to JSON | ||
if (data.setup) { | ||
joke = `${data.setup} ... ${data.delivery}`; // Combine setup and delivery if the joke has both | ||
} else { | ||
joke = data.joke; // Use single part joke | ||
} | ||
tellMe(joke); // Pass the joke to the text-to-speech function | ||
toggleButton(); // Disable the button | ||
} catch (error) { | ||
console.log(error); // Log any errors to the console | ||
} | ||
} | ||
|
||
// Event listener for the button to fetch a new joke when clicked | ||
button.addEventListener('click', getJokes); | ||
// Event listener for the audio element to re-enable the button when the audio ends | ||
audioElement.addEventListener('ended', toggleButton); |
This file contains 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