-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 1.78 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
61
62
63
64
65
let myLeads = [];
const inputBTN = document.querySelector("#input-btn");
const deleteBTN = document.querySelector("#delete-btn");
const tabBTN = document.querySelector("#save-tab-btn");
const inputEl = document.querySelector("#input-el");
const ulEl = document.querySelector("#ul-el");
const leadsFromLocalStorage = JSON.parse(localStorage.getItem("myLeads"));
if (leadsFromLocalStorage) {
myLeads = leadsFromLocalStorage;
render(myLeads);
}
tabBTN.addEventListener("click", () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
myLeads.push(tabs[0].url);
localStorage.setItem("myLeads", JSON.stringify(myLeads));
render(myLeads);
});
});
function render(leads) {
ulEl.innerHTML = "";
for (let i = 0; i < leads.length; i++) {
const listItem = document.createElement("li");
const anchorTag = document.createElement("a");
//ADD DELETE BUTTON
const deleteButton = document.createElement("button");
deleteButton.textContent = "X";
deleteButton.classList.add("delete-button"); // Add the "delete-button" class
deleteButton.addEventListener("click", () => {
deleteLead(i);
});
listItem.appendChild(deleteButton);
anchorTag.textContent += leads[i];
anchorTag.href = leads[i];
anchorTag.setAttribute("target", "_blank");
listItem.appendChild(anchorTag);
ulEl.appendChild(listItem);
}
}
function deleteLead(index) {
myLeads.splice(index, 1);
localStorage.setItem("myLeads", JSON.stringify(myLeads));
render(myLeads);
}
inputBTN.addEventListener("click", () => {
myLeads.push(inputEl.value);
inputEl.value = "";
localStorage.setItem("myLeads", JSON.stringify(myLeads));
render(myLeads);
});
deleteBTN.addEventListener("click", () => {
localStorage.clear();
myLeads = [];
render(myLeads);
});