prompt_template/web/js/utils.js

48 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

/**
* Utility-Funktionen für das Prompt Templates Frontend.
*/
/**
* XSS-Schutz: Escaped HTML-Special-Chars
* @param {string} s - Zu escapender String
* @returns {string} Escaped String
*/
function esc(s) {
const d = document.createElement('div');
d.textContent = s == null ? '' : String(s);
return d.innerHTML;
}
/**
* Zeige Toast-Nachricht für 3 Sekunden
* @param {string} message - Nachrichtentext
*/
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 3000);
}
/**
* Inhalt in die Clipboard kopieren
* @param {string} content - Zu kopierender Inhalt
*/
async function copyContentToClipboard(content) {
try {
await navigator.clipboard.writeText(content);
showToast('✓ Inhalt kopiert');
} catch (e) {
// Fallback für ältere Browser
const textarea = document.createElement('textarea');
textarea.value = content;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
showToast('✓ Inhalt kopiert');
}
}
// exported functions are global (loaded as <script> tags)