]> git.ardis.lu - static-encrypt/commitdiff
Expand output section
authorArdis Lu <ardisglu@gmail.com>
Wed, 22 Feb 2023 05:08:21 +0000 (21:08 -0800)
committerArdis Lu <ardisglu@gmail.com>
Wed, 22 Feb 2023 05:09:33 +0000 (21:09 -0800)
index.html

index 642078bc4cd4d2b86c406db57aed153a18b82934..e681414c86ec859387fc009a719dedff6b52e9fe 100644 (file)
@@ -50,8 +50,8 @@
       margin-block-end: 16px;
     }
 
-    textarea {
-      block-size: 20rem;
+    output {
+      display: none;
     }
 
     button {
 
       <button name="encrypt">Encrypt</button>
 
-      <output name="output"></output>
+      <output id="output" name="output">
+        <label for="output">Encrypted content</label>
+        <pre id="ciphertext" readonly></pre>
+        <button id="download">Download HTML file</button>
+      </output>
     </form>
   </main>
 
       return content;
     }
 
+    function downloadHtml(htmlString, fileName) {
+      const encodedHtml = new TextEncoder().encode(htmlString);
+      const file = new File([encodedHtml], fileName, { type: 'text/html' });
+
+      const a = document.createElement('a');
+      a.href = URL.createObjectURL(file);
+      a.download = file.name;
+      a.click();
+    }
+
     const { default: decrypt } = await import('/decrypt.js');
     const template = await fetch('/template.html').then(r => r.text());
     document.querySelector('form').addEventListener('submit', async e => {
       const password = data.get('password');
 
       const encrypted = await encrypt(content, password);
+      document.querySelector('#output').style.display = 'block';
+      document.querySelector('#ciphertext').textContent = encrypted;
+    });
+
+    document.querySelector('#download').addEventListener('click', e => {
+      const encrypted = document.querySelector('#ciphertext').textContent;
       const scriptInjection = `<script>const content = '${encrypted}'; ${decrypt.toString()}<\/script>`;
       const output = template.replace('<!-- SCRIPT INJECTION -->', scriptInjection);
-      e.target.elements.output.textContent = output;
-    });
+      downloadHtml(output, `${Date.now()}.html`);
+    })
   </script>
 </body>
 
This page took 0.050345 seconds and 4 git commands to generate.