-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.js
168 lines (143 loc) · 7.17 KB
/
dashboard.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
document.addEventListener('DOMContentLoaded', function () {
loadAllNotes();
document.getElementById('exportButton').addEventListener('click', () => {
chrome.storage.sync.get(['allNotes'], (result) => {
const notes = result.allNotes || [];
const dataStr = JSON.stringify(notes, null, 2);
const blob = new Blob([dataStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'notes.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
});
document.getElementById('importButton').addEventListener('click', () => {
document.getElementById('importFile').click();
});
document.getElementById('importFile').addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
try {
const importedNotes = JSON.parse(e.target.result);
importNotes(importedNotes);
} catch (error) {
alert('Failed to import notes: Invalid JSON format');
}
};
reader.readAsText(file);
}
});
function importNotes(notes) {
if (!Array.isArray(notes)) {
alert('Invalid notes format.');
return;
}
chrome.storage.sync.get(['allNotes'], (result) => {
const existingNotes = result.allNotes || [];
const newNotes = existingNotes.concat(notes);
chrome.storage.sync.set({ allNotes: newNotes }, () => {
loadAllNotes();
alert('Notes imported successfully!');
});
});
}
function loadAllNotes(searchTerm = '') {
chrome.storage.sync.get(['allNotes'], (result) => {
const notes = result.allNotes || [];
const allNotesListDiv = document.getElementById('allNotesList');
allNotesListDiv.innerHTML = '';
const filteredNotes = notes.filter(noteData =>
noteData.note.toLowerCase().includes(searchTerm.toLowerCase()) ||
noteData.websiteTitle.toLowerCase().includes(searchTerm.toLowerCase()) ||
noteData.websiteURL.toLowerCase().includes(searchTerm.toLowerCase())
);
if (filteredNotes.length === 0) {
allNotesListDiv.innerHTML = '<p>No notes found.</p>';
return;
}
filteredNotes.reverse();
filteredNotes.forEach((noteData, index) => {
const noteItem = document.createElement('div');
noteItem.className = 'noteItem';
noteItem.innerHTML = `
<strong>Note ${index + 1}</strong>
<div><strong>Website Name:</strong> ${noteData.websiteTitle || 'Unknown'}</div>
<div><strong>Website URL:</strong> <a href="${noteData.websiteURL || '#'}" target="_blank">${noteData.websiteURL || 'No URL'}</a></div>
<div>${noteData.dateTime || 'No date available'}</div>
<div>${noteData.note || 'No content'}</div>
<button class="editButton" data-index="${index}">Edit</button>
<button class="removeButton" data-index="${index}">Remove</button>
<div class="editContainer" style="display: none;"></div>
`;
allNotesListDiv.appendChild(noteItem);
// Event listeners for editing and removing notes
noteItem.querySelector('.removeButton').addEventListener('click', function () {
const confirmRemove = confirm('Are you sure you want to remove this note?');
if (confirmRemove) {
chrome.storage.sync.get(['allNotes'], (result) => {
const notes = result.allNotes || [];
notes.splice(index, 1);
chrome.storage.sync.set({ allNotes: notes }, () => {
loadAllNotes(searchTerm);
});
});
}
});
noteItem.querySelector('.editButton').addEventListener('click', function () {
const editContainer = noteItem.querySelector('.editContainer');
editContainer.innerHTML = `
<textarea id="noteText${index}" rows="4" style="width: 100%;">${noteData.note}</textarea>
<button class="saveNote" data-index="${index}">Save</button>
<button class="cancelEdit">Cancel</button>
`;
editContainer.style.display = 'block';
editContainer.querySelector('.saveNote').addEventListener('click', function () {
const newNote = document.getElementById(`noteText${index}`).value;
chrome.storage.sync.get(['allNotes'], (result) => {
const notes = result.allNotes || [];
notes[index].note = newNote;
chrome.storage.sync.set({ allNotes: notes }, () => {
loadAllNotes(searchTerm);
});
});
});
editContainer.querySelector('.cancelEdit').addEventListener('click', function () {
editContainer.style.display = 'none';
});
});
});
});
}
document.getElementById('saveNote').addEventListener('click', () => {
const note = document.getElementById('noteText').value.trim();
const websiteTitle = document.getElementById('websiteTitle').textContent;
const websiteURL = document.getElementById('websiteURL').textContent;
const dateTime = new Date().toLocaleString();
if (note) {
chrome.storage.sync.get(['allNotes'], (result) => {
const notes = result.allNotes || [];
notes.push({ note, websiteTitle, websiteURL, dateTime });
chrome.storage.sync.set({ allNotes: notes }, () => {
loadAllNotes();
document.getElementById('noteText').value = '';
});
});
} else {
alert('Please enter a note before saving.');
}
});
document.getElementById('searchButton').addEventListener('click', () => {
const searchTerm = document.getElementById('searchInput').value;
loadAllNotes(searchTerm);
});
document.getElementById('clearSearch').addEventListener('click', () => {
document.getElementById('searchInput').value = '';
loadAllNotes();
});
});