-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
269 lines (218 loc) · 9 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
var videoHeartIcon = document.querySelectorAll(".heart-icon");
var iconCtn = document.querySelector(".icon-ctn");
var menuCtn = document.querySelector(".menu-ctn");
var settingsCtn = document.querySelector("#settings");
var searchInput = document.getElementById("search-input");
var addVideoTitleBtn = document.querySelectorAll(".add-video-title");
var likedVideoFilter = document.getElementById("likes-select-filter");
var dateVideoFilter = document.getElementById("date-select-filter");
var clearListBtn = document.getElementById("clear-list-btn");
var rootElement = document.body;
window.addEventListener("DOMContentLoaded", async function () {
await renderVideoBookmarkList();
const toggle = document.querySelector("#toggle");
var setTheme = (await chrome.storage.sync.get("theme")).theme;
if (setTheme) rootElement.classList.toggle("lightMode");
toggle.checked = setTheme;
[...document.querySelectorAll(".remove-btn")].forEach(function (item) {
item.addEventListener("click", removeVideoFromList);
});
[...document.querySelectorAll(".heart-icon")].forEach(function (item) {
item.addEventListener("click", heartIconToggle);
});
[...document.querySelectorAll(".add-video-title")].forEach(function (item) {
item.addEventListener("click", updatedVideoTitle);
});
searchInput.addEventListener("keyup", searchFilter);
menuCtn.addEventListener("click", menuToggle);
likedVideoFilter.addEventListener("click", videoLikesFilter);
dateVideoFilter.addEventListener("click", dateFilter);
toggle.addEventListener("click", async function (item) {
var themeToggle = rootElement.classList.toggle("lightMode");
await chrome.storage.sync.set({ theme: themeToggle });
toggle.checked = themeToggle;
});
clearListBtn.addEventListener("click", async function () {
if (confirm("Are you sure you want to clear your bookmark list?"))
await chrome.storage.sync.remove("videoObj");
});
});
chrome.storage.onChanged.addListener(async function () {
await renderVideoBookmarkList();
[...document.querySelectorAll(".remove-btn")].forEach(function (item) {
item.addEventListener("click", removeVideoFromList);
});
[...document.querySelectorAll(".heart-icon")].forEach(function (item) {
item.addEventListener("click", heartIconToggle);
});
[...document.querySelectorAll(".add-video-title")].forEach(function (item) {
item.addEventListener("click", updatedVideoTitle);
});
});
async function renderVideoBookmarkList(event, area) {
document.querySelector("#video-list-ctn").innerHTML = "";
let bkmarkImg = document.createElement("img");
bkmarkImg.src = chrome.runtime.getURL("images/bookmark.svg");
let videoArr = await chrome.storage.sync.get("videoObj");
let ul = document.createElement("ul");
ul.className = "video-ul";
for (let video of videoArr.videoObj) {
ul.innerHTML += `
<li>
<div class="card-ctn ctn" id="${video.url}">
<div class="section-info d-row">
<div class="img-ctn ctn">
<a href="${video.urlTimestamp}" target="_blank" class="cursor">
<img src="${video.thumbnail}" alt="video thumbnail">
</a>
</div>
<div class="info-ctn ctn d-column">
<a href="${video.urlTimestamp}" target="_blank" class="title-url cursor">
<h2 class="title"> ${video.title} </h2>
</a>
<div class="video-details-ctn d-row">
<div class="icon-details d-row">
<img class="icon calender" src="./images/calender-icon.svg" alt="">
<span>${video.dateUpdated}</span>
</div>
<div class="icon-details d-row">
<img class="icon clock" src="./images/clock.svg" alt="">
<span>${video.timeStamp} / ${video.videoLength}</span>
</div>
</div>
<div class="input-ctn d-row">
<input id="input-${video.url}" type="text" placeholder="bookmark a name"
class="bookmark-name text-input">
<img id="add-${video.url}" class="add-video-title icon cursor" src="./images/plus-button.svg" alt="">
</div>
</div>
</div>
<div class="section-icons d-row">
<div class="icon-ctn ctn d-column">
<div class="icon heart cursor">
<img id="heart-icon" class="${video.heart} heart-icon" src="./images/${video.heart}-heart.svg" alt="">
</div>
<div class="icon trashcan cursor remove-btn">
<img src="./images/trashcan.svg" alt="">
</div>
</div>
<div class="right-arrow-ctn ctn d-row">
<a href="${video.urlTimestamp}" target="_blank" class="title-url cursor">
<img class="icon cursor" src="./images/right-arrow.svg" alt="">
</a>
</div>
</div>
</div>
</li>
`;
}
//document.querySelector('#video-list-ctn').innerHTML = "";
document.querySelector("#video-list-ctn").appendChild(ul);
}
async function removeVideoFromList(item) {
const removeUrl = item.target.closest(".card-ctn").id;
let videoArr = (await chrome.storage.sync.get("videoObj")).videoObj;
for (let video of videoArr) {
if (video.url === removeUrl) {
let index = videoArr.indexOf(video);
if (index > -1) {
videoArr.splice(index, 1);
}
await chrome.storage.sync.set({ videoObj: videoArr });
}
}
}
async function updatedVideoTitle(item) {
const idString = item.target.id.toString().substring(4);
const inputField = document.getElementById(`input-${idString}`);
let videoObject = await chrome.storage.sync.get("videoObj");
let videoArr = videoObject.videoObj;
let video = videoArr.find((vid) => {
return vid.url === idString;
});
video.title = inputField.value;
await chrome.storage.sync.set({ videoObj: videoArr });
}
async function heartIconToggle(item) {
heartIcon = item.target;
let videoArr = (await chrome.storage.sync.get("videoObj")).videoObj;
let videoID = heartIcon.closest(".card-ctn").id;
videoArr.forEach((video, val) => {
if (video.url === videoID) {
if (heartIcon.classList.contains("open")) {
heartIcon.src = "./images/close-heart.svg";
heartIcon.classList.add("close");
heartIcon.classList.remove("open");
videoArr[val].heart = "close";
} else {
heartIcon.src = "./images/open-heart.svg";
heartIcon.classList.add("open");
heartIcon.classList.remove("close");
videoArr[val].heart = "open";
}
}
});
await chrome.storage.sync.set({ videoObj: videoArr });
}
async function searchFilter() {
let filter, ul, li, a, i, txtValue;
filter = searchInput.value.toUpperCase();
ul = document.querySelector(".video-ul");
li = ul.querySelectorAll("li");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("h2")[0];
if (!a.innerText || !a.textContent) {
txtValue = li[i].getElementsByTagName("h2")[0];
} else txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) li[i].style.display = "";
else li[i].style.display = "none";
}
}
async function menuToggle() {
if (settingsCtn.classList.contains("close-menu")) {
settingsCtn.classList.add("open-menu");
settingsCtn.classList.remove("close-menu");
} else {
settingsCtn.classList.add("close-menu");
settingsCtn.classList.remove("open-menu");
}
}
async function videoLikesFilter(item) {
let selectedOption = item.target.value;
let ul, li, heartIcon;
ul = document.querySelector(".video-ul");
li = ul.querySelectorAll("li");
if (selectedOption === "Liked") {
for (i = 0; i < li.length; i++) {
heartIcon = li[i].getElementsByClassName("heart-icon")[0];
if (heartIcon.classList.contains("close")) li[i].style.display = "";
else li[i].style.display = "none";
}
} else {
for (i = 0; i < li.length; i++) {
heartIcon = li[i].getElementsByClassName("heart-icon")[0];
li[i].style.display = "";
}
}
}
async function dateFilter(item) {
let selectedOption = item.target.value;
let ul, li;
ul = document.querySelector(".video-ul");
li = ul.querySelectorAll("li");
let videoArr = (await chrome.storage.sync.get("videoObj")).videoObj;
let result;
if (selectedOption === "Oldest")
result = videoArr.sort(
(date1, date2) =>
Date.parse(date1.dateUpdated) - Date.parse(date2.dateUpdated)
);
else
result = videoArr.sort(
(date1, date2) =>
Date.parse(date2.dateUpdated) - Date.parse(date1.dateUpdated)
);
await chrome.storage.sync.set({ videoObj: result });
likedVideoFilter.value = "All";
}
///////////////////////////// UPDATES /////////////////////////////