-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.js
49 lines (49 loc) · 1.49 KB
/
tracker.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
let myLinks = [];
const ulEl = document.querySelector("#ul-ele");
const inputEl = document.querySelector("#input-ele");
const saveBtn = document.querySelector("#save-btn");
const linksFromLocalStorage = JSON.parse(localStorage.getItem("myLinks"));
const deleteBtn = document.querySelector("#delete-btn");
const clearBtn = document.querySelector("#clear-btn");
const saveTabBtn = document.querySelector("#save-tab-btn");
if (linksFromLocalStorage) {
myLinks = linksFromLocalStorage;
render(myLinks);
}
//saving links from local storage
saveTabBtn.addEventListener("click", () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
myLinks.push(tabs[0].url);
localStorage.setItem("myLinks", JSON.stringify(myLinks));
render(myLinks);
});
});
//main function
function render(links) {
let listItems = "";
for (let i = 0; i < links.length; i++) {
listItems += `
<li>
<a target = "_blank" href="${links[i]}">${links[i]}</a>
</li>`;
}
ulEl.innerHTML = listItems;
}
//delete links
deleteBtn.addEventListener("click", () => {
myLinks.pop();
render(myLinks);
});
//clear links saved
clearBtn.addEventListener("dblclick", () => {
localStorage.clear();
myLinks = [];
render(myLinks);
});
//save link
saveBtn.addEventListener("click", () => {
myLinks.push(inputEl.value);
inputEl.value = "";
localStorage.setItem("myLinks", JSON.stringify(myLinks));
render(myLinks);
});