-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (71 loc) · 1.87 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// chrome://extensions/
const inputEl = document.getElementById("input-el")
const inputBtn = document.getElementById("input-btn")
const ulEl = document.getElementById("ul-el")
const deleteBtn = document.getElementById("delete-btn")
const tabBtn = document.getElementById("tab-btn")
let leads = getLeads();
if(leads == null){
setLeads([]);
}
render();
tabBtn.addEventListener("click", function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
addLead(tabs[0].url)
render()
})
})
deleteBtn.addEventListener("dblclick", function() {
setLeads([])
leads = []
render()
})
inputBtn.addEventListener("click", function() {
addLead(inputEl.value);
inputEl.value = "";
render();
})
function render() {
ulEl.innerHTML="";
let leads = getLeads();
leads.forEach( lead => {
let leadtext = document.createTextNode(lead);
let li = document.createElement('li');
let btn = document.createElement('button');
btn.textContent = 'X';
btn.style.marginLeft = '10px';
li.appendChild(leadtext);
li.appendChild(btn);
ulEl.appendChild(li);
btn.addEventListener('click', () => {
clearThisLead(lead);
})
})
}
function getLeads() {
leadsFromLocalStorage = localStorage.getItem("myLeads");
if(leadsFromLocalStorage) {
return JSON.parse(leadsFromLocalStorage);
}
return null;
}
function setLeads(leads) {
return localStorage.setItem('myLeads', JSON.stringify(leads));
}
function removeLead(lead) {
let leads = getLeads();
let index = leads.indexOf(lead);
if (index > -1) {
leads.splice(index, 1);
}
setLeads(leads);
}
function addLead(lead) {
let leads = getLeads();
leads.push(lead);
setLeads(leads);
}
function clearThisLead(lead) {
removeLead(lead);
render();
}