// popup.js const TEMPLATE_STORAGE_KEY = 'message_templates'; async function getTemplates() { try { const result = await browser.storage.local.get(TEMPLATE_STORAGE_KEY); return result[TEMPLATE_STORAGE_KEY] || []; } catch (error) { console.error("Error retrieving templates in popup:", error); return []; } } function insertTemplateAndClose(templateText) { browser.runtime.sendMessage({ action: 'insertTemplate', text: templateText }).catch(e => console.error("Error sending message to background:", e)); window.close(); } async function renderPopupButtons() { const templates = await getTemplates(); const templateList = document.getElementById('template-list'); templateList.innerHTML = ''; if (templates.length === 0) { templateList.innerHTML = `
Keine Vorlagen vorhanden.
Jetzt einrichten
`; document.getElementById('open-options').addEventListener('click', (e) => { e.preventDefault(); browser.runtime.openOptionsPage(); window.close(); }); return; } templates.forEach(template => { const button = document.createElement('button'); button.textContent = template.name; button.addEventListener('click', () => { insertTemplateAndClose(template.content); }); templateList.appendChild(button); }); // Footer with manage link const footer = document.createElement('div'); footer.className = 'footer'; const manageLink = document.createElement('a'); manageLink.href = "#"; manageLink.textContent = 'Vorlagen verwalten'; manageLink.addEventListener('click', (e) => { e.preventDefault(); browser.runtime.openOptionsPage(); window.close(); }); footer.appendChild(manageLink); templateList.parentNode.appendChild(footer); } document.addEventListener('DOMContentLoaded', renderPopupButtons);