-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
82 lines (77 loc) · 2.42 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
function validateWhitelist(input) {
var invalidServers = [];
var whitelistRegExps = [];
input = input.trim();
if (input !== "*") {
var list = input.split(",");
for(var i=0; i < list.length; i++) {
var server = list[i].trim();
if (!server.match(/salesforce\.com$/)) invalidServers.push(server);
else {
var re = server.replace(/\.?salesforce\.com$/,".salesforce.com").
replace(/\./g,"\\.").replace(/\*/g,".*");
try {
new RegExp(re);
whitelistRegExps.push(re);
} catch(e) {
// Invalid regular expression
invalidServers.push(server);
}
}
}
}
return {"invalidServers": invalidServers, "whitelistRegExps": whitelistRegExps.join()};
}
function showStatus(message, success) {
var status = document.getElementById('status');
status.textContent = message;
if (success) {
setTimeout(function() {
status.textContent = '';
window.close();
}, 750);
}
}
function clearStatus() {
showStatus('', false);
}
// Saves options to browser.storage.sync.
function save_options() {
clearStatus();
var input = document.getElementById('whitelistServers').value;
if (input.trim() === "") {
input = "*";
document.getElementById('whitelistServers').value = input;
}
var whitelistServers = input;
var v = validateWhitelist(whitelistServers);
if (v["invalidServers"].length > 0) {
showStatus("Invalid server entries: " + v["invalidServers"].join(), false);
} else {
var debugEnabled = document.getElementById("debug").selectedIndex;
browser.storage.sync.set({
whitelistServers: whitelistServers,
whitelistRegExps: v["whitelistRegExps"],
debug: debugEnabled
}).then( function() {
// Update status to let user know options were saved.
document.getElementById("save").disabled = true;
showStatus("Options saved.", true);
});
}
}
// Restores the preferences
// stored in browser.storage
function restore_options() {
// Use default value color = 'red' and likesColor = true.
browser.storage.sync.get({
whitelistServers: '*',
debug: 1
}).then( function(items) {
document.getElementById('whitelistServers').value = items.whitelistServers;
document.getElementById("debug").selectedIndex = items.debug;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
save_options);