From f01b0f0f8c54b31aa933c8f3b0c189fce903398a Mon Sep 17 00:00:00 2001 From: Tomkndn Date: Tue, 15 Oct 2024 21:19:43 +0530 Subject: [PATCH] created a share button --- index.html | 6 ++++++ script.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/index.html b/index.html index bb02977..55d5808 100644 --- a/index.html +++ b/index.html @@ -369,6 +369,12 @@

By Richard Dawkins

>View comments +
diff --git a/script.js b/script.js index e84ab15..f106e18 100644 --- a/script.js +++ b/script.js @@ -15,6 +15,40 @@ document.addEventListener("DOMContentLoaded", function () { let allBooksList = []; let currentBook; + const urlParams = new URLSearchParams(window.location.search); + const audioFile = urlParams.get("file"); + const startTime = urlParams.get("t"); + + if (audioFile && startTime) { + audioPlayer.src = `audio/${audioFile}`; + audioPlayer.currentTime = startTime; + audioPlayer.play(); + } + + const shareBtn = document.getElementById("share-link"); + + shareBtn.addEventListener("click", () => { + + const currentAudioFile = audioPlayer.src.split("/").pop(); + const currentTime = audioPlayer.currentTime; + + const currentUrl = `${ + window.location.href + }?file=${currentAudioFile}&t=${Math.floor(currentTime)}`; + const shareText = `Check out this audio "${currentAudioFile}" starting at ${Math.floor( + currentTime + )} seconds!`; + + navigator.clipboard + .writeText(currentUrl) + .then(() => { + alert(`Link copied to clipboard`); + }) + .catch((err) => { + console.error("Failed to copy: ", err); + }); + }); + // Function to display error popup function showErrorPopup(message) { const overlay = document.createElement("div");