From ec077435af1225ec5d490f286d229801a3c5452e Mon Sep 17 00:00:00 2001 From: kom-senapati Date: Thu, 11 Jul 2024 10:23:24 +0530 Subject: [PATCH] code: quotely laughs --- public/Quotely-Laughs/index.html | 102 +++++++++++++++++++++++++++++++ public/Quotely-Laughs/script.js | 87 ++++++++++++++++++++++++++ public/index.html | 6 ++ 3 files changed, 195 insertions(+) create mode 100644 public/Quotely-Laughs/index.html create mode 100644 public/Quotely-Laughs/script.js diff --git a/public/Quotely-Laughs/index.html b/public/Quotely-Laughs/index.html new file mode 100644 index 00000000..9248a2a1 --- /dev/null +++ b/public/Quotely-Laughs/index.html @@ -0,0 +1,102 @@ + + + + + + Quotely Laughs + + + + + + +
+

Quotely Laughs

+
+
+ +
+
+

+
+

Share quote on:

+ +
+
+
+

+
+

Share joke on:

+ +
+
+
+
+ + + + diff --git a/public/Quotely-Laughs/script.js b/public/Quotely-Laughs/script.js new file mode 100644 index 00000000..12ef8448 --- /dev/null +++ b/public/Quotely-Laughs/script.js @@ -0,0 +1,87 @@ +document.getElementById("generate").addEventListener("click", generateContent); + +document.addEventListener("DOMContentLoaded", async () => { + await generateContent(); +}); + +async function generateContent() { + showLoading(); + const quote = await getRandomQuote(); + const joke = await getRandomJoke(); + hideLoading(); + + document.getElementById("quote").innerText = quote; + document.getElementById("joke").innerText = joke; + + updateShareLinks(quote, joke); +} + +function showLoading() { + document.getElementById("loading").classList.remove("hidden"); +} + +function hideLoading() { + document.getElementById("loading").classList.add("hidden"); +} + +async function getRandomQuote() { + const quoteApiUrl = "https://api.freeapi.app/api/v1/public/quotes/quote/random"; + try { + const response = await fetch(quoteApiUrl); + if (response.ok) { + const quoteData = await response.json(); + const quoteText = quoteData.data.content; + const quoteAuthor = quoteData.data.author; + return `"${quoteText}" - ${quoteAuthor}`; + } + } catch (error) { + console.warn("Failed to fetch a quote from the API:", error); + } + + const fallbackQuotes = [ + "The only way to do great work is to love what you do. -Steve Jobs", + "Believe you can and you're halfway there. -Theodore Roosevelt", + "Success is not final, failure is not fatal: It is the courage to continue that counts. -Winston Churchill", + "In the middle of difficulty lies opportunity. -Albert Einstein", + "Don't watch the clock; do what it does. Keep going. -Sam Levenson", + ]; + return fallbackQuotes[Math.floor(Math.random() * fallbackQuotes.length)]; +} + +async function getRandomJoke() { + const jokeApiUrl = + "https://v2.jokeapi.dev/joke/Programming,Spooky?blacklistFlags=political,racist,sexist&format=txt"; + try { + const response = await fetch(jokeApiUrl); + if (response.ok) { + return await response.text(); + } + } catch (error) { + console.warn("Failed to fetch a joke from the API:", error); + } + + const fallbackJokes = [ + "Why don’t scientists trust atoms? Because they make up everything!", + "I told my wife she was drawing her eyebrows too high. She looked surprised.", + "Why don’t skeletons fight each other? They don’t have the guts.", + "What do you call fake spaghetti? An impasta.", + ]; + return fallbackJokes[Math.floor(Math.random() * fallbackJokes.length)]; +} + +function updateShareLinks(quote, joke) { + const siteURL = + "https://50-days-50-web-project.vercel.app/Quotely-Laughs/index.html"; + + document.getElementById( + "share-quote-x" + ).href = `https://twitter.com/intent/tweet?text=${encodeURIComponent( + `${quote} \n\nGenerated by Quotely Laughs.\nVisit site at ${siteURL}` + )}`; + + document.getElementById( + "share-joke-x" + ).href = `https://twitter.com/intent/tweet?text=${encodeURIComponent( + `${joke} \n\nGenerated by Quotely Laughs.\nVisit site at ${siteURL}` + )}`; +} diff --git a/public/index.html b/public/index.html index 1c822de2..2bf14517 100644 --- a/public/index.html +++ b/public/index.html @@ -438,6 +438,12 @@
Insect-Catch_Game
Demo + +
+
Day 71
+
Quotely Laughs
+ Demo +