Files
background.js
content.css
content.js
manifest.json
popup.css
popup.html
popup.js
Code
7 391
popup.js
const selectionElement = document.getElementById('selection');
const saveButton = document.getElementById('save-selection');
const statusElement = document.getElementById('status');
const notesElement = document.getElementById('notes');
let selectedText = '';
async function loadSelection() {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab || !tab.id) {
selectionElement.textContent = 'Select text on a webpage to use it here.';
return;
}
const response = await chrome.tabs.sendMessage(tab.id, { type: 'GET_SELECTION' });
selectedText = response?.text?.trim() || '';
selectionElement.textContent = selectedText || 'Select text on the page, then open this popup.';
saveButton.disabled = !selectedText;
} catch (error) {
console.error('Failed to read selected page text:', error);
selectionElement.textContent = 'Selection is unavailable on this page. Try a regular website.';
}
}
async function loadNotes() {
try {
const { studyNotes = [] } = await chrome.storage.local.get('studyNotes');
renderNotes(Array.isArray(studyNotes) ? studyNotes : []);
} catch (error) {
console.error('Failed to load study notes:', error);
notesElement.textContent = 'Could not load saved notes.';
}
}
function renderNotes(notes) {
notesElement.replaceChildren();
if (notes.length === 0) {
const emptyMessage = document.createElement('p');
emptyMessage.className = 'empty';
emptyMessage.textContent = 'No notes saved yet.';
notesElement.append(emptyMessage);
return;
}
for (const note of notes) {
const container = document.createElement('article');
container.className = 'note';
const text = document.createElement('p');
text.className = 'note-text';
text.textContent = note.text;
const source = document.createElement('span');
source.className = 'note-source';
source.textContent = note.title || note.url || 'Saved note';
const removeButton = document.createElement('button');
removeButton.className = 'delete-note';
removeButton.type = 'button';
removeButton.textContent = 'Delete';
removeButton.addEventListener('click', () => deleteNote(note.id));
container.append(text, source, removeButton);
notesElement.append(container);
}
}
async function saveNote(text) {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const { studyNotes = [] } = await chrome.storage.local.get('studyNotes');
const notes = Array.isArray(studyNotes) ? studyNotes : [];
notes.unshift({
id: `${Date.now()}-${Math.random().toString(36).slice(2)}`,
text,
title: tab?.title || '',
url: tab?.url || '',
createdAt: Date.now()
});
await chrome.storage.local.set({ studyNotes: notes });
await loadNotes();
}
async function deleteNote(id) {
try {
const { studyNotes = [] } = await chrome.storage.local.get('studyNotes');
const notes = Array.isArray(studyNotes) ? studyNotes : [];
await chrome.storage.local.set({
studyNotes: notes.filter(note => note.id !== id)
});
await loadNotes();
} catch (error) {
console.error('Failed to delete study note:', error);
}
}
saveButton.addEventListener('click', async () => {
try {
if (!selectedText) return;
await saveNote(selectedText);
statusElement.textContent = 'Note saved.';
} catch (error) {
console.error('Failed to save selected text:', error);
statusElement.textContent = 'Could not save the note.';
}
});
loadSelection();
loadNotes();