-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
68 lines (64 loc) · 2 KB
/
popup.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
const ulContainer = document.getElementById('compareList');
const displayMSG = document.getElementById('display');
function isEmpty(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
function removeScrapedData(items, index) {
let newListOfItems = [];
ulContainer.innerHTML = "";
for(let ti in items) {
if(ti==index) {
continue;
}
let liElement = document.createElement('li');
liElement.innerHTML = `
<b>${items[ti].title}</b> <span class="cls_btn">x<span>
`;
ulContainer.appendChild(liElement);
newListOfItems.push(items[ti]);
}
const clsBtns = Array.from(document.getElementsByClassName('cls_btn'));
clsBtns.forEach((clsBtn, index) => {
clsBtn.addEventListener('click', () => {
removeScrapedData(newListOfItems, index);
})
})
chrome.storage.local.set({ listOfItems: newListOfItems });
if(!newListOfItems.length) {
displayMSG.innerText = "Please add something to compare";
document.getElementById('openPage').style.display = "none";
}
}
const openPageBtn = document.getElementById('openPage');
openPageBtn.addEventListener('click', () => {
const tabProperties = {
url: './comparePage.html'
}
chrome.tabs.create(tabProperties);
})
chrome.storage.local.get('listOfItems', function (object) {
if(isEmpty(object) || !object.listOfItems.length) {
displayMSG.innerText = "Please add something to compare";
document.getElementById('openPage').style.display = "none";
} else {
let items = object.listOfItems;
items.forEach(function (element) {
let liElement = document.createElement('li');
liElement.innerHTML = `
<b>${element.title}</b> <span class="cls_btn">x<span>
`;
ulContainer.appendChild(liElement);
})
const clsBtns = Array.from(document.getElementsByClassName('cls_btn'));
clsBtns.forEach((clsBtn, index) => {
clsBtn.addEventListener('click', () => {
removeScrapedData(items, index);
})
})
}
})