-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
119 lines (108 loc) · 3.12 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
function populateCurrent(config) {
$('#current .name').text(config.name);
$('#current .logo').attr('src', config.logo);
$('#current').show();
}
function populateForm(config) {
for (field in config) {
$('#'+field).val(config[field]);
}
$('#logoPreview').attr('src', config.logo);
$('#logoFile').val('');
}
function loadImage(file) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function(event) {
resolve(event.target.result);
}
reader.onerror = reject;
reader.readAsDataURL(file)
});
}
function importConfig(file) {
return new Promise(function(resolve, reject) {
var reader = new FileReader()
reader.onload = function(event) {
var config;
try {
config = JSON.parse(event.target.result);
} catch (e) {
reject(new Error('Invalid configuration file'));
}
resolve(config);
}
reader.onerror = reject;
reader.readAsText(file)
}).then(function(config) {
populateForm(config);
$('#form').submit();
});
}
// Stop default browser actions
$(window).bind('dragover dragleave', function(event) {
event.stopPropagation()
event.preventDefault()
})
// Catch drop event
$(window).bind('drop', function(event) {
// Stop default browser actions
event.stopPropagation()
event.preventDefault()
// Get all files that are dropped
files = event.originalEvent.target.files || event.originalEvent.dataTransfer.files
importConfig(files[0]).catch(function(error) {
alert(error.message);
});
return false;
})
$('#logoFile').change(function(event) {
loadImage(event.target.files[0]).then(function(dataUrl) {
$('#logo').val(dataUrl);
$('#logoPreview').attr('src', dataUrl);
}).catch(function(error) {
$('#logoFile').val('');
alert(error.message);
});
});
$('#importFile').change(function(event) {
importConfig(event.target.files[0]).then(function() {
$('#importFile').val('');
}).catch(function(error) {
$('#importFile').val('');
alert(error.message);
});
});
function handleSubmit(event) {
event.preventDefault();
var config = {
serverUrl: $('#serverUrl').val(),
authToken: $('#authToken').val(),
name: $('#name').val(),
logo: $('#logo').val()
};
SESConfig.saveConfiguration(config);
populateCurrent(config);
chrome.runtime.sendMessage(null, 'updateNameAndImage');
}
$('#form').submit(handleSubmit);
$('#export').click(function handleExport(event) {
handleSubmit(event);
var config = SESConfig.getSelectedConfiguration();
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(config));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "suspicious-email-submitter-" + config.name + ".json");
downloadAnchorNode.click();
downloadAnchorNode.remove();
});
try {
var config = SESConfig.getSelectedConfiguration();
populateForm(config);
populateCurrent(config);
} catch(e) {
console.log(e);
$('#current').hide();
$('#editor').show();
// not configured yet.
}