forked from softvar/enhanced-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
69 lines (59 loc) · 2.24 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
/* global chrome */
function validateUserToken(token) {
var invalidTokenMsg;
if (typeof token !== 'string') {
invalidTokenMsg = 'Access Token should be a String eg: 17c1a8d5b399d66b6212382d98d4c67a94d58955';
} else if (token.length < 30) {
invalidTokenMsg = 'Access Token length appears wrong!';
}
if (invalidTokenMsg) {
document.getElementById('validation-block').style.display = 'inline-block';
} else {
document.getElementById('validation-block').style.display = 'none';
}
return invalidTokenMsg;
}
// Saves options to chrome.storage
function saveOptions() {
document.getElementById('save-btn').setAttribute('disabled', 'disabled');
document.getElementById('save-btn').style.background = '#DEDEDE';
var token = document.getElementById('x-github-token').value;
chrome.storage.sync.set(
{
'x-github-token': token
},
function() {
// Update statusText to let user know options were saved.
var statusText = document.getElementById('status--text');
var validationWarning = document.getElementById('validation-warning');
statusText.textContent = 'Options saved!!';
validationWarning.textContent = validateUserToken(token);
document.getElementById('status').style.display = 'inline-block';
setTimeout(function() {
statusText.textContent = '';
document.getElementById('status').style.display = 'none';
document.getElementById('save-btn').removeAttribute('disabled');
document.getElementById('save-btn').style.background = '#3fb594';
}, 1500);
}
);
}
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restoreOptions() {
var token;
chrome.storage.sync.get(
{
'x-github-token': ''
},
function(storedData) {
token = storedData['x-github-token'];
document.getElementById('x-github-token').value = token;
var validationWarning = document.getElementById('validation-warning');
validationWarning.textContent = validateUserToken(token);
document.getElementById('save-btn').style.background = '#3fb594';
}
);
}
document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('save-btn').addEventListener('click', saveOptions);