|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Image encoder</title> |
| 7 | + |
| 8 | + <div id="secret-container"> |
| 9 | + <input type="file" id="fileInput" accept="image/*"> |
| 10 | + <label id="hint">Select an image file to encode</label> |
| 11 | + <br> |
| 12 | + <label for="secretInput">Secret:</label> |
| 13 | + <input id="secretInput" placeholder="Enter your secret here"> |
| 14 | + </script> |
| 15 | + |
| 16 | + <hr> |
| 17 | + <div id="button-container"> |
| 18 | + <button id="encodeButton">Encode</button> |
| 19 | + <button id="decodeButton">Decode</button> |
| 20 | + </div> |
| 21 | + |
| 22 | + <div id="output"></div> |
| 23 | +</head> |
| 24 | + |
| 25 | +<style> |
| 26 | + body { |
| 27 | + font-family: Arial, sans-serif; |
| 28 | + margin: 20px; |
| 29 | + } |
| 30 | + #fileInput { |
| 31 | + display: block; |
| 32 | + margin-bottom: 10px; |
| 33 | + } |
| 34 | + #output img { |
| 35 | + max-width: 100%; |
| 36 | + height: auto; |
| 37 | + } |
| 38 | +</style> |
| 39 | + |
| 40 | +<body> |
| 41 | + <script type="module"> |
| 42 | + import init, { encode, decode } from './pkg/chaotic_enc.js'; |
| 43 | + |
| 44 | + await init(); // Initialize the WASM module |
| 45 | + const secretInput = document.getElementById('secretInput'); |
| 46 | + const fileInput = document.getElementById('fileInput'); |
| 47 | + const outputDiv = document.getElementById('output'); |
| 48 | + const hintLabel = document.getElementById('hint'); |
| 49 | + const encodeButton = document.getElementById('encodeButton'); |
| 50 | + const decodeButton = document.getElementById('decodeButton'); |
| 51 | + |
| 52 | + async function getInputBlob() { |
| 53 | + if (fileInput.files.length === 0) { |
| 54 | + throw new Error("No file selected"); |
| 55 | + } |
| 56 | + const file = fileInput.files[0]; |
| 57 | + const arrayBuffer = await file.arrayBuffer(); |
| 58 | + return new Uint8Array(arrayBuffer); |
| 59 | + } |
| 60 | + |
| 61 | + async function showImage(imBlob) { |
| 62 | + const blob = new Blob([imBlob], { type: 'image/png' }); |
| 63 | + const url = URL.createObjectURL(blob); |
| 64 | + |
| 65 | + const imgElem = document.createElement('img'); |
| 66 | + imgElem.src = url; |
| 67 | + imgElem.alt = 'Encoded Image'; |
| 68 | + outputDiv.innerHTML = ''; // Clear previous output |
| 69 | + outputDiv.appendChild(imgElem); |
| 70 | + } |
| 71 | + |
| 72 | + async function encode_image() { |
| 73 | + const inputBlob = await getInputBlob(); |
| 74 | + |
| 75 | + hintLabel.textContent = `Encoding...`; |
| 76 | + await new Promise(resolve => setTimeout(resolve, 0)); // Simulate processing delay |
| 77 | + |
| 78 | + let outputBlob; |
| 79 | + try { |
| 80 | + outputBlob = encode(inputBlob, secretInput.value); |
| 81 | + hintLabel.textContent = `Encode successfully!`; |
| 82 | + } |
| 83 | + catch (error) { |
| 84 | + hintLabel.textContent = `Error encoding: ${error.message}`; |
| 85 | + console.error(error); |
| 86 | + return; |
| 87 | + } |
| 88 | + await showImage(outputBlob); |
| 89 | + } |
| 90 | + |
| 91 | + async function decode_image() { |
| 92 | + const inputBlob = await getInputBlob(); |
| 93 | + |
| 94 | + hintLabel.textContent = `Decoding...`; |
| 95 | + await new Promise(resolve => setTimeout(resolve, 0)); // Simulate processing delay |
| 96 | + |
| 97 | + let outputBlob; |
| 98 | + try { |
| 99 | + outputBlob = decode(inputBlob, secretInput.value); |
| 100 | + hintLabel.textContent = `Decode successfully!`; |
| 101 | + } |
| 102 | + catch (error) { |
| 103 | + hintLabel.textContent = `Error decoding: ${error.message}`; |
| 104 | + console.error(error); |
| 105 | + return; |
| 106 | + } |
| 107 | + await showImage(outputBlob); |
| 108 | + } |
| 109 | + |
| 110 | + encodeButton.addEventListener('click', encode_image); |
| 111 | + decodeButton.addEventListener('click', decode_image); |
| 112 | + |
| 113 | + </script> |
| 114 | +</body> |
| 115 | +</html> |
0 commit comments