-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (56 loc) · 1.96 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
const inputBtn = document.querySelector('#input-btn');
const deleteBtn = document.querySelector('#delete-btn');
const tabBtn = document.querySelector('#tab-btn');
const inputEl = document.querySelector('#input-el');
const ulEl = document.querySelector('#ul-el');
// Falsy values -> false, 0, "", null, undefined, NaN
// null -> how developers can set a variable to nothing
// undefined -> how JavaScript sets a variable to nothing
let myLeads = [];
// localStorage.clear();
const leadsFromLocalStorage = JSON.parse(localStorage.getItem('myLeads'));
if (leadsFromLocalStorage) {
myLeads = leadsFromLocalStorage;
render(myLeads);
};
function render(leads) {
let listItems = "";
for (let i = 0; i < leads.length; i++) {
// document.createElement("li");
// li.textContent = myLeads[i];
// ulEl.append(li);
listItems += `
<li>
<a target='_blank' href='${leads[i]}'>
${leads[i]}
</a>
</li>
`;
};
ulEl.innerHTML = listItems;
};
inputBtn.addEventListener('click', function() {
myLeads.push(inputEl.value);
inputEl.value = "";
// myLeads = JSON.parse(myLeads); // Turns it to previous type e.g array
// myLeads = JSON.stringify(myLeads); // Turns it to string
localStorage.setItem('myLeads', JSON.stringify(myLeads));
// localStorage.setItem('myLeads', 'www.waltertayarg.tech');
render(myLeads);
console.log(localStorage.getItem('myLeads'));
});
deleteBtn.addEventListener('dblclick', function() {
localStorage.clear();
myLeads = [];
render(myLeads);
});
tabBtn.addEventListener('click', function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
myLeads.push(tabs[0].url);
localStorage.setItem('myLeads', JSON.stringify(myLeads));
render(myLeads);
});
// myLeads.push(inputEl.value);
// localStorage.setItem('myLeads', JSON.stringify(myLeads));
// render(myLeads);
});