deathau.weblog.lol/weblog/B0. Template/fediverse.js.md

51 lines
1.6 KiB
Markdown
Raw Normal View History

2024-05-10 07:08:16 +00:00
/*/
Type: file
Content-Type: application/javascript
2024-05-10 23:01:03 +00:00
Title: Fediverse script
Location: /fediverse.js
2024-05-10 07:08:16 +00:00
/**/
const SUBSCRIBE_LINK_REL = 'http://ostatus.org/schema/1.0/subscribe'
function share(uri, handle) {
if(!handle){
2024-05-10 07:13:53 +00:00
handle = prompt("Please enter your fediverse / mastodon handle (e.g. '@user@domain.social')\n(remember to look for blocked pop-ups)", "@")
2024-05-10 07:08:16 +00:00
}
if(handle) {
const input = handle
handle = handle.trim().replace(/^@/,'')
const split = handle.split('@')
if(split.length == 2) {
const resource = `acct:${handle}`
const domain = split[1]
// look up remote user via webfinger
const url = `https://${domain}/.well-known/webfinger?resource=${resource}`
fetch(url, {headers: {
'Content-Type': 'application/activity+json'
}}).then(async result => {
const json = await result.json()
const subscribe = json.links.find(link => link.rel && link.rel == SUBSCRIBE_LINK_REL)
let template = subscribe.template
window.open(template.replace("{uri}", uri), '_blank').focus()
})
.catch(e => {
console.error(e)
throw `Sorry, we couldn't find an uri for ${input}.\n\nTry searching for "${uri}" on ${domain} (or in your fediverse client of choice)`
})
}
else {
throw 'Please enter your fediverse address in @user@domain.social format'
}
}
2024-05-10 23:01:03 +00:00
}
function fediverse() {
document.querySelectorAll("a.external_url:not([href='{external_url}'])").forEach(el => {
el.addEventListener('click', e => {
e.preventDefault()
share(el.href)
})
})
2024-05-10 07:08:16 +00:00
}