misuzu/assets/redirects.js/bsky.jsx

33 lines
1.2 KiB
React
Raw Normal View History

const MszRedirectsBsky = 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 ATProto DID for @{BSKY_HANDLE}...</>);
try {
const { body } = await $xhr.get(`${location.protocol}//${BSKY_HANDLE}/.well-known/atproto-did`);
if(typeof body !== 'string' || !body.startsWith('did:'))
throw new Error('Was unable to resolve the given handle.');
const url = BSKY_FORMAT.replace('%s', body);
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>);
}
};