-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
215 lines (177 loc) · 7.05 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
function getRandomColor() {
var red = Math.floor(Math.random() * 256);
var green = Math.floor(Math.random() * 256);
var blue = Math.floor(Math.random() * 256);
// Convert the values to a hexadecimal string and return it
return "#" + componentToHex(red) + componentToHex(green) + componentToHex(blue);
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
const saveButton = document.getElementById('save-button')
const exportButton = document.getElementById('export-button')
const importButton = document.getElementById('import-button')
const accountIdInput = document.getElementById('account-input')
const accountNameInput = document.getElementById('name-input')
const accountColorInput = document.getElementById('color-input')
const awsAccountsDiv = document.getElementById('aws_accounts');
accountColorInput.value = getRandomColor()
exportButton.addEventListener('click', () => {
chrome.storage.sync.get(['aws_accounts'], result => {
if (result.aws_accounts.length > 0) {
const jsonData = result.aws_accounts
const textArea = document.getElementById('accounts_export')
textArea.value = JSON.stringify(jsonData, null, 2);
document.getElementById('div_accounts_export').hidden = false
textArea.select();
}
})
})
function mergeJsonData(mergedData, newData) {
newData.forEach(function (obj) {
if (!mergedData.some(search => search.accountID === obj.accountID && search.color == obj.color && search.name == obj.name)) {
mergedData.push(obj)
}
});
return mergedData
}
importButton.addEventListener('click', () => {
let fileInput = document.getElementById('fileInput');
var accountImportedJsonData = []
for (const file of fileInput.files) {
let reader = new FileReader();
reader.onload = function (e) {
let fileContent = e.target.result;
let jsonData = JSON.parse(fileContent);
mergeJsonData(accountImportedJsonData, jsonData);
clear_accounts_table()
saveAccountsToStorage(accountImportedJsonData)
render(accountImportedJsonData)
};
reader.readAsText(file);
}
})
chrome.storage.sync.get(['aws_accounts'], result => {
if (result.aws_accounts == null) {
console.log('Extension init...')
chrome.storage.sync.set({aws_accounts: []}, () => {
console.log('Init aws accounts storage...')
})
}
})
chrome.storage.sync.get(['new_account_id'], result => {
if (result !== null) {
accountIdInput.value = result.new_account_id
const obj = {};
obj['new_account_id'] = null;
chrome.storage.sync.set(obj, () => {
});
}
})
function clear_accounts_table() {
const awsAccountsDiv = document.getElementById("aws_accounts");
while (awsAccountsDiv.firstChild) {
awsAccountsDiv.removeChild(awsAccountsDiv.firstChild);
}
}
function deleteAccount(accountID) {
chrome.storage.sync.get(['aws_accounts'], result => {
const filteredAccounts = result.aws_accounts.filter(account => account.accountID !== accountID);
saveAccountsToStorage(filteredAccounts)
clear_accounts_table()
render_accounts_table()
})
}
function addAccount(aws_account) {
chrome.storage.sync.get(['aws_accounts'], result => {
result.aws_accounts.push(aws_account)
saveAccountsToStorage(result.aws_accounts)
clear_accounts_table()
render_accounts_table()
clear_account_input()
})
}
function saveAccountsToStorage(accounts) {
const obj = {};
obj['aws_accounts'] = accounts;
chrome.storage.sync.set(obj)
exportButton.style.display = (accounts.length > 0) ? 'block' : 'none'
}
function updateAccount(accountID, accountName, color) {
chrome.storage.sync.get(['aws_accounts'], result => {
const updatedAccounts = result.aws_accounts.map(account => (account.accountID === accountID) ? {...account, name: accountName, color: color} : account);
saveAccountsToStorage(updatedAccounts)
})
}
let render = (aws_accounts) => {
exportButton.style.display = (aws_accounts.length > 0) ? 'block' : 'none'
if (aws_accounts.length > 0) {
const table = document.createElement('table');
table.className = "table table-hover"
table.innerHTML = `
<thead>
<tr>
<th>Color</th>
<th>Account ID</th>
<th>Alias</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
</tbody>
`;
const tbody = table.querySelector('tbody');
aws_accounts.forEach(account => {
const row = document.createElement('tr');
row.innerHTML = `
<td>
<div class="colorcontainer">
<input id="color_${account.accountID}" type="color" value="${account.color}" class="circle"/>
</div>
</td>
<td>${account.accountID}</td>
<td><input type="text" id="account_name_${account.accountID}" value="${account.name}" class="form-control me-2"></td>
<td><button id="update_${account.accountID}" class="btn btn-warning">Update</button></td>
<td><button id="del_${account.accountID}" class="btn btn-danger">Delete</button></td>
`;
tbody.appendChild(row);
const deleteButton = row.querySelector(`#del_${account.accountID}`);
const updateButton = row.querySelector(`#update_${account.accountID}`);
deleteButton.addEventListener("click", function () {
const buttonId = deleteButton.id; // Get the unique ID of the clicked button
const accountID = buttonId.replace(new RegExp(`^${'del_'}`), '');
deleteAccount(accountID)
})
updateButton.addEventListener("click", function () {
const buttonId = updateButton.id; // Get the unique ID of the clicked button
const accountID = buttonId.replace(new RegExp(`^${'update_'}`), '');
const inputAccountName = document.getElementById(`account_name_${accountID}`);
const color = document.getElementById(`color_${accountID}`).value;
const accountName = inputAccountName.value;
updateAccount(accountID, accountName, color)
})
})
// Inject the table into the div with id "aws_accounts"
awsAccountsDiv.appendChild(table);
} else {
console.log('no accounts')
}
}
function render_accounts_table() {
// Populate the table with data from the accounts array
chrome.storage.sync.get(['aws_accounts'], result => render(result.aws_accounts))
}
render_accounts_table()
function clear_account_input() {
accountIdInput.value = '';
accountNameInput.value = '';
}
saveButton.addEventListener('click', () => {
// TODO: add user input validation
let aws_account = {
accountID: accountIdInput.value, name: accountNameInput.value, color: accountColorInput.value
}
addAccount(aws_account)
})