Files
manifest.json
popup.css
popup.html
popup.js
Code
4 305
popup.js
document.addEventListener('DOMContentLoaded', () => {
const noteInput = document.getElementById('noteInput');
const addNoteBtn = document.getElementById('addNoteBtn');
const noteList = document.getElementById('noteList');
const searchInput = document.getElementById('searchInput');
let notes = [];
function loadNotes() {
chrome.storage.local.get(['notes'], (result) => {
notes = result.notes || [];
renderNotes();
});
}
function saveNotes(callback) {
chrome.storage.local.set({ notes }, () => {
if (callback) callback();
});
}
function renderNotes() {
const query = searchInput.value.toLowerCase().trim();
const filtered = query
? notes.filter(n => n.text.toLowerCase().includes(query))
: notes;
if (filtered.length === 0) {
noteList.innerHTML = `
<div class="empty-state">
<p>${query ? 'Keine passenden Notizen gefunden.' : 'Noch keine Notizen.'}</p>
<p class="sub">${query ? 'Anderen Suchbegriff versuchen.' : 'Schreibe oben eine Notiz und klicke auf "Hinzufügen".'}</p>
</div>`;
return;
}
noteList.innerHTML = filtered
.map((note, index) => {
const realIndex = notes.indexOf(note);
const date = new Date(note.timestamp);
const formatted = date.toLocaleString('de-DE', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
return `
<div class="note-item">
<div class="note-text">${escapeHtml(note.text)}</div>
<div class="note-meta">
<span>${formatted}</span>
<button class="note-delete" data-index="${realIndex}">Löschen</button>
</div>
</div>`;
})
.join('');
document.querySelectorAll('.note-delete').forEach((btn) => {
btn.addEventListener('click', (e) => {
const index = parseInt(e.target.dataset.index, 10);
deleteNote(index);
});
});
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function addNote() {
const text = noteInput.value.trim();
if (!text) return;
notes.push({
text,
timestamp: Date.now(),
});
saveNotes(() => {
noteInput.value = '';
renderNotes();
});
}
function deleteNote(index) {
notes.splice(index, 1);
saveNotes(renderNotes);
}
addNoteBtn.addEventListener('click', addNote);
noteInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
addNote();
}
});
searchInput.addEventListener('input', renderNotes);
loadNotes();
});