-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
226 lines (192 loc) · 6.95 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
let allNotes = []; // Store all notes globally
// Utility function to escape special characters for regex
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// Function to highlight matching text
function highlightText(text, query = '') {
if (!query) return text;
const regex = new RegExp(`(${escapeRegExp(query)})`, 'gi');
return text.replace(regex, '<mark>$1</mark>');
}
// **Wrap event listener attachments inside DOMContentLoaded**
document.addEventListener('DOMContentLoaded', () => {
// Event listener for saveButton
document.getElementById('saveButton').addEventListener('click', () => {
// Send a message to the content script to get the post data
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
try {
const results = await chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: getPostData,
});
if (results && results[0] && results[0].result) {
saveNote(results[0].result);
} else {
alert('Failed to retrieve post data.');
}
} catch (error) {
console.error('Error executing script:', error);
}
});
});
// Event listener for deleteAllButton
document.getElementById('deleteAllButton').addEventListener('click', () => {
// Show the custom confirmation modal
document.getElementById('confirmationModal').style.display = 'block';
});
// Event listener for the confirm delete button
document.getElementById('confirmDeleteButton').addEventListener('click', async () => {
try {
// Clear all notes
allNotes = [];
await chrome.storage.local.set({ notes: allNotes });
console.log("All notes have been deleted.");
// Verify storage after deletion
let data = await chrome.storage.local.get('notes');
console.log("Notes in storage after deletion:", data.notes);
displayNotes();
// Hide the modal
document.getElementById('confirmationModal').style.display = 'none';
} catch (error) {
console.error('Error deleting all notes:', error);
}
});
// Event listener for the cancel delete button
document.getElementById('cancelDeleteButton').addEventListener('click', () => {
// Hide the modal
document.getElementById('confirmationModal').style.display = 'none';
});
// Search Input Event Listener
document.getElementById('searchInput').addEventListener('input', debounce(handleSearchInput, 300));
// Load notes when popup opens
loadNotes();
});
// Debounce function to limit search input processing
function debounce(func, delay) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), delay);
};
}
// Handle search input
function handleSearchInput() {
const query = document.getElementById('searchInput').value.toLowerCase();
const filteredNotes = allNotes.filter((note) => {
return note.title.toLowerCase().includes(query) || note.content.toLowerCase().includes(query);
});
displayNotes(filteredNotes, query);
}
// Function to save note to Chrome storage
async function saveNote(note) {
try {
// Retrieve existing notes from storage
let data = await chrome.storage.local.get({ notes: [] });
// Ensure data.notes is an array
if (!Array.isArray(data.notes)) {
data.notes = [];
}
allNotes = data.notes;
// Check for duplicates based on the note's link
const isDuplicate = allNotes.some(existingNote => existingNote.link === note.link);
if (isDuplicate) {
alert('This note already exists.');
return;
}
// Assign a unique ID to the note
note.id = Date.now();
// Add the new note to the existing notes
allNotes.push(note);
// Save the updated notes back to storage
await chrome.storage.local.set({ notes: allNotes });
console.log("Note saved successfully!");
displayNotes();
} catch (error) {
console.error('Error saving note:', error);
}
}
// Function to display notes with optional filtering
function displayNotes(filteredNotes = null, query = '') {
const notesToDisplay = filteredNotes !== null ? filteredNotes : allNotes;
if (!Array.isArray(notesToDisplay)) {
console.error('notesToDisplay is not an array:', notesToDisplay);
return;
}
const notesDiv = document.getElementById('notes');
notesDiv.innerHTML = '';
notesToDisplay.forEach((note) => {
const noteDiv = document.createElement('div');
noteDiv.className = 'note';
// Note content container
const noteContentDiv = document.createElement('div');
noteContentDiv.className = 'noteContent';
noteContentDiv.innerHTML = `<div>
<strong>${highlightText(note.title, query)}</strong>
</div><br>
<a href="${note.link}" target="_blank">Go to Post</a>
`;
// Make note clickable to open in new window
noteContentDiv.style.cursor = 'pointer';
noteContentDiv.addEventListener('click', () => {
openNoteInWindow(note);
});
// Create delete button (minus sign)
const deleteButton = document.createElement('button');
deleteButton.className = 'deleteButton';
deleteButton.textContent = '-';
deleteButton.addEventListener('click', () => {
deleteNoteById(note.id);
});
noteDiv.appendChild(noteContentDiv);
noteDiv.appendChild(deleteButton);
notesDiv.appendChild(noteDiv);
});
}
// Function to load notes from storage
async function loadNotes() {
try {
let data = await chrome.storage.local.get({ notes: [] });
if (!Array.isArray(data.notes)) {
console.error('data.notes is not an array, resetting to empty array.');
data.notes = [];
await chrome.storage.local.set({ notes: data.notes });
}
allNotes = data.notes;
displayNotes();
} catch (error) {
console.error('Error loading notes:', error);
}
}
// Function to delete a note by unique ID
async function deleteNoteById(noteId) {
try {
allNotes = allNotes.filter((note) => note.id !== noteId);
await chrome.storage.local.set({ notes: allNotes });
displayNotes();
} catch (error) {
console.error('Error deleting note:', error);
}
}
// Function to open note in new window (like Sticky Notes)
function openNoteInWindow(note) {
const url = chrome.runtime.getURL('note.html') +
`?title=${encodeURIComponent(note.title)}&content=${encodeURIComponent(note.content)}&link=${encodeURIComponent(note.link)}&id=${note.id}`;
chrome.windows.create({
url: url,
type: 'popup',
width: 450,
height: 600,
});
}
// Content script function to get post data from Reddit
function getPostData() {
const titleElement = document.querySelector('h1');
const contentElement = document.querySelector('[slot="text-body"]');
const title = titleElement ? titleElement.innerText : 'No Title Found';
const content = contentElement ? contentElement.innerHTML : 'No Content Found';
const link = window.location.href;
if (link.includes('reddit.com')) {
return { title, content, link };
}
}