| document.addEventListener('DOMContentLoaded', function() { |
| |
| document.getElementById('encode-btn').addEventListener('click', function() { |
| const input = document.getElementById('encode-input').value; |
| if (!input) { |
| alert('Please enter a message to encode'); |
| return; |
| } |
| |
| let binaryString = ''; |
| for (let i = 0; i < input.length; i++) { |
| |
| const charCode = input.charCodeAt(i); |
| |
| let binary = charCode.toString(2).padStart(8, '0'); |
| |
| binary = binary.replace(/0/g, '2').replace(/1/g, '3'); |
| binaryString += binary + ' '; |
| } |
| |
| document.getElementById('encode-output').value = binaryString.trim(); |
| }); |
|
|
| |
| document.getElementById('decode-btn').addEventListener('click', function() { |
| const input = document.getElementById('decode-input').value.trim(); |
| if (!input) { |
| alert('Please enter a message to decode'); |
| return; |
| } |
| |
| |
| const chunks = input.split(' '); |
| let output = ''; |
| |
| for (const chunk of chunks) { |
| if (chunk.length !== 8) { |
| alert('Invalid encoded message. Each character should be represented by 8 digits (2s and 3s).'); |
| return; |
| } |
| |
| |
| const binary = chunk.replace(/2/g, '0').replace(/3/g, '1'); |
| |
| const charCode = parseInt(binary, 2); |
| |
| output += String.fromCharCode(charCode); |
| } |
| |
| document.getElementById('decode-output').value = output; |
| }); |
|
|
| |
| document.getElementById('copy-encode').addEventListener('click', function() { |
| const output = document.getElementById('encode-output'); |
| output.select(); |
| document.execCommand('copy'); |
| |
| |
| const originalText = this.innerHTML; |
| this.innerHTML = '<i data-feather="check" class="w-5 h-5"></i> Copied!'; |
| setTimeout(() => { |
| this.innerHTML = originalText; |
| feather.replace(); |
| }, 2000); |
| }); |
|
|
| document.getElementById('copy-decode').addEventListener('click', function() { |
| const output = document.getElementById('decode-output'); |
| output.select(); |
| document.execCommand('copy'); |
| |
| |
| const originalText = this.innerHTML; |
| this.innerHTML = '<i data-feather="check" class="w-5 h-5"></i> Copied!'; |
| setTimeout(() => { |
| this.innerHTML = originalText; |
| feather.replace(); |
| }, 2000); |
| }); |
|
|
| |
| feather.replace(); |
| }); |