app.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // We need to import the CSS so that webpack will load it.
  2. // The MiniCssExtractPlugin is used to separate it out into
  3. // its own CSS file.
  4. import "../css/app.scss"
  5. // webpack automatically bundles all modules in your
  6. // entry points. Those entry points can be configured
  7. // in "webpack.config.js".
  8. //
  9. // Import deps with the dep name or local files with a relative path, for example:
  10. //
  11. // import {Socket} from "phoenix"
  12. // import socket from "./socket"
  13. //
  14. import "phoenix_html"
  15. window.onload = function () {
  16. document.getElementById('archiveUrlInput').addEventListener('keydown', function(event) {
  17. setTimeout(function () { generatePodcastUrl(); }, 100)
  18. });
  19. document.getElementById('copiaRss').addEventListener('click', function(event) {
  20. setTimeout(function () { copyToClipboard(fetchPodcastUrl()); }, 100)
  21. });
  22. };
  23. function fetchArchiveUrl() {
  24. return document.getElementById("archiveUrlInput").value.trim();
  25. }
  26. function fetchPodcastUrl() {
  27. return document.getElementById("podcastUrl").value.trim();
  28. }
  29. function generatePodcastUrl() {
  30. const archiveUrl = fetchArchiveUrl();
  31. if (!validateArchiveUrl(archiveUrl)) {
  32. document.getElementById("podcastUrl").value = "";
  33. return;
  34. }
  35. const url = new URL(archiveUrl);
  36. const podcastName = url.pathname.split("/").slice(-1).pop();
  37. const podcastUrl = "https://openpod.abbiamoundominio.org/podcast/" + podcastName;
  38. document.getElementById("podcastUrl").value = podcastUrl;
  39. }
  40. function validateArchiveUrl(url) {
  41. try {
  42. const archiveUrl = new URL(url);
  43. var urlPattern = new RegExp("(http|https)://archive.org/details/[a-zA-Z0-9\._-]*$")
  44. if (!urlPattern.test(url) ) {
  45. throw 'Paste and archive.org url';
  46. }
  47. } catch (e) {
  48. return false;
  49. }
  50. return true;
  51. }
  52. function copyToClipboard(text) {
  53. if (window.clipboardData && window.clipboardData.setData) {
  54. // Internet Explorer-specific code path to prevent textarea being shown while dialog is visible.
  55. return clipboardData.setData("Text", text);
  56. }
  57. else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
  58. var textarea = document.createElement("textarea");
  59. textarea.textContent = text;
  60. textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in Microsoft Edge.
  61. document.body.appendChild(textarea);
  62. textarea.select();
  63. try {
  64. return document.execCommand("copy"); // Security exception may be thrown by some browsers.
  65. }
  66. catch (ex) {
  67. console.warn("Copy to clipboard failed.", ex);
  68. return false;
  69. }
  70. finally {
  71. document.body.removeChild(textarea);
  72. }
  73. }
  74. }
  75. // archiveUrlInput
  76. // https://archive.org/details/uau-pod
  77. // https://openpod.abbiamoundominio.org/podcast/il-mio-podcast