-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.js
261 lines (235 loc) · 6.67 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//FIXME: replace filter on delete
var filterKeys = ['filter-', 'filtershortcut-', 'filterfallback-'];
var shortcutNone = '[none]';
function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
return letter.toUpperCase();
}).replace(/\s+/g, '');
}
//most retarded recursive funciton I've ever written, thanks to async storage
function findUniqueFilterName(name, callback, uniqueBit)
{
var uniqueBit = uniqueBit || '';
mystorage.get('filter-' + name + uniqueBit, function(value){
if (value)
findUniqueFilterName(name, callback, uniqueBit * 1 + 1);
else
callback(name + uniqueBit);
});
}
function saveFilter(filter)
{
var nameInput = filter.find('.filter-name');
var savedName = nameInput.attr('data-saved-name');
var newName = nameInput.val();
function saveData(name)
{
var shortcut = filter.find('.filter-shortcut').val();
if (shortcut == shortcutNone)
shortcut = null;
var data = {};
data['filter-' + name] = filter.find('.filter-source').val();
data['filterfallback-' + name] = filter.find('.filter-fallback').val();
data['filtershortcut-' + name] = shortcut;
mystorage.set(data);
mylogger.log('saved filter source for "' + name + '"');
for (var k in data)
mymessages.sendTabs({key:k, value:data[k]});
}
if (savedName != newName)
{
newName = camelize(newName);
findUniqueFilterName(newName, function(uniqueName){
if (uniqueName)
{
deleteFilter(savedName);
saveData(uniqueName);
nameInput.attr('data-saved-name', uniqueName);
nameInput.val(uniqueName);
}
else
{
saveData(savedName);
nameInput.val(savedName);
alert("Cannot rename filter to " + newName + ", it conflicts with another filter.");
}
});
}
else
saveData(savedName);
}
function deleteFilter(name)
{
var toRemove = []
for (var i = 0; i < filterKeys.length; ++i)
toRemove.push(filterKeys[i] + name);
mystorage.remove(toRemove);
}
function createFilter(name, source, fallback, shortcut)
{
shortcut = shortcut || shortcutNone;
var filter = $($('#filter-template').html());
$('#filters').append(filter);
filter.find('.filter-name').val(name).attr('data-saved-name', name);
filter.find('.deletefilter').click(function(){
var name = filter.find('.filter-name').attr('data-saved-name');
if (confirm("Deleting " + name + ". Are you sure?")) {
deleteFilter(name);
filter.remove();
}
});
filter.find('.savefilter').click(function(){
saveFilter(filter);
});
var sourceLen = (source.match(/\n/g)||[]).length + 3;
filter.find('.filter-source').val(source).attr('rows', Math.min(Math.max(sourceLen, 6), 30));
filter.find('.filter-fallback').val(fallback);
filter.find('.filter-shortcut').val(shortcut);
}
function loadOptions(){
mystorage.all(function(items){
for (var key in items)
{
if (key.match(/^[a-z-0-9]+$/))
{
var option = $('input[name="' + key + '"]');
if (option.length == 1)
{
var val = items[key];
if (option.hasClass('shortcut') && !val)
val = shortcutNone;
if (option[0].type == 'checkbox')
option[0].checked = val;
else
option[0].value = val;
continue;
}
}
var filter = key.match(/^filter-(.*)$/);
if (filter)
{
createFilter(
filter[1],
items[key],
items['filterfallback-' + filter[1]],
items['filtershortcut-' + filter[1]]
);
}
var log = key.match(/^log-(.*)$/);
if (log)
{
var list = items[key];
for (var i in list)
{
$('#logs').append('<li>' + log[1] + ': ' + list[i] + '<br/></li>');
}
}
}
});
}
//https://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
$(function(){
$('#logs').hide();
$('#show-logs').click(function(){
$(this).hide();
$('#logs').show();
});
$('#export').click(function(){
mystorage.all(function(items){
var saveItems = {};
for (var k in items)
if (!k.match(/^log/))
saveItems[k] = items[k];
download('imagefilter_options.json', JSON.stringify(saveItems, null, 2));
});
});
$('#downloadlog').click(function(){
var logs = $('#logs');
logs = logs[0].innerText || logs[0].textContent;
download('imagefilter_log.txt', logs);
});
$('#importfile').on('change', function(){
if (!this.files.length)
return;
var file = this.files[0];
fr = new FileReader();
fr.onload = function(){
var options = JSON.parse(fr.result);
mystorage.set(options);
mylogger.log('imported ' + options.length + ' options from file');
for (var key in options)
{
mymessages.sendTabs({key:key, value:options[key]});
}
};
fr.readAsText(file);
});
$('#import').click(function(){
$('#importfile').trigger('click');
});
$('#clearoptions').click(function(){
if (confirm("This will remove all options, including custom filters, and restore the defaults. Are you sure?"))
mystorage.clear(function(){
location.reload();
});
});
$('#newfilter').click(function(){
findUniqueFilterName("NewFilter", function(name){
createFilter(name, "", "", "");
});
});
$('.option').on('change paste keydown input', function(event){
var value = this.type == 'checkbox' ? this.checked : this.value;
if (value == shortcutNone) //handle shortcut "none"
value = null;
var data = {};
data[this.name] = value;
console.log(this.name, value);
mystorage.set(data);
mylogger.log('options sets ' + this.name + '=' + value);
mymessages.sendTabs({key:this.name, value:value});
});
$(document).on("click", ".shortcut", function() {
var button = $(this);
button.addClass('recording');
Mousetrap.record({partialCallback:function(sequence){
//partial sequence
button.val(sequence.join(' '));
}}, function(sequence) {
//final sequence
//FIXME: stupid hack because of broken library
if (sequence.indexOf('shift') >= 0)
{
sequence.splice(sequence.indexOf('shift'), 1);
sequence.unshift('shift');
}
if (sequence.indexOf('alt') >= 0)
{
sequence.splice(sequence.indexOf('alt'), 1);
sequence.unshift('alt');
}
if (sequence.indexOf('ctrl') >= 0)
{
sequence.splice(sequence.indexOf('ctrl'), 1);
sequence.unshift('ctrl');
}
var str = sequence.join(' ');
if (!str || str == 'del' || str == 'esc')
str = shortcutNone;
button.val(str);
button.removeClass('recording');
button.trigger('change');
});
});
$(".shortcut").val(shortcutNone);
assertDefaultsAreLoaded(loadOptions);
});