diff --git a/configuration/template.html b/configuration/template.html index c44145f..4f57304 100644 --- a/configuration/template.html +++ b/configuration/template.html @@ -29,6 +29,10 @@ via status.lol Interact on the Fediverse + diff --git a/fediverse.js b/fediverse.js new file mode 120000 index 0000000..a2d5558 --- /dev/null +++ b/fediverse.js @@ -0,0 +1 @@ +./weblog/B0. Template/fediverse.js.md \ No newline at end of file diff --git a/weblog/B0. Template/fediverse.js.md b/weblog/B0. Template/fediverse.js.md index fce0cb7..11200f7 100644 --- a/weblog/B0. Template/fediverse.js.md +++ b/weblog/B0. Template/fediverse.js.md @@ -6,46 +6,102 @@ Location: /fediverse.js /**/ const SUBSCRIBE_LINK_REL = 'http://ostatus.org/schema/1.0/subscribe' -function share(uri, handle) { - if(!handle){ - handle = prompt("Please enter your fediverse / mastodon handle (e.g. '@user@domain.social')\n(remember to look for blocked pop-ups)", "@") +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 + }) } - - 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 { + 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 { - throw 'Please enter your fediverse address in @user@domain.social format' + 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 = ` + + + + ` + + const span = document.createElement('span') + span.classList.add('fediverse') + span.innerHTML = innerHTML + + el.insertAdjacentElement('afterend', span) + el.remove() +} + function fediverse() { - document.querySelectorAll("a.external_url:not([href='{external_url}'])").forEach(el => { - el.addEventListener('click', e => { - e.preventDefault() - share(el.href) - }) - }) -} \ No newline at end of file + 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)) + } + }) +} diff --git a/weblog/B0. Template/styles.css.md b/weblog/B0. Template/styles.css.md index 1ded9d0..c9f8665 100644 --- a/weblog/B0. Template/styles.css.md +++ b/weblog/B0. Template/styles.css.md @@ -93,7 +93,7 @@ main.page { margin-left: .75em; } -.post-info>a { +.post-info a { margin-left: 1em; background: none; }