-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
78 lines (65 loc) · 2.13 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
var formOptions = document.getElementById('optionsForm');
var inAccessToken = document.getElementById('inputAccessToken');
var inDomain = document.getElementById('inputDomain');
var inCollection = document.getElementById('inputCollection');
var txtStatus = document.getElementById('status');
var btnSave = document.getElementById('save');
// Saves options to chrome.storage.sync.
function save_options() {
if (formOptions.checkValidity()) {
chrome.storage.sync.set({
userAccessToken: inAccessToken.value,
userDomain: inDomain.value,
userCollection: inCollection.value
}, function() {
// Update status to let user know options were saved.
txtStatus.textContent = '> Options saved, you are good to GO!';
// reset status message
setTimeout(function() {
txtStatus.textContent = '';
}, 2000
);
});
}else{
if(!inDomain.checkValidity()){
txtStatus.textContent = inDomain.validationMessage;
}
if(!inCollection.checkValidity()){
txtStatus.textContent = inCollection.validationMessage;
}
if(!inAccessToken.checkValidity()){
txtStatus.textContent = inAccessToken.validationMessage;
}
}
event.preventDefault();
}
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restoreOptions() {
// Set defaults or store uservalues
chrome.storage.sync.get({
userAccessToken: '<insert>',
userDomain: 'https://',
userCollection: 'DefaultCollection'
}, function(storedItems) {
inAccessToken.value = storedItems.userAccessToken;
inDomain.value = storedItems.userDomain;
inCollection.value = storedItems.userCollection;
});
}
function focusInAccessToken(){
if(inAccessToken.value=='<insert>')
inAccessToken.value='';
}
function blurInAccessToken(){
if(inAccessToken.value=='')
inAccessToken.value='<insert>';
}
function whenPageHasLoaded()
{
btnSave.addEventListener('click', save_options);
inAccessToken.addEventListener('focus', focusInAccessToken);
inAccessToken.addEventListener('blur', blurInAccessToken);
restoreOptions();
}
document.addEventListener('DOMContentLoaded', whenPageHasLoaded());