| skipped 17 lines |
18 | 18 | | <!-- Scripts --> |
19 | 19 | | <script type="text/javascript" src="b64.js"></script> |
20 | 20 | | <script type="text/javascript" src="api.js"></script> |
21 | | - | <script type="text/javascript"> |
22 | | - | function error(text) { |
23 | | - | document.querySelector(".error").style.display = "inherit"; |
24 | | - | document.querySelector("#errortext").innerText = `Error: ${text}`; |
25 | | - | } |
26 | | - | |
27 | | - | // Run when the <body> loads |
28 | | - | async function main() { |
29 | | - | if (window.location.hash) { |
30 | | - | // Fail if the b64 library or API was not loaded |
31 | | - | if (!("b64" in window)) { |
32 | | - | error("Base64 library not loaded."); |
33 | | - | return; |
34 | | - | } |
35 | | - | if (!("apiVersions" in window)) { |
36 | | - | error("API library not loaded."); |
37 | | - | return; |
38 | | - | } |
39 | | - | |
40 | | - | // Try to get page data from the URL if possible |
41 | | - | const hash = window.location.hash.slice(1); |
42 | | - | let params; |
43 | | - | try { |
44 | | - | params = JSON.parse(b64.decode(hash)); |
45 | | - | } catch { |
46 | | - | error("The link appears corrupted."); |
47 | | - | return; |
48 | | - | } |
49 | | - | |
50 | | - | // Check that all required parameters encoded in the URL are present |
51 | | - | if (!("v" in params && "e" in params)) { |
52 | | - | error("The link appears corrupted. The encoded URL is missing necessary parameters."); |
53 | | - | return; |
54 | | - | } |
55 | | - | |
56 | | - | // Check that the version in the parameters is valid |
57 | | - | if (!(params["v"] in apiVersions)) { |
58 | | - | error("Unsupported API version. The link may be corrupted."); |
59 | | - | return; |
60 | | - | } |
61 | | - | |
62 | | - | const api = apiVersions[params["v"]]; |
63 | | - | |
64 | | - | // Get values for decryption |
65 | | - | const encrypted = b64.base64ToBinary(params["e"]); |
66 | | - | const salt = "s" in params ? b64.base64ToBinary(params["s"]) : null; |
67 | | - | const iv = "i" in params ? b64.base64ToBinary(params["i"]) : null; |
68 | | - | |
69 | | - | let hint, password; |
70 | | - | if ("h" in params) { |
71 | | - | hint = params["h"]; |
72 | | - | password = prompt(`Please enter the password to unlock the link.\n\nHint: ${hint}`); |
73 | | - | } else { |
74 | | - | password = prompt("Please enter the password to unlock the link."); |
75 | | - | } |
76 | | - | |
77 | | - | // Decrypt and redirect if possible |
78 | | - | let url; |
79 | | - | try { |
80 | | - | url = await api.decrypt(encrypted, password, salt, iv); |
81 | | - | } catch { |
82 | | - | // Password is incorrect. |
83 | | - | error("Password is incorrect."); |
84 | | - | return; |
85 | | - | } |
86 | | - | |
87 | | - | try { |
88 | | - | // Extra check to make sure the URL is valid. Probably shouldn't fail. |
89 | | - | new URL(url); |
90 | | - | // IMPORTANT NOTE: must use window.location.href instead of the (in |
91 | | - | // my opinion more proper) window.location.replace. If you use |
92 | | - | // replace, it causes Chrome to change the icon of a bookmarked link |
93 | | - | // to update it to the unlocked destination. |
94 | | - | window.location.href = url; |
95 | | - | } catch { |
96 | | - | error("A corrupted URL was encrypted. Cannot redirect."); |
97 | | - | console.log(url); |
98 | | - | return; |
99 | | - | } |
100 | | - | } else { |
101 | | - | // Otherwise redirect to the creator |
102 | | - | window.location.replace("./create"); |
103 | | - | } |
104 | | - | } |
105 | | - | </script> |
| 21 | + | <script type="text/javascript" src="index.js"> </script> |
106 | 22 | | </head> |
107 | 23 | | |
108 | 24 | | <body onload="main()"> |
| skipped 21 lines |