`;
const systemPrompt = "You are an expert affiliate marketer and direct-response copywriter. Your task is to generate 3 to 4 distinct product descriptions based on the provided details. Each description should have a clear style or angle. Respond only with a valid JSON object matching the provided schema.";
const userPrompt = `Product Name: "${inputsData.name}", Key Features: "${inputsData.features}", Target Audience: "${inputsData.audience}", Tone: "${inputsData.tone}"`;
const jsonSchema = {
type: "OBJECT",
properties: {
descriptions: {
type: "ARRAY",
items: {
type: "OBJECT",
properties: {
style: { type: "STRING" },
content: { type: "STRING" }
},
required: ["style", "content"]
}
}
},
required: ["descriptions"]
};
const apiKey = "";
const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=${apiKey}`;
const payload = {
contents: [{ parts: [{ text: userPrompt }] }],
systemInstruction: { parts: [{ text: systemPrompt }] },
generationConfig: { responseMimeType: "application/json", responseSchema: jsonSchema }
};
try {
const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) });
if (!response.ok) throw new Error(`API Error: ${response.statusText}`);
const result = await response.json();
const aiResult = JSON.parse(result.candidates?.[0]?.content?.parts?.[0]?.text);
if (aiResult.descriptions && aiResult.descriptions.length > 0) {
generatedDescriptions = aiResult.descriptions;
renderDescriptions();
} else {
throw new Error("No descriptions were generated.");
}
} catch (error) {
console.error("Content generation error:", error);
resultsContainer.innerHTML = ``;
showNotification("Error generating content.");
} finally {
generateBtn.disabled = false;
generateBtn.textContent = 'Generate Descriptions';
}
}
function renderDescriptions() {
resultsContainer.innerHTML = generatedDescriptions.map((desc, index) => `
`).join('');
}
// --- UTILITY FUNCTIONS ---
function showNotification(message) {
notification.textContent = message;
notification.classList.remove('opacity-0');
setTimeout(() => notification.classList.add('opacity-0'), 2000);
}
function copyToClipboard(text, message) {
navigator.clipboard.writeText(text).then(() => showNotification(message));
}
// --- PDF GENERATION ---
async function generatePdfReport() {
if (generatedDescriptions.length === 0) {
showNotification("No descriptions to export.");
return;
}
downloadPdfBtn.disabled = true;
downloadPdfBtn.textContent = '...';
const descriptionCards = generatedDescriptions.map(desc => `
`).join('');
const reportHtml = `
`;
const pdfTemplate = document.getElementById('pdf-template');
pdfTemplate.innerHTML = reportHtml;
pdfTemplate.classList.remove('invisible');
try {
const { jsPDF } = window.jspdf;
const canvas = await html2canvas(pdfTemplate.querySelector('.pdf-page'), { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth(), pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
pdf.save(`Product_Descriptions_${inputsData.name.replace(/\s+/g, '_')}.pdf`);
} catch (e) { console.error('PDF Generation Error:', e); } finally {
downloadPdfBtn.disabled = false;
downloadPdfBtn.textContent = 'Download PDF';
pdfTemplate.classList.add('invisible');
pdfTemplate.innerHTML = '';
}
}
// --- EVENT LISTENERS ---
generateBtn.addEventListener('click', generateDescriptions);
copyAllBtn.addEventListener('click', () => {
if (generatedDescriptions.length > 0) {
const allText = generatedDescriptions.map(d => `--- ${d.style} ---\n${d.content}`).join('\n\n');
copyToClipboard(allText, 'All descriptions copied!');
}
});
resultsContainer.addEventListener('click', (e) => {
if (e.target.classList.contains('copy-desc-btn')) {
const text = generatedDescriptions[e.target.dataset.index].content;
copyToClipboard(text, 'Description copied!');
}
});
downloadPdfBtn.addEventListener('click', generatePdfReport);
});
An error occurred: ${error.message}
${desc.style}
${desc.content}
${desc.style}
${desc.content.replace(/\n/g, '
')}
Affiliate Content Sheet
Product: ${inputsData.name}
Key Features & Audience
Features: ${inputsData.features.replace(/\n/g, ', ')}
Audience: ${inputsData.audience}
Generated Descriptions
${descriptionCards}
