-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (44 loc) · 1.45 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
const inputField = document.getElementById("input-el");
const saveBtn = document.getElementById("save-btn");
const currentLinkBtn = document.getElementById("currentLink-btn");
const resetBtn = document.getElementById("reset-btn");
const listUl = document.getElementById("id-ul") ;
const state = JSON.parse(localStorage.getItem("listOfLinks")); ;
let listOfLinks= state? state: [];
renderListUl(listOfLinks);
saveBtn.addEventListener("click", ()=>{
if(validInput(inputField.value)) {
listOfLinks.push(inputField.value);
inputField.value = "";
renderListUl(listOfLinks);
}
});
currentLinkBtn.addEventListener("click", function(){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if(validInput(tabs[0].url)) {
listOfLinks.push(tabs[0].url);
renderListUl(listOfLinks);
}
})
});
resetBtn.addEventListener("click", ()=>{
listUl.innerHTML= "";
listOfLinks= [];
localStorage.clear();
});
function renderListUl(listOfLinks) {
localStorage.setItem("listOfLinks", JSON.stringify(listOfLinks));
listUl.innerHTML="";
for(const item of listOfLinks) {
listUl.innerHTML += `<li><a href="${item}" target="_blank">${item}</a></li>`
}
}
function validInput(str) {
if(str==="")
return false;
for(const item of listOfLinks){
if(item===str)
return false;
}
return true;
}