-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (54 loc) · 1.6 KB
/
index.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
let myNotes = [];
const inputEl = document.getElementById("input-el");
const ulEl = document.getElementById("ul-el");
const deleteBtn = document.getElementById("delete-btn");
const tabBtn = document.getElementById("tab-btn");
let myTabs = [];
const notesFromLocalStorage = JSON.parse(localStorage.getItem("myNotes"));
const tabsFromLocalStorage = JSON.parse(localStorage.getItem("myTabs"));
if (notesFromLocalStorage) {
myNotes = notesFromLocalStorage;
myTabs = tabsFromLocalStorage;
render(myNotes, myTabs);
}
tabBtn.addEventListener("click", function () {
let notes = inputEl.value;
inputEl.value = "";
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const tabUrl = tabs[0].url;
const tabTitle = tabs[0].title;
if (!myNotes.includes(tabUrl)) {
myNotes.push(tabUrl);
myTabs.push(tabTitle);
}
localStorage.setItem("myNotes", JSON.stringify(myNotes));
localStorage.setItem("myTabs", JSON.stringify(myTabs));
localStorage.setItem(tabUrl, notes);
render(myNotes, myTabs);
});
});
function render(notes, tabs) {
let listItems = "";
for (let i = 0; i < notes.length; i++) {
listItems += `
<li>
<a href='#' id=${notes[i]}>
${tabs[i]}
</a>
</li>
`;
}
ulEl.innerHTML = listItems;
}
deleteBtn.addEventListener("click", function () {
localStorage.clear();
myNotes = [];
myTabs = [];
render(myNotes, myTabs);
});
ulEl.addEventListener("click", function (event) {
if (event.target.tagName === "A") {
const noteID = event.target.id;
inputEl.value = localStorage.getItem(noteID);
}
});