diff --git a/assets/js/app.js b/assets/js/app.js index 8c02de5..5f7066f 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -12,4 +12,80 @@ import "../css/app.scss" // import {Socket} from "phoenix" // import socket from "./socket" // -import "phoenix_html" \ No newline at end of file +import "phoenix_html" + +window.onload = function () { + document.getElementById('archiveUrlInput').addEventListener('keydown', function(event) { + setTimeout(function () { generatePodcastUrl(); }, 100) + }); + + document.getElementById('copiaRss').addEventListener('click', function(event) { + setTimeout(function () { copyToClipboard(fetchPodcastUrl()); }, 100) + }); +}; + +function fetchArchiveUrl() { + return document.getElementById("archiveUrlInput").value.trim(); +} + +function fetchPodcastUrl() { + return document.getElementById("podcastUrl").value.trim(); +} + +function generatePodcastUrl() { + const archiveUrl = fetchArchiveUrl(); + if (!validateArchiveUrl(archiveUrl)) { + document.getElementById("podcastUrl").value = ""; + return; + } + + const url = new URL(archiveUrl); + const podcastName = url.pathname.split("/").slice(-1).pop(); + const podcastUrl = "https://openpod.abbiamoundominio.org/podcast/" + podcastName; + + document.getElementById("podcastUrl").value = podcastUrl; +} + +function validateArchiveUrl(url) { + try { + const archiveUrl = new URL(url); + + var urlPattern = new RegExp("(http|https)://archive.org/details/[a-zA-Z0-9_-]*$") + if (!urlPattern.test(url) ){ + throw 'Paste and archive.org url'; + } + } catch (e) { + return false; + } + + return true; +} + +function copyToClipboard(text) { + if (window.clipboardData && window.clipboardData.setData) { + // Internet Explorer-specific code path to prevent textarea being shown while dialog is visible. + return clipboardData.setData("Text", text); + + } + else if (document.queryCommandSupported && document.queryCommandSupported("copy")) { + var textarea = document.createElement("textarea"); + textarea.textContent = text; + textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in Microsoft Edge. + document.body.appendChild(textarea); + textarea.select(); + try { + return document.execCommand("copy"); // Security exception may be thrown by some browsers. + } + catch (ex) { + console.warn("Copy to clipboard failed.", ex); + return false; + } + finally { + document.body.removeChild(textarea); + } + } +} + +// archiveUrlInput +// https://archive.org/details/uau-pod +// https://openpod.abbiamoundominio.org/podcast/il-mio-podcast diff --git a/lib/openpod_web/templates/page/index.html.eex b/lib/openpod_web/templates/page/index.html.eex index 7091b00..931dd0c 100644 --- a/lib/openpod_web/templates/page/index.html.eex +++ b/lib/openpod_web/templates/page/index.html.eex @@ -55,25 +55,26 @@ 🥳