share-to-mastodon/js/settings.js

38 lines
1.1 KiB
JavaScript

// Function for populating settings
async function loadSettings() {
// Retrieve settings from storage
const server = document.getElementById('mastodon-server')
new Promise(function (resolve) {
chrome.storage.sync.get(function (data) {
console.log(data)
// Server setting
if (data.userServer) {
server.value = data.userServer
} else {
document.querySelector('#mastodon-server-alert').classList.remove('d-none')
}
resolve()
})
}).then(function () {
// Allow interaction on settings
server.removeAttribute('disabled')
})
}
// Save settings after any input change
document.querySelectorAll('input,select').forEach(function (el) {
el.addEventListener('change', function () {
chrome.storage.sync.set({
userServer: document.querySelector('#mastodon-server').value,
}, function() {
console.log('Settings saved')
})
})
})
// Open keyboard shortcut
document.querySelector('#mastodon-keyboard-shortcut').addEventListener('click', function() {
chrome.tabs.create({ url: 'chrome://extensions/shortcuts#:~:text=Share%20to%20Mastodon' })
})
loadSettings()