-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptions.js
197 lines (176 loc) · 6.02 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
let usernameInput = document.getElementById('username');
let passwordInput = document.getElementById('password');
let serverIpInput = document.getElementById('serverIp');
let serverPortInput = document.getElementById('serverPort');
let serverPathInput = document.getElementById('serverPath');
let useHTTPSInput = document.getElementById('useHTTPS');
let spinnerDiv = document.getElementById('spinnerDiv');
let loginStatusOKDiv = document.getElementById('loginStatusOK');
let loginStatusKODiv = document.getElementById('loginStatusKO');
let currentURL = document.getElementById('currentURL');
let saveButton = document.getElementById('saveButton');
let loginButton = document.getElementById('loginButton');
let loginButtonModal = document.getElementById('loginButtonModal');
let alertDanger = document.getElementById('alertDanger');
let loginModal = $('#loginModal');
function enableSpinner() {
spinnerDiv.innerHTML = `
<div class="spinner-border text-primary m-3"></div>
<div>Checking status...</div>
`;
}
function disableSpinner() {
spinnerDiv.innerHTML = ``;
}
function setDangerMessage(message, timeout=3000) {
if (!message) {
alertDanger.hidden = true;
return;
}
alertDanger.innerText = message;
alertDanger.hidden = false;
if (timeout > 0) {
setTimeout(function() {
alertDanger.hidden = true;
alertDanger.innerText = '';
}, timeout);
}
}
function getProtocol() {
return useHTTPSInput.checked ? 'https' : 'http';
}
function updateLoggedInStatus(callback) {
saveButton.disabled = true;
loginStatusOKDiv.hidden = true;
loginStatusKODiv.hidden = true;
loginButton.hidden = true;
enableSpinner();
isLoggedIn(function(loggedIn, unauthorized, error) {
disableSpinner();
loginStatusOKDiv.hidden = !loggedIn;
loginStatusKODiv.hidden = loggedIn;
loginStatusKODiv.innerHTML = `<i class="fa fa-times small mr-3"></i>`
if (!loggedIn && unauthorized) {
loginStatusKODiv.innerHTML += `Please log in`;
} else {
loginStatusKODiv.innerHTML += error ? error : `You are not logged in`;
}
loginButton.hidden = !unauthorized;
saveButton.disabled = false;
if (callback) callback();
});
}
function requestPermission(callback) {
chrome.permissions.contains({
origins: [`${origin}/`]
}, function(result) {
if (!result) {
chrome.permissions.request({
origins: [`${origin}/`]
}, function(granted) {
if (callback) {
if (!granted) {
alert('Not granting this permission will make the extension unusable.');
}
callback(granted);
}
});
} else if (callback) {
callback(true);
}
});
}
function validHost(str) {
let pattern = new RegExp('^((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))$'); // OR ip (v4) address
return !!pattern.test(str);
}
function validateForm() {
// Host
const value = serverIpInput.value;
const isLocalhost = (value === 'localhost');
const isServerIPValid = validHost(value) || isLocalhost;
if (isServerIPValid) {
serverIpInput.classList.remove('is-invalid');
} else {
serverIpInput.classList.add('is-invalid');
saveButton.disabled = true;
}
// Port
const isValidPort = /\d+/.test(serverPortInput.value);
if (isValidPort) {
serverPortInput.classList.remove('is-invalid');
} else {
serverPortInput.classList.add('is-invalid');
saveButton.disabled = true;
}
// Path
const isValidPath = /^((\/[.\w-]+)*\/{0,1}|\/)$/.test(serverPathInput.value);
if (isValidPath) {
serverPathInput.classList.remove('is-invalid');
} else {
serverPathInput.classList.add('is-invalid');
saveButton.disabled = true;
}
}
function requireSaving() {
updateCurrentURL();
if (xhr !== null) {
xhr.abort();
}
if (serverIpInput.value === serverIp &&
parseInt(serverPortInput.value) === parseInt(serverPort) &&
useHTTPSInput.checked === (serverProtocol === 'https') &&
serverPathInput.value === serverPath) {
updateLoggedInStatus();
} else {
saveButton.disabled = false;
loginStatusOKDiv.hidden = true;
loginStatusKODiv.hidden = true;
loginButton.hidden = true;
}
validateForm();
}
function updateCurrentURL() {
portString = `:${serverPortInput.value}`;
if ((useHTTPSInput.checked && serverPortInput.value === '443') || (!useHTTPSInput.checked && serverPortInput.value === '80')) {
portString = '';
}
currentURL.innerHTML = `${useHTTPSInput.checked ? 'https' : 'http'}://${serverIpInput.value}${portString}${serverPathInput.value}`;
}
saveButton.onclick = function(ev) {
setOrigin(serverIpInput.value, serverPortInput.value, getProtocol(), serverPathInput.value, function() {
requestPermission(function(granted) {
updateLoggedInStatus();
});
});
};
loginButton.onclick = function(ev) {
loginModal.modal('show');
}
loginButtonModal.onclick = function(ev) {
setDangerMessage('');
login(usernameInput.value, passwordInput.value, function(success, error_msg) {
if (success) {
loginModal.modal('hide');
updateLoggedInStatus();
} else {
setDangerMessage(error_msg, 0);
}
});
}
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
pullStoredData(function() {
serverIpInput.value = serverIp;
serverPortInput.value = serverPort;
serverPathInput.value = serverPath;
useHTTPSInput.checked = serverProtocol === 'https';
updateCurrentURL();
serverIpInput.oninput = requireSaving;
serverPortInput.oninput = requireSaving;
useHTTPSInput.oninput = requireSaving;
serverPathInput.oninput = requireSaving;
updateLoggedInStatus();
});