misuzu/assets/redir-fedi.js/main.js

40 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2025-01-29 21:07:12 +00:00
(async () => {
const status = document.querySelector('.js-status');
status.textContent = 'Looking up Fediverse profile...';
let url = null;
try {
const response = await fetch(`${location.protocol}//${FEDI_INSTANCE}/.well-known/webfinger?format=json&resource=acct:${FEDI_USERNAME}@${FEDI_INSTANCE}`);
const webfinger = await response.json();
if(typeof webfinger === 'object') {
if(Array.isArray(webfinger.links))
for(const link of webfinger.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(webfinger.aliases))
for(const alias of webfinger.aliases)
if(typeof alias === 'string') {
url = alias;
break;
}
}
} catch(ex) {
status.style.color = 'red';
status.textContent = `Could not complete Webfinger lookup! ${ex}`;
return;
}
if(typeof url !== 'string' || (!url.startsWith('https://') && !url.startsWith('http://'))) {
status.style.color = 'red';
status.textContent = 'Could not find an acceptable profile URL.';
return;
}
status.textContent = `Redirecting to ${url}...`;
location.replace(url);
})();