-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
51 lines (43 loc) · 1.62 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
var optionsContainer = document.getElementById('options-container');
var totalInputFields = 0;
var addFilterButton = document.getElementById('add-filter');
addFilterButton.addEventListener('click', addFilter);
var saveButton = document.getElementById('save-filters');
saveButton.addEventListener('click', saveFilters);
chrome.storage.sync.get("filters", function(obj) {
loadFilters(obj["filters"]);
});
function addFilter() {
var elements = document.getElementsByTagName('input');
optionsContainer.insertAdjacentHTML('beforeend',
'<input placeholder="Anime title" class="' + totalInputFields.toString()
+ '"><div class="remove-filter ' + totalInputFields.toString()
+ '">X</div><br class="' + totalInputFields.toString() + '">'
);
var removeButton = document.getElementsByClassName(totalInputFields.toString())[1];
var temp = totalInputFields;
removeButton.addEventListener('click', function(){removeFilter(temp);});
totalInputFields++;
}
function loadFilters(filters) {
for (var i = 0; i < filters.length; i++) {
addFilter();
var elements = document.getElementsByTagName('input');
elements[elements.length-1].value = filters[i];
}
}
function removeFilter(number) {
var toRemove = document.getElementsByClassName(number.toString());
for (var i = toRemove.length-1; i >= 0; i--)
toRemove[i].remove();
}
function saveFilters() {
var elements = document.getElementsByTagName('input');
var filters = [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].value != "")
filters.push(elements[i].value);
}
chrome.storage.sync.set({"filters": filters});
alert("Saved!");
}