Simulated Alien Translator

Translate human text to see alien output...

Translate alien output to see human interpretation...

Translate alien output to see human interpretation...

'; // Enable translate to human button translateToHumanBtn.disabled = false; // Download PDF is enabled after both translations are done } function processBackTranslation() { const alienText = alienOutputContent.textContent.trim(); if (alienText === "" || alienText === "Translate human text to see alien output...") { statusMessage.textContent = 'Generate alien output first.'; humanOutputContent.innerHTML = '

Translate alien output to see human interpretation...

'; downloadPdfBtn.disabled = true; return; } statusMessage.textContent = 'Translating back to Human...'; const humanTranslation = alienToHuman(alienText); humanOutputContent.innerHTML = `

${humanTranslation}

`; statusMessage.textContent = 'Human translation generated.'; // Enable download PDF button downloadPdfBtn.disabled = false; translateToHumanBtn.disabled = true; // Disable back-translation after one attempt } function downloadPdf() { if (downloadPdfBtn.disabled || humanInput.value.trim() === "" || alienOutputContent.textContent.trim() === "Translate human text to see alien output..." || humanOutputContent.textContent.trim() === "Translate alien output to see human interpretation...") { alert("Generate both translations first!"); return; } const { jsPDF } = window.jspdf; const doc = new jsPDF({ orientation: 'P', // Portrait unit: 'mm', format: 'a4' }); // Target the entire translator container for PDF const element = document.getElementById('alienTranslatorSimulator'); // Apply temporary print styles for PDF generation (cleaner output) const pdfStyles = ` #alienTranslatorSimulator { background-color: #ffffff !important; /* White background for print */ color: #000 !important; /* Black text */ box-shadow: none !important; border: none !important; padding: 10mm !important; /* Use mm for consistency */ margin: 0 !important; font-size: 10pt !important; line-height: 1.5 !important; font-family: sans-serif !important; /* Use standard font */ } #alienTranslatorSimulator h2, #alienTranslatorSimulator label { /* Print label as h3 approximation */ color: #000 !important; /* Black headings and labels */ text-shadow: none !important; font-family: sans-serif !important; } #alienTranslatorSimulator h2 { font-size: 14pt !important; margin-bottom: 10mm !important; } #alienTranslatorSimulator label { font-size: 12pt !important; font-weight: bold !important; margin-bottom: 5mm !important; } #alienTranslatorSimulator .input-section, #alienTranslatorSimulator .controls-section, #alienTranslatorSimulator .message-area { /* Hide controls, input, and message area */ display: none !important; } #alienTranslatorSimulator .output-section { border: none !important; background-color: #fff !important; padding: 0 !important; } #alienTranslatorSimulator .output-group { margin-bottom: 10mm !important; /* Space between alien and human output */ } #alienTranslatorSimulator .output-box { color: #000 !important; /* Black text for print */ border: 1px solid #ccc !important; /* Light gray border */ background-color: #fff !important; padding: 5mm !important; min-height: auto !important; white-space: pre-wrap !important; overflow-x: visible !important; font-size: 10pt !important; font-family: sans-serif !important; /* Use standard font */ } #alienTranslatorSimulator #alienOutputContent { color: #333 !important; /* Darker gray for alien text in print */ font-family: monospace !important; /* Keep monospace for alien text */ } #alienTranslatorSimulator #humanOutputContent { color: #000 !important; /* Black for human text in print */ font-family: sans-serif !important; /* Keep sans-serif */ } #alienTranslatorSimulator #alienOutputContent p, #alienTranslatorSimulator #humanOutputContent p { margin: 0 !important; padding: 0 !important; } `; const styleTag = document.createElement('style'); styleTag.setAttribute('id', 'pdfPrintStyles'); styleTag.innerHTML = pdfStyles; document.head.appendChild(styleTag); // Use html2canvas to render the element as an image/canvas html2canvas(element, { scale: 2, // Increase scale for better resolution in PDF logging: false, // Disable html2canvas logging backgroundColor: '#ffffff' // Explicitly set white background }).then(canvas => { const imgData = canvas.toDataURL('image/png'); const pdfWidth = doc.internal.pageSize.getWidth(); const pdfHeight = doc.internal.pageSize.getHeight(); const imgWidth = pdfWidth - 20; // Leave 10mm margin on each side const imgHeight = canvas.height * imgWidth / canvas.width; let heightLeft = imgHeight; let position = 10; // Starting Y position (10mm margin) // Add the image to the PDF const imgX = (pdfWidth - imgWidth) / 2; // Center the image doc.addImage(imgData, 'PNG', imgX, position, imgWidth, imgHeight); heightLeft -= (pdfHeight - position); // Handle multiple pages if content exceeds one page while (heightLeft >= 0) { position = 10; // Reset position for new page doc.addPage(); // Add background to the new page doc.setFillColor(255); // White doc.rect(0, 0, pdfWidth, pdfHeight, 'F'); // Calculate the source y position on the canvas image const srcY = imgHeight - heightLeft; // Add the remaining part of the image doc.addImage(imgData, 'PNG', imgX, position, imgWidth, heightLeft, null, null, 0, srcY); heightLeft -= (pdfHeight - position); } // Add a title specifically for the PDF doc.setFontSize(18); doc.setTextColor(50, 50, 50); // Dark Gray doc.text("Alien Translator Transcript", pdfWidth / 2, 8, { align: 'center' }); // Remove the temporary print styles const addedTag = document.getElementById('pdfPrintStyles'); if (addedTag) { addedTag.remove(); } doc.save('alien_translator_transcript.pdf'); }).catch(error => { console.error("Error generating PDF:", error); alert("Could not generate PDF. Please try again."); // Ensure styles are removed even on error const addedTag = document.getElementById('pdfPrintStyles'); if (addedTag) { addedTag.remove(); } }); } // Event Listeners translateToAlienBtn.addEventListener('click', processTranslation); translateToHumanBtn.addEventListener('click', processBackTranslation); downloadPdfBtn.addEventListener('click', downloadPdf); // Initial state text alienOutputContent.innerHTML = '

Translate human text to see alien output...

'; humanOutputContent.innerHTML = '

Translate alien output to see human interpretation...

'; // Ensure buttons are disabled initially translateToHumanBtn.disabled = true; downloadPdfBtn.disabled = true; });
Scroll to Top