-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.js
76 lines (69 loc) · 2.67 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
function isEmpty(str) {
return !str.trim().length;
}
function updateStatus(msg) {
var status = document.getElementById('status');
status.textContent = msg;
setTimeout(function() {
status.textContent = '';
}, 1500);
}
const isValidEmail = (email) => {
return String(email)
.toLowerCase()
.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);
};
function save_options() {
var username = document.getElementById('displayname').value;
var email = document.getElementById('emailAddress').value;
var shouldOpenGarminbadgesMainPage = document.getElementById('shouldOpenGarminbadgesMainPage').checked;
var shouldOpenGarminbadgesChallengePage = document.getElementById('shouldOpenGarminbadgesChallengePage').checked;
var shouldShowSuccessfulAlert = document.getElementById('shouldShowSuccessfulAlert').checked;
//Validate input
if(isEmpty(username) || isEmpty(email)) {
updateStatus("All fields are required.")
return;
} else if(!isValidEmail(email)) {
updateStatus("Not a valid email address.")
return;
}
chrome.storage.sync.set({
username: username,
email: email,
shouldOpenGarminbadgesMainPage: shouldOpenGarminbadgesMainPage,
shouldOpenGarminbadgesChallengePage: shouldOpenGarminbadgesChallengePage,
shouldShowSuccessfulAlert: shouldShowSuccessfulAlert
}, function() {
// Update status to let user know options were saved.
updateStatus("Options saved.");
});
/*chrome.storage.sync.set({
username: '',
email: '',
shouldOpenGarminbadgesMainPage: true,
shouldOpenGarminbadgesChallengePage: false
shouldShowSuccessfulAlert: true
}, function() {
// Update status to let user know options were saved.
updateStatus("Options saved.");
});*/
}
function restore_options() {
chrome.storage.sync.get({
username: '',
email: '',
shouldOpenGarminbadgesMainPage: true,
shouldOpenGarminbadgesChallengePage: false,
shouldShowSuccessfulAlert: true
}, function(data) {
document.getElementById('displayname').value = data.username;
document.getElementById('emailAddress').value = data.email;
document.getElementById('shouldOpenGarminbadgesMainPage').checked = data.shouldOpenGarminbadgesMainPage;
document.getElementById('shouldOpenGarminbadgesChallengePage').checked = data.shouldOpenGarminbadgesChallengePage;
document.getElementById('shouldShowSuccessfulAlert').checked = data.shouldShowSuccessfulAlert;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('saveButton').addEventListener('click', save_options);