-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
184 lines (155 loc) · 7.32 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
document.addEventListener('DOMContentLoaded', () => {
// homepage items
const form_add = document.getElementById("user-input");
const div_alert_success = document.getElementById("alert-success");
const div_alert_error = document.getElementById("alert-error");
const div_storage = document.getElementById("storage-container");
const button_view_links = document.getElementById('get-links');
// edit form items
const form_edit = document.getElementById("edit-form");
const button_edit_save = document.getElementById("save-edit-button");
const button_edit_cancel = document.getElementById("cancel-edit-button");
// list of links and edit form are hidden on load
div_storage.style.display = 'none';
form_edit.style.display = 'none';
// event listener for form submission
// create a new quick link
form_add.addEventListener("submit", (event) => {
event.preventDefault();
// data for a new quick-link item
const data = [
form_add.elements["quick-link-key"].value, // keyword
form_add.elements["quick-link-url"].value // url
];
// send message to background.js
chrome.runtime.sendMessage({ type: 'form_submission', data }, (response) => {
// display success or error message depending
// on response from background.js
if (response.success) {
div_alert_error.style.display = 'none'; // hide error message
div_alert_success.innerHTML = `<p>${response.message}</p>`; // insert success message
div_alert_success.style.display = 'block'; // display success message
form_add.reset(); // clear form
} else {
div_alert_success.style.display = 'none'; // hide success message
div_alert_error.innerHTML = `<p>${response.message}</p>`; // insert error message
div_alert_error.style.display = 'block'; // display error message
form_add.reset(); // clear form
}
});
div_storage.style.display = 'none'; // hide list of links in case visible
div_storage.innerHTML = ""; // reset list of links
button_view_links.innerHTML = "View Links"; // reset button text
});
// event listener for button click
// display a list of all stored quick-links
button_view_links.addEventListener('click', getQuickLinks);
// event listener for cancel button in edit form
// hide edit form and display list of links
button_edit_cancel.addEventListener('click', () => {
form_edit.style.display = 'none';
div_storage.style.display = 'block';
});
// function to get all quick-links from storage
function getQuickLinks() {
// if button text is "Hide Links", hide list of links
if (button_view_links.innerHTML == "Hide Links") {
button_view_links.innerHTML = "View Links";
div_storage.style.display != 'none';
div_storage.innerHTML = "";
}
// if button text is "View Links", display list of links
else {
button_view_links.innerHTML = "Hide Links";
chrome.storage.sync.get(['redirect_link'], (result) => {
if (div_storage.style.display != 'none') {
div_storage.innerHTML = "";
}
if (result.redirect_link === undefined || Object.keys(result.redirect_link).length == 0) {
div_storage.innerHTML = "<p><small>Empty</small></p>";
} else {
// create a new link item for each quick-link
for (let key in result.redirect_link) {
const template = document.getElementById('link-template').content.cloneNode(true);
const link_item = template.querySelector('.link-item');
template.querySelector('.link-key').textContent = key;
template.querySelector('.link-url').textContent = result.redirect_link[key];
// delete quick-link on delete button click
const button_delete = template.querySelector('.delete-button');
button_delete.addEventListener('click', () => deleteLink(key));
// add strike-through effect on delete button hover
button_delete.addEventListener('mouseover', () => link_item.classList.add('strike-through'));
button_delete.addEventListener('mouseout', () => link_item.classList.remove('strike-through'));
// add edit form to div on edit button click
template.querySelector('.edit-button').addEventListener('click', () => showEditForm(key, result.redirect_link[key]));
div_storage.appendChild(template);
}
}
// display list of links
div_storage.style.display = 'block';
});
}
}
// function to show edit form
function showEditForm(key, url) {
div_storage.style.display = 'none';
form_edit.style.display = 'block';
form_edit.elements["edit-link-key"].value = key;
form_edit.elements["edit-link-url"].value = url;
button_edit_save.onclick = (event) => {
event.preventDefault();
editLink(key);
};
}
// function to edit a quick-link
function editLink(old_keyword) {
const new_keyword = form_edit.elements["edit-link-key"].value;
const new_url = form_edit.elements["edit-link-url"].value;
if (new_keyword && new_url) {
// send message to background.js to update quick-link item
chrome.runtime.sendMessage({
type: 'edit_submission',
old_keyword: old_keyword,
new_keyword: new_keyword,
new_url: new_url
}, (response) => {
// display success or error message depending
// on response from background.js
if (response.success) {
div_alert_error.style.display = 'none';
div_alert_success.innerHTML = `<p>${response.message}</p>`;
div_alert_success.style.display = 'block';
form_edit.reset();
form_edit.style.display = 'none';
div_storage.style.display = 'block';
getQuickLinks();
} else {
div_alert_success.style.display = 'none';
div_alert_error.innerHTML = `<p>${response.message}</p>`;
div_alert_error.style.display = 'block';
form_edit.reset();
}
});
}
}
// function to delete a quick-link
function deleteLink(key) {
chrome.runtime.sendMessage({
type: 'delete_link',
key: key
}, (response) => {
// display success or error message depending
// on response from background.js
if (response.success) {
div_alert_error.style.display = 'none';
div_alert_success.innerHTML = `<p>${response.message}</p>`;
div_alert_success.style.display = 'block';
getQuickLinks();
} else {
div_alert_success.style.display = 'none';
div_alert_error.innerHTML = `<p>${response.message}</p>`;
div_alert_error.style.display = 'block';
}
});
}
});