const stammTVBaseUrl = document.getElementById('stammtv-base-url'); const stammTVWSSUrl = document.getElementById('stammtv-wss-url'); const saveButton = document.getElementById('save'); function validateAndSave() { let valid = true; if (stammTVBaseUrl.value.startsWith('http://') || stammTVBaseUrl.value.startsWith('https://')) { stammTVBaseUrl.classList.remove('is-invalid'); stammTVBaseUrl.classList.add('is-valid'); valid = valid && true; } else { stammTVBaseUrl.classList.remove('is-valid'); stammTVBaseUrl.classList.add('is-invalid'); valid = false; } if (stammTVWSSUrl.value.startsWith('wss://')) { stammTVWSSUrl.classList.remove('is-invalid'); stammTVWSSUrl.classList.add('is-valid'); valid = valid && true; } else { stammTVWSSUrl.classList.remove('is-valid'); stammTVWSSUrl.classList.add('is-invalid'); valid = false; } if (valid) { save(); } else { console.log('Failed to save settings - form validation was not successful.') saveButton.classList.replace('btn-primary', 'btn-danger'); saveButton.classList.replace('btn-success', 'btn-danger'); saveButton.textContent = getMessage('settingsSaveButtonCaptionFailure'); } } // Saves options to chrome.storage function save() { 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 = getMessage('settingsSaveButtonCaptionSuccess'); }); } else { console.error('Unsupported browser or no access to localStorage') } } // Restores select box and checkbox state using the preferences // stored in chrome.storage. function load() { if (chrome && chrome.storage && chrome.storage.local) { chrome.storage.local.get({ baseURL: getMessage("settingsFormInputRequired"), wssURL: getMessage("settingsFormInputRequired") }, function (items) { stammTVBaseUrl.value = items.baseURL; stammTVWSSUrl.value = items.wssURL; }); } else { console.error('Unsupported browser or no access to localStorage') } } document.addEventListener('DOMContentLoaded', load); saveButton.addEventListener('click', validateAndSave);