Projects STRLCPY link-lock Commits e15798de
🤬
  • ■ ■ ■ ■ ■
    index.html
    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
  • ■ ■ ■ ■ ■ ■
    index.js
     1 +function error(text) {
     2 + document.querySelector(".error").style.display = "inherit";
     3 + document.querySelector("#errortext").innerText = `Error: ${text}`;
     4 +}
     5 + 
     6 +// Run when the <body> loads
     7 +async function main() {
     8 + if (window.location.hash) {
     9 + // Fail if the b64 library or API was not loaded
     10 + if (!("b64" in window)) {
     11 + error("Base64 library not loaded.");
     12 + return;
     13 + }
     14 + if (!("apiVersions" in window)) {
     15 + error("API library not loaded.");
     16 + return;
     17 + }
     18 + 
     19 + // Try to get page data from the URL if possible
     20 + const hash = window.location.hash.slice(1);
     21 + let params;
     22 + try {
     23 + params = JSON.parse(b64.decode(hash));
     24 + } catch {
     25 + error("The link appears corrupted.");
     26 + return;
     27 + }
     28 + 
     29 + // Check that all required parameters encoded in the URL are present
     30 + if (!("v" in params && "e" in params)) {
     31 + error("The link appears corrupted. The encoded URL is missing necessary parameters.");
     32 + return;
     33 + }
     34 + 
     35 + // Check that the version in the parameters is valid
     36 + if (!(params["v"] in apiVersions)) {
     37 + error("Unsupported API version. The link may be corrupted.");
     38 + return;
     39 + }
     40 + 
     41 + const api = apiVersions[params["v"]];
     42 + 
     43 + // Get values for decryption
     44 + const encrypted = b64.base64ToBinary(params["e"]);
     45 + const salt = "s" in params ? b64.base64ToBinary(params["s"]) : null;
     46 + const iv = "i" in params ? b64.base64ToBinary(params["i"]) : null;
     47 + 
     48 + let hint, password;
     49 + if ("h" in params) {
     50 + hint = params["h"];
     51 + password = prompt(`Please enter the password to unlock the link.\n\nHint: ${hint}`);
     52 + } else {
     53 + password = prompt("Please enter the password to unlock the link.");
     54 + }
     55 + 
     56 + // Decrypt and redirect if possible
     57 + let url;
     58 + try {
     59 + url = await api.decrypt(encrypted, password, salt, iv);
     60 + } catch {
     61 + // Password is incorrect.
     62 + error("Password is incorrect.");
     63 + return;
     64 + }
     65 + 
     66 + try {
     67 + // Extra check to make sure the URL is valid. Probably shouldn't fail.
     68 + new URL(url);
     69 + // IMPORTANT NOTE: must use window.location.href instead of the (in my
     70 + // opinion more proper) window.location.replace. If you use replace, it
     71 + // causes Chrome to change the icon of a bookmarked link to update it to
     72 + // the unlocked destination.
     73 + window.location.href = url;
     74 + } catch {
     75 + error("A corrupted URL was encrypted. Cannot redirect.");
     76 + console.log(url);
     77 + return;
     78 + }
     79 + } else {
     80 + // Otherwise redirect to the creator
     81 + window.location.replace("./create");
     82 + }
     83 +}
     84 + 
Please wait...
Page is in error, reload to recover