-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoptions.js
170 lines (152 loc) · 5.22 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
let serverAPIKeyInput = document.getElementById('serverAPIKey');
let serverIpInput = document.getElementById('serverIp');
let serverPathInput = document.getElementById('serverPath');
let serverPortInput = document.getElementById('serverPort');
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 alertDanger = document.getElementById('alertDanger');
function enableSpinner() {
spinnerDiv.innerHTML = `
<div class="spinner-border text-primary m-3"></div>
<div class="text-white">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;
enableSpinner();
isLoggedIn(function(loggedIn) {
disableSpinner();
loginStatusOKDiv.hidden = !loggedIn;
loginStatusKODiv.hidden = loggedIn;
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;
}
// 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;
}
// API key
const isValidAPIKey = /^[\w=]{60,70}$/.test(serverAPIKeyInput.value);
if (isValidAPIKey) {
serverAPIKeyInput.classList.remove('is-invalid');
} else {
serverAPIKeyInput.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') &&
serverAPIKeyInput.value === serverAPIKey &&
serverPathInput.value === serverPath) {
updateLoggedInStatus();
} else {
saveButton.disabled = false;
loginStatusOKDiv.hidden = true;
loginStatusKODiv.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(serverAPIKeyInput.value, serverIpInput.value, serverPortInput.value, getProtocol(), serverPathInput.value, function() {
requestPermission(function(granted) {
updateLoggedInStatus();
});
});
};
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
pullStoredData(function() {
serverAPIKeyInput.value = serverAPIKey;
serverIpInput.value = serverIp;
serverPortInput.value = serverPort;
serverPathInput.value = serverPath;
useHTTPSInput.checked = serverProtocol === 'https';
updateCurrentURL();
serverAPIKeyInput.oninput = requireSaving;
serverIpInput.oninput = requireSaving;
serverPortInput.oninput = requireSaving;
serverPathInput.oninput = requireSaving;
useHTTPSInput.oninput = requireSaving;
updateLoggedInStatus();
});