Share this post on fediverse

Sometimes when I find something interesting on the web, I share it with people on fediverse. To this day lots of blogs and news sites use little buttons to let readers share their articles on various social media and in email. So I thought I could add "share on fediverse" to my blag posts.

Fedishare button v0.1

This is just a testing version of the button. (Maybe I should have labeled this version v0.1b) In the future I would like to make the JS script a bit smarter and check whether people's servers' software is actually supported or not. For now the script just hopes your server's software uses Mastodon API (which many non-Mastodon servers support).

I will slowly start adding the button to my previous posts. It may take a while though, depending on how many spoons I got.

About the script

Don't worry though, the JS script doesn't call home, doesn't collect any data about you, nor spies on you in any other way. All you have to do is put it the address for your fedi server and click a button. The script then opens a new window for you to share it on your server. And if you don't support JS (for whatever reason), then you don't have to worry about anything and can share the link manually.

Code

The button and field to fill in to share the article (working version can be found below):


<div id="sharePost">
<span>Share on fediverse:</span> <br>
<input type="text" id="domain" value="" placeholder="Your server address">
<button onclick="fediShare();">Share</button>
</div>

The JS script:


function fediShare(){
	// get value of domain from the input field
    domain = document.getElementById("domain").value;

	// if empty, then don't do anything
    if (domain == "" || domain == null){
		return;
    }
	// building the url
    url = `https://${domain}/share?text=+${encodeURIComponent(document.title)} ${encodeURIComponent(location.href)}`;

	// open in a new window
    window.open(url, '_blank');
}

domain refers to the input field next to the share button. This is where the reader writes their fedi server address (for example alt.lawndale.space).

document.title refers to the title of the page, in this case Share this post on fediverse - doughnut

location.href refers to the URL of the page, in this case https://doughnut.neocities.org/blag/18.html

That's it, folks!

As I said, it's a very simple script for now. It all stands and falls on whether the reader's server understands /share?text=. I've checked a few servers (different fedi software) and it's worked so far on all of them. However, it would be nice to eventually add software look up and change the script behaviour based on the server software. One thing that I'm not sure about is saving people's server for future use. I could save their domain input in a cookie (locally) so it would be default next time, but I'm not sure how I feel about using cookies for this. It shouldn't be too much work to fill it out[citation needed] (and I don't expect people to start sharing all my posts) and this way I don't have to deal with asking for cookie permissions.

But if you're interested in this script for its simplicity, feel free to use it on your own website. All I'd like to ask is that you would mention I'm the author and link back to this blag post.

If you have any questions or opinions on this, please let me know on fedi @bugbear@alt.lawndale.space.


see you, space cowboy

Share on fediverse: