share-to-mastodon/js/share.js

83 lines
3.6 KiB
JavaScript
Raw Normal View History

2023-02-23 23:45:09 +01:00
// Function to generate URL
function getFinalURL(domain, text, link) {
var url = ''
if ((domain === 'elk.zone') || (domain === 'main.elk.zone')) {
2023-02-23 23:45:09 +01:00
// Elk web app
url = 'https://elk.zone/intent/post?text=' + encodeURIComponent(text + '<br /><br />' + link)
} else {
// Standard Mastodon URL intent, also used by Calckey, Misskey, and other projects
// Misskey documentation: https://misskey-hub.net/en/docs/features/share-form.html
// Calckey implementation: https://codeberg.org/calckey/calckey/src/branch/main/packages/backend/src/server/web/manifest.json#:~:text=share_target
2023-02-23 23:45:09 +01:00
url = 'https://' + domain + '/share?text=' + encodeURIComponent(text + '\n\n' + link)
}
return url
}
2023-02-13 18:28:09 +01:00
// Function to initialize UI and redirects
2023-02-12 08:59:46 +01:00
async function init() {
// Generate links to options page
document.querySelectorAll('.extension-settings-link').forEach(function(el) {
el.addEventListener('click', function() {
chrome.runtime.openOptionsPage()
window.close()
})
})
2023-02-12 08:59:46 +01:00
// Get data from URL and storage
var inputParams = new URL((window.location.href)).searchParams
var shareLink = inputParams.get('link')
var shareText = inputParams.get('text')
var data = await chrome.storage.sync.get()
// Show warning if no servers are saved
if ((!data.serverList) || (data.serverList.length === 0)) {
2023-02-12 08:59:46 +01:00
document.querySelector('#server-warning').classList.remove('d-none')
return false
}
2023-02-17 23:12:59 +01:00
// If there's only one server, or if the server was picked from the context menu, redirect to that one
if (inputParams.get('server') != 'generic') {
document.querySelector('#server-loading').classList.remove('d-none')
2023-02-23 23:45:09 +01:00
window.location = getFinalURL(inputParams.get('server'), shareText, shareLink)
2023-02-17 23:12:59 +01:00
return false
} else if (data.serverList.length === 1) {
document.querySelector('#server-loading').classList.remove('d-none')
2023-02-23 23:45:09 +01:00
window.location = getFinalURL(data.serverList[0], shareText, shareLink)
return false
}
2023-02-17 23:12:59 +01:00
console.log(inputParams.get('server'))
2023-02-12 08:59:46 +01:00
// Create list of servers
var serverListEl = document.querySelector('#server-list')
for (server in data.serverList) {
// Create link list element
var serverUrl = data.serverList[server]
var linkEl = document.createElement('a')
2023-02-13 18:28:09 +01:00
linkEl.classList.add('list-group-item', 'list-group-item', 'list-group-item-action', 'fw-bold')
2023-02-12 08:59:46 +01:00
linkEl.innerText = serverUrl
2023-02-23 23:45:09 +01:00
linkEl.href = getFinalURL(serverUrl, shareText, shareLink)
2023-02-12 08:59:46 +01:00
linkEl.rel = 'preconnect'
// Add server icon to list
var serverImg = document.createElement('img')
if ((serverUrl === 'elk.zone') || (serverUrl === 'main.elk.zone')) {
2023-02-23 23:45:09 +01:00
serverImg.setAttribute('src', chrome.runtime.getURL('img/elk.png'))
} else {
serverImg.setAttribute('src', 'https://' + serverUrl + '/favicon.ico')
}
2023-02-13 18:28:09 +01:00
serverImg.setAttribute('alt', serverUrl + ' icon')
serverImg.ariaHidden = 'true'
serverImg.onerror = function() {
// Show a monochrome Mastodon icon if the server isn't responding
this.src = chrome.runtime.getURL('img/mastodon-offline.png')
}
2023-02-12 08:59:46 +01:00
linkEl.prepend(serverImg)
// Inject element
serverListEl.appendChild(linkEl)
}
// Show list
serverListEl.classList.remove('d-none')
}
// Show loading animation when a link is clicked
window.addEventListener('beforeunload', function() {
document.querySelector('#server-list').classList.add('d-none')
document.querySelector('#server-loading').classList.remove('d-none')
})
init()