-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptions.js
More file actions
99 lines (86 loc) · 3.14 KB
/
options.js
File metadata and controls
99 lines (86 loc) · 3.14 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
98
99
var apiKeyInput = document.getElementById('apiKeyInput');
var remoteOptionsSelect = document.getElementById('remoteOptionsSelect');
var saveApiKeyBtn = document.getElementById('saveApiKeyBtn');
var saveRemoteOptionBtn = document.getElementById('saveRemoteOptionBtn');
var statusDiv = document.querySelector('.status-div');
restoreOptions();
setEventHandlers();
function setEventHandlers() {
saveApiKeyBtn.addEventListener('click', function () {
var apiKey = apiKeyInput.value;
if (apiKey == "")
apiKey = null;
chrome.storage.local.set({
apiKey: apiKey
}, function () {
chrome.runtime.sendMessage({
action: "setApiKey",
newApiKey: apiKey
});
statusDiv.innerText = 'Your Api Key has been successfully changed!';
});
});
saveRemoteOptionBtn.addEventListener('click', function () {
var remoteOptionId = remoteOptionsSelect.value;
if (remoteOptionId != "default") {
chrome.storage.local.set({
remoteOptionId: remoteOptionId
}, function () {
chrome.runtime.sendMessage({
action: "setRemoteOptionId",
newRemoteOptionId: remoteOptionId
});
statusDiv.innerText = 'Your default remote account has been successfully changed!';
});
} else {
chrome.storage.local.remove('remoteOptionId', function () {
chrome.runtime.sendMessage({
action: "removeRemoteOptionId"
});
});
}
});
}
function restoreOptions() {
chrome.storage.local.get(['apiKey', 'remoteOptionId'], function (object) {
if (object.apiKey != null) {
apiKeyInput.value = object.apiKey;
getRemoteOptionsRequest(object.apiKey)
.then((data) => {
setRemoteOptions(data, object.remoteOptionId);
})
.catch((err) => {
});
}
});
}
function setRemoteOptions(data, lastRemoteOptionId) {
var remoteOptionsArray = JSON.parse(data).data;
remoteOptionsArray.forEach(function (obj) {
var type = obj.type;
var username = obj.username;
var value = obj.remoteOptionId;
var text = type + " - " + username;
var option = document.createElement('option');
option.value = value;
option.textContent = text;
remoteOptionsSelect.appendChild(option);
});
if (lastRemoteOptionId != null)
remoteOptionsSelect.value = lastRemoteOptionId;
}
function getRemoteOptionsRequest(apiKey) {
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.readyState == 4) {
resolve(this.responseText);
}
}
request.onerror = function () {
reject(new Error('Request failed!'));
}
request.open("POST", "https://offcloud.com/api/remote-account/list?apikey=" + apiKey, true);
request.send();
});
}