-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathoptions.js
150 lines (136 loc) · 4.83 KB
/
options.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
{
const list = document.getElementById("saved-urls");
chrome.storage.local.get("scroll-mark", function(result) {
const urls = result["scroll-mark"];
let delay = 0.3;
// if no saved scrolls are available
const heading = document.getElementById("saved-scroll-heading");
if (Object.entries(urls).length === 0 && urls.constructor === Object) {
heading.innerHTML =
"<h1 id='saved-scroll-heading'>You don't have any saved scrrolls yet!</h1>";
heading.parentElement.style.height = "100vh";
} else {
heading.innerHTML =
"<h1 id='saved-scroll-heading'>All Saved Scrrolls</h1>";
const btn = document.createElement("div");
btn.innerHTML = `
<div id="delete-button">
<div class="btn del" id="delete-all"> <img src='./images/bin.png'> </div>
</div>
`;
document.body.appendChild(btn);
for (var url in urls) {
const title = urls[url].title || url;
const url_id = url;
const div = document.createElement("div");
div.setAttribute("class", "Scroll");
div.setAttribute(
"data-date",
urls[url].date ? urls[url].date.slice(0, 15) : "Date-Here"
);
const percentage = Math.round(
(urls[url].offset / urls[url].total) * 100
);
const delete_html = `
<div class="dropdown" id="${url_id}_|_">
<div id="${url_id}_|" class="dropdown-content">
<button class="ScrollElement" id="${url_id}">Delete</button>
</div>
</div>
`;
if (percentage)
div.innerHTML =
"<a href=" +
url +
">" +
title +
"</a> " +
"<div class='perc'>" +
percentage +
"</div>" +
delete_html;
else
div.innerHTML =
"<a href=" +
url +
">" +
title +
"</a> " +
"<div class='perc'>" +
0 +
"</div>" +
delete_html;
div.style.animationDelay = (delay += 0.1) + "s";
list.appendChild(div);
}
}
// function to open dropdown menu
const showDropdown = function(e) {
const drop_id = this.id.substring(0, this.id.length - 1);
document.getElementById(drop_id).classList.toggle("show_dropdown");
};
// function to close dropdown menu
const closeDropdown = function(e) {
const dropdowns = document.getElementsByClassName("dropdown-content");
if (e.target.className !== "dropdown") {
for (let i = 0; i < dropdowns.length; i++) {
dropdowns[i].classList.remove("show_dropdown");
}
}
};
// all dropdown menus
const dotElements = document.getElementsByClassName("dropdown");
for (let i = 0; i < dotElements.length; i++) {
dotElements[i].addEventListener("click", showDropdown, false);
}
const containers = document.getElementsByClassName("container");
containers[0].addEventListener("click", closeDropdown);
const scrollElements = document.getElementsByClassName("ScrollElement");
const deleteAllBtn = document.getElementById("delete-all");
// TODO: use the delete.js script instead
// but that would require some modifications to the original delete functionality
const deleteScrollElement = function(element) {
Swal.fire({
title: "Are you sure?",
text: "This scrroll would be removed from your collection.",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!"
}).then(result => {
if (result.value) {
const { [this.id]: _, ...restData } = urls;
chrome.storage.local.set({ "scroll-mark": restData }, () => {
chrome.runtime.sendMessage("setInactive");
});
let x = document.querySelector(`a[href = "${this.id}"]`);
x.parentElement.parentElement.removeChild(x.parentElement);
// window.location.reload();
}
});
};
const deleteAllScrollElement = function(element) {
Swal.fire({
title: "Remove all Scrrolls?",
text: "This action is not reversible!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Delete All"
}).then(result => {
if (result.value) {
chrome.storage.local.set({ "scroll-mark": {} }, data => {});
window.location.reload();
}
});
};
for (let i = 0; i < scrollElements.length; i++) {
scrollElements[i].addEventListener("click", deleteScrollElement, false);
}
if (deleteAllBtn) {
deleteAllBtn.addEventListener("click", deleteAllScrollElement, false);
}
});
}