-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (82 loc) · 3 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
let myLeads = []
const inputEl = document.getElementById("input-el")
const inputBtn = document.getElementById("save-btn")
const deleteBtn = document.getElementById("delete-btn")
const tabBtn = document.getElementById("tab-btn")
const warning = document.getElementById('warning-msg')
const ulEl = document.getElementById("ul-el")
const leadsFromLocalStorage = JSON.parse(localStorage.getItem("myLeads"))
if (leadsFromLocalStorage) {
myLeads = leadsFromLocalStorage
render(myLeads)
}
tabBtn.addEventListener("click", function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
myLeads.push({ url: tabs[0].url, tags: [] })
localStorage.setItem("myLeads", JSON.stringify(myLeads))
render(myLeads)
})
})
deleteBtn.addEventListener("dblclick", function() {
localStorage.clear()
myLeads = []
render(myLeads)
})
inputBtn.addEventListener("click", function() {
if (inputEl.value) {
if (inputEl.value.includes(" ")) {
warning.textContent = "Please remove the spaces from the input"
} else {
myLeads.push({ url: "https://" + inputEl.value, tags: [] })
inputEl.value = ""
warning.textContent = ""
localStorage.setItem("myLeads", JSON.stringify(myLeads))
render(myLeads)
}
} else {
warning.textContent = "Please enter some input in the input field"
}
})
function delSingle(i) {
myLeads.splice(i, 1)
localStorage.setItem("myLeads", JSON.stringify(myLeads))
render(myLeads)
}
function addTag(index) {
const tagInput = document.querySelector(`#tag-input-${index}`).value
if (tagInput) {
myLeads[index].tags.push(tagInput)
localStorage.setItem("myLeads", JSON.stringify(myLeads))
render(myLeads)
}
}
function delTag(index , tagIndex) {
myLeads[index].tags.splice(tagIndex, 1)
localStorage.setItem("myLeads", JSON.stringify(myLeads))
render(myLeads)
}
function render(myLeads) {
let listItems = ""
for (let i = 0; i < myLeads.length; i++) {
listItems += `
<li class="list-items">
<h4>${i+1})</h4>
<a target='_blank' href='${myLeads[i].url}'>
${myLeads[i].url}
</a>
<button id='single-del-button' onClick="delSingle(${i})">
<i class="fa-solid fa-trash"></i>
</button>
<div class="tag-div">
<input id="tag-input-${i}" class="tag-input" placeholder="Enter tags" />
<button class="tag-button" onClick="addTag(${i})">TAG</button>
</div>
</li>
<div class="tags-container">
${myLeads[i].tags.map(tag => `<span class="tag">${tag}
<button id='single-del-button' onClick="delTag(${i},${myLeads[i].tags[tag]})"> <i class="fa-solid fa-trash"></i> </button></span>`).join(' ')}
</div>
`
}
ulEl.innerHTML = listItems
}