deathau.weblog.lol/weblog/B0. Template/fediverse.js.md
Gordon Pedersen d929f8935b
All checks were successful
/ weblog.lol (push) Successful in 12s
Add some very basic fediverse integration
It doesn't go as far as I wanted, because I don't
know how to get the counts I need as yet.
2024-05-11 12:43:50 +10:00

3.2 KiB

/*/ Type: file Content-Type: application/javascript Title: Fediverse script Location: /fediverse.js /**/

const SUBSCRIBE_LINK_REL = 'http://ostatus.org/schema/1.0/subscribe' async function getSubscribeTemplate() { const input = prompt("Please enter your fediverse / mastodon handle (e.g. '@user@domain.social')", "@") let handle = input.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}`
return fetch(url, {headers: {
  'Content-Type': 'application/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
  return template
})
.catch(e => {
  console.error(e)
  alert(`Sorry, we couldn't find an uri for ${input}.\n\nTry searching for "${uri}" on ${domain} (or in your fediverse client of choice)`)
  return null
})

} else { alert('Please enter your fediverse address in @user@domain.social format') return null } return null }

function linkToUrl(uri) { // window.open(uri, '_blank').focus() const link = document.createElement('a') link.href = uri link.target = "_blank" link.click() }

function noDataCick(href) { return async (e) => { e.preventDefault() let subscribe_template = await getSubscribeTemplate() if(subscribe_template) { localStorage.setItem('subscribe_template', subscribe_template) const url = subscribe_template.replace("{uri}", href) linkToUrl(url) addSocialButtons(this, url) } else { linkToUrl(href) } } }

function addSocialButtons(el, href) { // TODO: fetch the post from the fediverse to add in comment, like and retweet count // the following fetches the activitypub object but has no reference to the likes, etc // if I had the local id, I could use the mastodon API to get it, but friendica uses the diaspora:guid in the link // const url = new URL(href).searchParams.get("uri") // fetch(url, { // headers: { // 'Accept': 'application/json' // } // }).then(async result => { // const json = await result.text() // console.log(json) // }) // .catch(e => { // console.error(e) // })

const innerHTML = <a href='${href}' target="_blank"><i class="fa fa-comment"></i></a> <a href='${href}' target="_blank"><i class="fa fa-star"></i></a> <a href='${href}' target="_blank"><i class="fa fa-retweet"></i></a>

const span = document.createElement('span') span.classList.add('fediverse') span.innerHTML = innerHTML

el.insertAdjacentElement('afterend', span) el.remove() }

function fediverse() { let subscribe_template = localStorage.getItem('subscribe_template');

document.querySelectorAll("a.external_url:not([href='{external_url}'])").forEach(el => { if(subscribe_template) { console.log(subscribe_template.replace("{uri}", el.href)) addSocialButtons(el, subscribe_template.replace("{uri}", el.href)) } else { el.addEventListener('click', noDataCick(el.href)) } }) }