-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
155 lines (130 loc) · 3.85 KB
/
options.js
File metadata and controls
155 lines (130 loc) · 3.85 KB
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
let debug = true
log('Adding event listener for DOMContentLoaded.')
document.addEventListener('DOMContentLoaded', onDomContentLoaded);
function onDomContentLoaded() {
restoreOptions();
log("Adding event listener for click event on addButton.");
document.getElementById('addButton').addEventListener("click", registerUrl);
}
// Restores the user's options.
function restoreOptions() {
log('Restoring options...')
chrome.storage.sync.get({
// Default values: empty list.
registeredLinks: []
}, function (items) {
// Get the data to populate the table with.
var registeredLinks = items.registeredLinks;
log('Loaded registered links:');
log(registeredLinks);
// Create a new table body element.
var newTableBody = document.createElement('tbody');
newTableBody.id = 'tableBody'
// These are stored as { 0: ..., 1:, ... } for some reason.
for (key in registeredLinks) {
let registeredLink = registeredLinks[key];
// Get and check values.
let keyword = registeredLink.keyword
let url = registeredLink.url;
if (keyword && url) {
// Create and populate row.
var row = newTableBody.insertRow();
row.id = keyword
var col1 = row.insertCell();
col1.innerText = keyword
var col2 = row.insertCell();
col2.innerText = url;
let removeButton = document.createElement('button');
removeButton.id = keyword;
removeButton.classList = "remove";
removeButton.innerText = "Remove";
var col3 = row.insertCell();
col3.innerHTML = removeButton.outerHTML;
col3.childNodes[0].addEventListener('click', function() {
unregisterUrl(keyword);
});
}
}
// Get the current table body element.
var oldTableBody = document.getElementById('tableBody');
// Replace table body with newly generated rows.
if (oldTableBody) {
oldTableBody.parentNode.replaceChild(newTableBody, oldTableBody)
}
});
}
// Registers a new URL.
function registerUrl() {
log('Registering new url...');
// Get and check the data entered by the user.
var keywordInput = document.getElementById('keywordInput');
var keyword = keywordInput.value;
var urlInput = document.getElementById('urlInput');
var url = urlInput.value;
if (keyword && url) {
var newLink = {
keyword: keyword,
url: url
};
log('Adding link:');
log(newLink);
// Get the existing data.
chrome.storage.sync.get({
// Default values: empty list.
registeredLinks: []
}, function (items) {
// Create a new list of registered links with the new item added.
// (Filter out empty items)
var updatedRegisteredLinks = items.registeredLinks.filter(function(value) {
let keyword = value.keyword;
let url = value.url;
if (keyword && url) {
return keyword != '' && url != '';
} else {
return false;
}
});
updatedRegisteredLinks.push(newLink);
log('Updated links:');
log(updatedRegisteredLinks);
// Store the updated list of registered links.
chrome.storage.sync.set({
registeredLinks: updatedRegisteredLinks
}, function () {
// Update the page to indicate the changes.
// Clear the inputs.
keywordInput.value = "";
urlInput.value = "";
// Reload the table.
restoreOptions();
});
});
} else {
// One or more inputs were empty.
log('Input was invalid.')
}
}
function unregisterUrl(keyword) {
log('Unregistering keyword: ' + keyword);
// Get current keywords.
chrome.storage.sync.get({
// Default values: empty list.
registeredLinks: []
}, function (items) {
// Get the data to populate the table with.
let updatedLinks = items.registeredLinks.filter(function(value) {
return value.keyword != keyword;
});
// Store the updated links.
chrome.storage.sync.set({
registeredLinks: updatedLinks
}, function () {
// Reload the table.
restoreOptions();
});
});
}
function log(value) {
if (!debug) return;
console.log(value)
}