diff --git a/public/main.js b/public/main.js index 8cacc5d..ef205c6 100644 --- a/public/main.js +++ b/public/main.js @@ -48,6 +48,23 @@ function updateTable() { getRequests(document.getElementById("count").value,allRequests); } +function applyUrlTransforms(url) { + console.log("Begin applyUrlTransforms:" + url); + if (url.match(/^https?:\/\/(www\.)?youtu(\.be|be\.com)\//)) { // Youtube + console.log("Youtube"); + var videoid = ""; + // youtu.be share url - Do not anchor to $ due to parameters such as ?t= + if (url.match(/^https?:\/\/youtu.be\/[A-Za-z0-9_-]{11}/)) { + videoid = url.match(/(?<=^https:\/\/youtu.be\/)[A-Za-z0-9_-]{11}/)[0]; + } else if (url.match(/^https?:\/\/(www\.)?youtube\.com\//)) { + videoid = url.match(/(?<=^https?:\/\/(www\.)?youtube\.com\/watch.*[?&]v=)[A-Za-z0-9_-]{11}/)[0]; + } + var result = 'https://www.youtube.com/watch?v=' + videoid; + console.log("Result: " + result); + return result; + } +} + function addRequestErr(msg) { document.getElementById('addRequestError').style.display = "inline-block"; document.getElementById('addRequestError').innerText = msg; @@ -142,7 +159,7 @@ const validUrlRegexes = [ ]; function validateAndSubmitRequest() { addRequestErrReset(); - var url = document.getElementById("addRequestUrl").value; + var url = applyUrlTransforms(document.getElementById("addRequestUrl").value); var validUrl = false; for (var regex of validUrlRegexes) { if (regex.test(url)) {