`;
// Create download link
const blob = new Blob([ebookHTML], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${book.title.replace(/[^a-z0-9]/gi, '_').toLowerCase()}.html`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Print/PDF functionality
printBtn.addEventListener('click', () => {
if (book.chapters.length === 0) {
alert('Please add at least one chapter before printing.');
return;
}
// Create print content
let printHTML = `
Chapter ${index + 1}: ${chapter.title}
${chapter.content.replace(/\n/g, '
')}
`;
});
printHTML += `
`;
printSection.innerHTML = printHTML;
printSection.style.display = 'block';
window.print();
printSection.style.display = 'none';
});
// Clear all content
clearBtn.addEventListener('click', () => {
if (confirm('Are you sure you want to clear all content?')) {
bookTitleInput.value = 'My Awesome E-book';
authorNameInput.value = 'Anonymous';
chapterTitleInput.value = '';
chapterContentInput.value = '';
book.chapters = [];
updateChapterList();
updatePreview();
}
});
// Update preview when metadata changes
bookTitleInput.addEventListener('input', updatePreview);
authorNameInput.addEventListener('input', updatePreview);
// Initialize
updateChapterList();
});