| 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 | + | // Display a message in the "alert" area |
| 23 | + | function error(text) { |
| 24 | + | const alertText = document.querySelector(".alert"); |
| 25 | + | alertText.innerText = text; |
| 26 | + | alertText.style.opacity = 1; |
| 27 | + | } |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | /******************************************************************************* |
| 32 | + | * Main UI Functions |
| 33 | + | ******************************************************************************/ |
| 34 | + | |
| 35 | + | async function onDecrypt() { |
| 36 | + | // Fail if the b64 library or API was not loaded |
| 37 | + | if (!("b64" in window && "apiVersions" in window)) { |
| 38 | + | error("Important libraries not loaded!"); |
| 39 | + | return; |
| 40 | + | } |
| 41 | + | |
| 42 | + | // Try to get page data from the URL if possible |
| 43 | + | const urlText = document.querySelector("#encrypted-url").value; |
| 44 | + | let url; |
| 45 | + | try { |
| 46 | + | url = new URL(urlText); |
| 47 | + | } catch { |
| 48 | + | error("Entered text is not a valid URL. Make sure it includes \"https://\" too!"); |
| 49 | + | return; |
| 50 | + | } |
| 51 | + | |
| 52 | + | let params; |
| 53 | + | try { |
| 54 | + | params = JSON.parse(b64.decode(url.hash.slice(1))); |
| 55 | + | } catch { |
| 56 | + | error("The link appears corrupted."); |
| 57 | + | return; |
| 58 | + | } |
| 59 | + | |
| 60 | + | // Check that all required parameters encoded in the URL are present |
| 61 | + | if (!("v" in params && "e" in params)) { |
| 62 | + | error("The link appears corrupted. The encoded URL is missing necessary parameters."); |
| 63 | + | return; |
| 64 | + | } |
| 65 | + | |
| 66 | + | // Check that the version in the parameters is valid |
| 67 | + | if (!(params["v"] in apiVersions)) { |
| 68 | + | error("Unsupported API version. The link may be corrupted."); |
| 69 | + | return; |
| 70 | + | } |
| 71 | + | |
| 72 | + | const api = apiVersions[params["v"]]; |
| 73 | + | |
| 74 | + | // Get values for decryption |
| 75 | + | const encrypted = b64.base64ToBinary(params["e"]); |
| 76 | + | const salt = "s" in params ? b64.base64ToBinary(params["s"]) : null; |
| 77 | + | const iv = "i" in params ? b64.base64ToBinary(params["i"]) : null; |
| 78 | + | |
| 79 | + | const password = document.querySelector("#password").value; |
| 80 | + | |
| 81 | + | // Decrypt if possible |
| 82 | + | let decrypted; |
| 83 | + | try { |
| 84 | + | decrypted = await api.decrypt(encrypted, password, salt, iv); |
| 85 | + | } catch { |
| 86 | + | error("Incorrect password!"); |
| 87 | + | return; |
| 88 | + | } |
| 89 | + | |
| 90 | + | // Print the decrypted link to the output area |
| 91 | + | document.querySelector("#output").value = decrypted; |
| 92 | + | error("Decrypted!"); |
| 93 | + | |
| 94 | + | // Update the "Open in New Tab" button to link to the correct place |
| 95 | + | document.querySelector("#open").href = decrypted; |
| 96 | + | } |
| 97 | + | |
| 98 | + | |
| 99 | + | // Activated when the "Copy" button is pressed |
| 100 | + | function onCopy(id) { |
| 101 | + | // Select and copy |
| 102 | + | const output = highlight(id); |
| 103 | + | document.execCommand("copy"); |
| 104 | + | |
| 105 | + | // Alert the user that the text was successfully copied |
| 106 | + | const alertArea = document.querySelector("#copy-alert"); |
| 107 | + | alertArea.innerText = `Copied ${output.value.length} characters`; |
| 108 | + | alertArea.style.opacity = "1"; |
| 109 | + | setTimeout(() => { alertArea.style.opacity = 0; }, 3000); |
| 110 | + | |
| 111 | + | // Deselect |
| 112 | + | output.selectionEnd = output.selectionStart; |
| 113 | + | output.blur(); |
| 114 | + | } |
| 115 | + | |