forked from softvar/enhanced-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
97 lines (84 loc) · 3.67 KB
/
options.js
File metadata and controls
97 lines (84 loc) · 3.67 KB
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
/* 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.opacity = 1;
document.getElementById('validation-block').style.display = 'inline-block';
} else {
document.getElementById('validation-block').style.opacity = 0;
document.getElementById('validation-block').style.display = 'hidden';
}
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;
var showRepoSize = document.getElementById('show-repo-size').checked;
var showFileSize = document.getElementById('show-file-size').checked;
var showDownloadLink = document.getElementById('show-download-link').checked;
var showCopyFileButton = document.getElementById('show-copy-file-button').checked;
var wrapCodeLines = document.getElementById('wrap-code-lines').checked;
chrome.storage.sync.set(
{
'x-github-token': token,
'show-repo-size': showRepoSize,
'show-file-size': showFileSize,
'show-download-link': showDownloadLink,
'show-copy-file-button': showCopyFileButton,
'wrap-code-lines': wrapCodeLines
},
function() {
// Update statusText to let user know options were saved.
var statusText = document.getElementById('status--text');
var validationWarning = document.getElementById('validation-warning');
var validationMessage = validateUserToken(token);
if (!validationMessage) {
statusText.textContent = 'Options saved!!';
document.getElementById('status').style.display = 'inline-block';
}
validationWarning.textContent = validationMessage;
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': '',
'show-repo-size': true,
'show-file-size': true,
'show-download-link': true,
'show-copy-file-button': true,
'wrap-code-lines': false
},
function(storedData) {
token = storedData['x-github-token'];
document.getElementById('x-github-token').value = token;
document.getElementById('show-repo-size').checked = storedData['show-repo-size'];
document.getElementById('show-file-size').checked = storedData['show-file-size'];
document.getElementById('show-download-link').checked = storedData['show-download-link'];
document.getElementById('show-copy-file-button').checked = storedData['show-copy-file-button'];
document.getElementById('wrap-code-lines').checked = storedData['wrap-code-lines'];
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);