-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
64 lines (55 loc) · 2.45 KB
/
options.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
// Export Notes Functionality
document.getElementById('exportButton').addEventListener('click', () => {
chrome.storage.local.get({ notes: [] }, (data) => {
const notes = data.notes;
const blob = new Blob([JSON.stringify(notes, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
// Create a download link and click it to download the file
const a = document.createElement('a');
a.href = url;
a.download = 'notes_export.json';
a.click();
URL.revokeObjectURL(url); // Clean up the object URL after download
});
});
// Import Notes Functionality
document.getElementById('importButton').addEventListener('click', () => {
document.getElementById('importInput').click();
});
document.getElementById('importInput').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);
console.log("Imported Notes from File:", importedNotes); // Debugging line
// Validate that imported data is an array of notes
if (Array.isArray(importedNotes)) {
chrome.storage.local.get({ notes: [] }, (data) => {
const existingNotes = data.notes;
const existingIds = new Set(existingNotes.map(note => note.id));
// Filter out duplicates from imported notes based on unique identifiers
const uniqueImportedNotes = importedNotes.filter(note =>
!existingIds.has(note.id) &&
!existingNotes.some(existingNote =>
existingNote.title === note.title && existingNote.link === note.link
)
);
// Concatenate unique imported notes with existing notes
const updatedNotes = existingNotes.concat(uniqueImportedNotes);
chrome.storage.local.set({ notes: updatedNotes }, () => {
alert("Notes imported successfully without duplicates!");
});
});
} else {
alert("Invalid file format. Please upload a valid JSON file.");
}
} catch (error) {
console.error("Error parsing imported JSON:", error); // Debugging line
alert("Failed to import notes. Invalid JSON format.");
}
};
reader.readAsText(file);
}
});