| 1 | + | /** |
| 2 | + | * Created by Jacob Strieb |
| 3 | + | * May 2020 |
| 4 | + | */ |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | /******************************************************************************* |
| 9 | + | * Helper Functions |
| 10 | + | ******************************************************************************/ |
| 11 | + | |
| 12 | + | // Highlight the text in an input with a given id |
| 13 | + | function highlight(id) { |
| 14 | + | let output = document.querySelector("#" + id); |
| 15 | + | output.focus(); |
| 16 | + | output.select() |
| 17 | + | output.setSelectionRange(0, output.value.length + 1); |
| 18 | + | return output; |
| 19 | + | } |
| 20 | + | |
| 21 | + | |
| 22 | + | // Validate all inputs, and display an error if necessary |
| 23 | + | function validateInputs() { |
| 24 | + | var inputs = document.querySelectorAll(".form .labeled-input input"); |
| 25 | + | for (let i = 0; i < inputs.length; i++) { |
| 26 | + | let input = inputs[i]; |
| 27 | + | input.reportValidity = input.reportValidity || (() => true); |
| 28 | + | if (!input.reportValidity()) { |
| 29 | + | return false; |
| 30 | + | } |
| 31 | + | } |
| 32 | + | |
| 33 | + | // Extra check for older browsers for URL input. Not sure if necessary, since |
| 34 | + | // older browsers without built-in HTML5 validation may fail elsewhere. |
| 35 | + | let url = document.querySelector("#url"); |
| 36 | + | try { |
| 37 | + | new URL(url.value); |
| 38 | + | } catch { |
| 39 | + | if (!("reportValidity" in url)) { |
| 40 | + | alert("URL invalid. Make sure to include 'http://' at the beginning."); |
| 41 | + | } |
| 42 | + | return false; |
| 43 | + | } |
| 44 | + | |
| 45 | + | return true; |
| 46 | + | } |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | /******************************************************************************* |
| 51 | + | * Main UI Functions |
| 52 | + | ******************************************************************************/ |
| 53 | + | |
| 54 | + | // Activated when the "Advanced" dropdown is pressed |
| 55 | + | function onAdvanced() { |
| 56 | + | let label = document.querySelector("#advanced-label"); |
| 57 | + | let advanced = document.querySelector(".advanced"); |
| 58 | + | if (advanced.style.display == "none" || advanced.style.display == "") { |
| 59 | + | label.innerHTML = "▾ advanced"; |
| 60 | + | advanced.style.display = "flex"; |
| 61 | + | } else { |
| 62 | + | label.innerHTML = "▸ advanced"; |
| 63 | + | advanced.style.display = "none"; |
| 64 | + | } |
| 65 | + | } |
| 66 | + | |
| 67 | + | |
| 68 | + | // Activated when the "Encrypt" button is pressed |
| 69 | + | function onEncrypt() { |
| 70 | + | if (!validateInputs()) { |
| 71 | + | return; |
| 72 | + | } |
| 73 | + | |
| 74 | + | // Check that password is successfully confirmed |
| 75 | + | const password = document.querySelector("#password").value; |
| 76 | + | const confirmPassword = document.querySelector("#confirm-password") |
| 77 | + | const confirmation = confirmPassword.value; |
| 78 | + | if (password != confirmation) { |
| 79 | + | confirmPassword.setCustomValidity("Passwords do not match"); |
| 80 | + | confirmPassword.reportValidity(); |
| 81 | + | return; |
| 82 | + | } |
| 83 | + | |
| 84 | + | // Initialize values for encryption |
| 85 | + | const url = document.querySelector("#url").value; |
| 86 | + | // TODO: Finish this |
| 87 | + | |
| 88 | + | const output = "foobar"; |
| 89 | + | document.querySelector("#output").value = output; |
| 90 | + | highlight("output"); |
| 91 | + | } |
| 92 | + | |
| 93 | + | |
| 94 | + | // Activated when the "Copy" button is pressed |
| 95 | + | function onCopy(id) { |
| 96 | + | // Select and copy |
| 97 | + | const output = highlight(id); |
| 98 | + | document.execCommand("copy"); |
| 99 | + | |
| 100 | + | // Alert the user that the text was successfully copied |
| 101 | + | const alertArea = document.querySelector(".alert"); |
| 102 | + | alertArea.innerText = `Copied ${output.value.length} characters`; |
| 103 | + | alertArea.style.opacity = "1"; |
| 104 | + | setTimeout(() => { alertArea.style.opacity = 0; }, 3000); |
| 105 | + | |
| 106 | + | // Deselect |
| 107 | + | output.selectionEnd = output.selectionStart; |
| 108 | + | output.blur(); |
| 109 | + | } |
1 | 110 | | |