misuzu/assets/redirects.js/fedi.jsx

49 lines
1.9 KiB
JavaScript

const MszRedirectsFedi = async () => {
const loading = new MszLoading({ element: '.js-loading', size: 2, inline: true });
const statusBig = document.querySelector('.js-status-big');
const statusSmall = document.querySelector('.js-status-small');
const setStatusSmall = body => {
$removeChildren(statusSmall);
$appendChild(statusSmall, body);
};
setStatusSmall(<>Resolving Webfinger resource for @{FEDI_USERNAME}@{FEDI_INSTANCE}...</>);
try {
const { body } = await $xhr.get(`${location.protocol}//${FEDI_INSTANCE}/.well-known/webfinger?format=json&resource=acct:${FEDI_USERNAME}@${FEDI_INSTANCE}`, { type: 'json' });
let url;
if(typeof body === 'object') {
if(Array.isArray(body.links))
for(const link of body.links)
if(typeof link === 'object' && link.rel === 'http://webfinger.net/rel/profile-page' && typeof link.href === 'string') {
url = link.href;
break;
}
if(typeof url !== 'string' && Array.isArray(body.aliases))
for(const alias of body.aliases)
if(typeof alias === 'string') {
url = alias;
break;
}
}
if(typeof url !== 'string' || (!url.startsWith('https://') && !url.startsWith('http://')))
throw new Error('Unable to resolve profile URL for given handle.');
setStatusSmall(<>Redirecting to <a href={url} rel="noopener">{url}</a>...</>);
location.replace(url);
} catch(ex) {
await loading.icon.stop();
loading.icon.batsu();
let errorText = ex.toString();
if(errorText.startsWith('['))
errorText = 'Something went wrong.';
statusBig.textContent = 'Profile not found!';
setStatusSmall(<span style="color: red">{errorText}</span>);
}
};