You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.2 KiB
JavaScript
63 lines
2.2 KiB
JavaScript
const stammTVBaseUrl = document.getElementById('stammtv-base-url');
|
|
const stammTVWSSUrl = document.getElementById('stammtv-wss-url');
|
|
const saveButton = document.getElementById('save');
|
|
|
|
// Saves options to chrome.storage
|
|
function save_options() {
|
|
let valid = true;
|
|
if (stammTVBaseUrl.value.startsWith('http')) {
|
|
stammTVBaseUrl.classList.add('is-valid');
|
|
valid = valid && true;
|
|
} else {
|
|
stammTVBaseUrl.classList.add('is-invalid');
|
|
valid = false;
|
|
}
|
|
|
|
if (stammTVWSSUrl.value.startsWith('wss://')) {
|
|
stammTVWSSUrl.classList.add('is-valid');
|
|
valid = valid && true;
|
|
} else {
|
|
stammTVWSSUrl.classList.add('is-invalid');
|
|
valid = false;
|
|
}
|
|
|
|
if(valid) {
|
|
if(chrome && chrome.storage && chrome.storage.local) {
|
|
chrome.storage.local.set({
|
|
baseURL: stammTVBaseUrl.value,
|
|
wssURL: stammTVWSSUrl.value
|
|
}, function () {
|
|
// Update form controls to let user know options were saved.
|
|
saveButton.classList.replace('btn-primary', 'btn-success');
|
|
saveButton.classList.replace('btn-danger', 'btn-success');
|
|
saveButton.textContent = '... saved!';
|
|
});
|
|
} else {
|
|
console.error('Unsupported browser or no access to localStorage')
|
|
}
|
|
} else {
|
|
console.error('Failed to save - wrong information given.')
|
|
saveButton.classList.replace('btn-primary', 'btn-danger');
|
|
saveButton.classList.replace('btn-success', 'btn-danger');
|
|
saveButton.textContent = '... failed to save!';
|
|
}
|
|
}
|
|
|
|
// Restores select box and checkbox state using the preferences
|
|
// stored in chrome.storage.
|
|
function load_options() {
|
|
if(chrome && chrome.storage && chrome.storage.local) {
|
|
chrome.storage.local.get({
|
|
baseURL: 'Please fill in.',
|
|
wssURL: 'Please fill in.'
|
|
}, function (items) {
|
|
stammTVBaseUrl.value = items.baseURL;
|
|
stammTVWSSUrl.value = items.wssURL;
|
|
});
|
|
} else {
|
|
console.error('Unsupported browser or no access to localStorage')
|
|
}
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', load_options);
|
|
saveButton.addEventListener('click', save_options); |