-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
104 lines (91 loc) · 3.02 KB
/
script.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
98
99
100
101
102
103
104
let myLeads = JSON.parse(localStorage.getItem("myLeads")) || [];
const inputElem = document.getElementById("input-el");
const inputBtn = document.getElementById("input-btn");
const ulElem = document.getElementById("ul-el");
const deleteElem = document.getElementById("delete-btn");
const saveTabBtn = document.getElementById("save-tab-btn");
const deleteItemElem = document.getElementById("delete-item");
const errorExistsElem = document.getElementById("error-exits");
inputBtn.onclick = saveLead;
deleteElem.onclick = deleteAll;
saveTabBtn.onclick = saveTab;
function deleteAll() {
localStorage.removeItem("myLeads");
myLeads = [];
renderLeads(myLeads);
}
function hideErrorMessage() {
errorExistsElem.style.display = "none";
duplicateIndex = undefined;
}
function showErrorMessage(index) {
errorExistsElem.style.display = "block";
duplicateIndex = index;
}
let duplicateIndex;
function saveTab() {
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
const tab = tabs[0];
const domain = tab.url.replace(/.+\/\/|www.|\..+/g, "");
const lead = `${domain}%${tab.title}%${tab.url}`;
if (!myLeads.includes(lead)) {
hideErrorMessage();
myLeads.push(lead);
localStorage.setItem("myLeads", JSON.stringify(myLeads));
renderLeads(myLeads);
} else {
showErrorMessage(myLeads.indexOf(lead));
}
});
}
function saveLead() {
myLeads.push(inputElem.value);
localStorage.setItem("myLeads", JSON.stringify(myLeads));
ulElem.innerHTML += `
<li class='link flex'>${domain} - ${title}
<a href="${link}" target='_blank' rel='noreferrer' class='link'>open link</a>
</li>
`;
inputElem.value = "";
}
function createItem(lead, index) {
const [domain, title, link] = lead.split("%");
const li = document.createElement("li");
li.classList.add("link", "flex");
const span = document.createElement("span");
span.textContent = `${domain} - ${title.slice(0, 30)}`;
const button = document.createElement("button");
button.id = "delete-item";
button.innerHTML = `<img src="/bin.png" width="12" height="12" alt="delete item" />`;
button.addEventListener("click", () => {
ulElem.removeChild(li);
let leads = JSON.parse(localStorage.getItem("myLeads"));
leads = [...leads.slice(0, index), ...leads.slice(index + 1)];
localStorage.setItem("myLeads", JSON.stringify(leads));
if (duplicateIndex === index) {
hideErrorMessage();
}
});
const div = document.createElement("div");
const a = document.createElement("a");
a.href = link;
a.target = "_blank";
a.rel = "noreferrer";
a.classList.add("link");
a.textContent = "open link";
li.appendChild(span);
div.appendChild(a);
div.appendChild(button);
li.appendChild(div);
return li;
}
function renderLeads(leads) {
ulElem.innerHTML = "";
const fragment = document.createDocumentFragment();
for (let i = 0; i < leads.length; i++) {
const elem = createItem(leads[i], i);
fragment.appendChild(elem);
}
ulElem.appendChild(fragment);
}
renderLeads(myLeads);