-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.js
More file actions
99 lines (80 loc) · 2.9 KB
/
options.js
File metadata and controls
99 lines (80 loc) · 2.9 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
/*global chrome*/
var filters = {};
var filterList = [];
function addFilter(fil){
var row = document.createElement('tr');
var enCell = document.createElement('td');
var tog = document.createElement('input');
tog.type = 'checkbox';
tog.checked = fil.enabled;
tog.addEventListener('click', function(evt){
fil.enabled = tog.checked;
});
enCell.appendChild(tog);
row.appendChild(enCell);
var labelCell = document.createElement('td');
labelCell.textContent = fil.inword + ' → ' + fil.outword;
row.appendChild(labelCell);
function deleteFilter(){
document.getElementById('filters').removeChild(row);
filterList.splice(filterList.indexOf(fil),1);
delete filters[fil.inword.toLowerCase()];
}
var actionCell = document.createElement('td');
var edButton = document.createElement('button');
edButton.type = 'button';
edButton.className = 'btn btn-default btn-sm';
edButton.textContent = 'Edit';
edButton.addEventListener('click', function () {
document.getElementById('word-in').value = fil.inword;
document.getElementById('word-out').value = fil.outword;
document.getElementById('lwb').checked = fil.lwb;
document.getElementById('rwb').checked = fil.rwb;
document.getElementById('strict').checked = fil.strict;
deleteFilter();
});
actionCell.appendChild(edButton);
actionCell.appendChild(document.createTextNode(' '));
var delButton = document.createElement('button');
delButton.type = 'button';
delButton.className = 'btn btn-danger btn-sm';
delButton.textContent = 'Delete';
delButton.addEventListener('click',deleteFilter);
actionCell.appendChild(delButton);
row.appendChild(actionCell);
document.getElementById('filters').appendChild(row);
filterList.push(fil);
filters[fil.inword.toLowerCase()] = fil;
}
function addCurrentFilter(){
if (document.getElementById('word-in').value
&& document.getElementById('word-out').value
&& !filters[document.getElementById('word-in').value.toLowerCase()]) {
addFilter({
enabled: true,
inword: document.getElementById('word-in').value,
outword: document.getElementById('word-out').value,
lwb: document.getElementById('lwb').checked,
rwb: document.getElementById('rwb').checked,
strict: document.getElementById('strict').checked});
document.getElementById('word-in').value = '';
document.getElementById('word-out').value = '';
}
}
function populateFilters(filters) {
return Object.keys(filters).forEach(function(key){
addFilter(filters[key]);
});
}
function loadExistingFilters(){
//TODO: sort filters before writing list
chrome.storage.sync.get('filters', function(res){
populateFilters(res.filters);
});
}
loadExistingFilters();
document.getElementById('save').addEventListener('click', function(){
chrome.storage.sync.set({filters: filters});
});
document.getElementById('newfilter').addEventListener(
'click', addCurrentFilter);