Repository navigation
Expand file tree
/
Copy pathoptions.js
More file actions
64 lines (57 loc) · 1.94 KB
/
Copy pathoptions.js
File metadata and controls
64 lines (57 loc) · 1.94 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
'use strict'
const defaultAPIURL = 'https://api.runmycode.online/run'
const $ = s => document.querySelector(s)
const $$ = s => document.querySelectorAll(s)
const editBtn = $('#edit')
const error = $('#error')
const apiUrl = $('#api-url')
const apiKey = $('#api-key')
const saveBtn = $('#save')
const inputs = Array.from($$('input'))
const toggleInputs = () => {
inputs.forEach((el) => {
if (el.type === 'button') el.disabled = !el.disabled
else if (el.type === 'text') {
el.getAttribute('readonly') ? el.removeAttribute('readonly') : el.setAttribute('readonly', 'readonly')
}
})
}
const enableEdit = () => {
saveBtn.value = 'Save'
toggleInputs()
}
const saveOptions = (e) => {
if (apiUrl.value === '') apiUrl.value = defaultAPIURL
error.style.display = 'block'
if (apiUrl.value !== defaultAPIURL && apiUrl.value.indexOf('.amazonaws.com') === -1) {
error.textContent = `Only ${defaultAPIURL} and AWS API Gateway URLs are supported as API URL`
return
} else if (!apiKey.value && apiUrl.value === defaultAPIURL) {
error.textContent = 'Key cannot be empty for https://api.runmycode.online/run'
return
// key may be not required for custom RunMyCode deployment
}
error.style.display = 'none'
browser.storage.local.set({
'apiUrl': apiUrl.value,
'apiKey': apiKey.value
})
saveBtn.value = 'Saved'
if (e) toggleInputs() // toggle inputs only if save from btn click
}
const restoreOptions = () => {
const getApiUrl = browser.storage.local.get('apiUrl')
const getApiKey = browser.storage.local.get('apiKey')
Promise.all([getApiUrl, getApiKey])
.then((result) => {
apiUrl.value = result[0]['apiUrl'] || defaultAPIURL
apiKey.value = result[1]['apiKey'] || ''
saveOptions()
})
.catch((error) => {
console.log('Error: ', error)
})
}
document.addEventListener('DOMContentLoaded', restoreOptions)
saveBtn.addEventListener('click', saveOptions)
editBtn.addEventListener('click', enableEdit)