share-to-mastodon/js/settings.js

70 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-02-10 07:30:26 +01:00
const serverList = document.querySelector('#mastodon-server-list')
// Function to load settings from storage
function loadSettings() {
chrome.storage.sync.get(function (data) {
for (const i of data.serverList) {
var el = document.createElement('option')
el.value = i
el.innerText = i
serverList.appendChild(el)
}
2022-12-18 19:47:46 +01:00
})
}
2023-02-10 07:30:26 +01:00
// Function to save settings to storage
function saveSettings() {
// Get list of servers
var array = []
document.querySelectorAll('#mastodon-server-list option').forEach(function (el) {
array.push(el.value)
})
console.log(array)
// Save to storage
chrome.storage.sync.set({
serverList: array
}, function () {
console.log('Settings saved')
2022-12-18 19:47:46 +01:00
})
2023-02-10 07:30:26 +01:00
}
// Add server to list
document.querySelector('#server-add-btn').addEventListener('click', function () {
2023-07-11 05:39:55 +02:00
// Get hostname from input
var serverInput = document.querySelector('#mastodon-server-text').value.replace(' ', '');
var serverDomain = '';
if (serverInput.startsWith('https://')) {
var serverObj = new URL(serverInput);
serverDomain = serverObj.hostname;
} else {
serverDomain = serverInput;
2023-02-10 07:30:26 +01:00
}
2023-07-11 05:39:55 +02:00
// Add URL to list
var el = document.createElement('option')
el.value = serverDomain
el.innerText = serverDomain
serverList.appendChild(el)
serverList.value = serverDomain
document.querySelector('#mastodon-server-text').value = ''
saveSettings()
2023-02-10 07:30:26 +01:00
})
// Remove button
document.querySelector('#server-remove-btn').addEventListener('click', function () {
var selectedOption = serverList.querySelector('option[value="' + serverList.value + '"]')
serverList.removeChild(selectedOption)
saveSettings()
2022-12-18 19:47:46 +01:00
})
2022-12-18 23:22:16 +01:00
// Open keyboard shortcut
2023-02-10 07:30:26 +01:00
document.querySelector('#mastodon-keyboard-shortcut').addEventListener('click', function () {
// Firefox cannot automatically navigate to the keyboard shortcut configuration page
if (window.navigator.userAgent.includes('Firefox')) {
const bsCollapse = new bootstrap.Collapse('#firefoxShortcutCollapse').show()
} else {
chrome.tabs.create({ url: 'chrome://extensions/shortcuts#:~:text=Share%20to%20Mastodon' })
}
2023-07-11 05:39:55 +02:00
2022-12-18 23:22:16 +01:00
})
2022-12-18 19:47:46 +01:00
loadSettings()