Skip to content

Instantly share code, notes, and snippets.

@AmeliaBR
Last active November 16, 2022 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmeliaBR/8892e02064432423452568f432defa6d to your computer and use it in GitHub Desktop.
Save AmeliaBR/8892e02064432423452568f432defa6d to your computer and use it in GitHub Desktop.
Tweet This Toot Bookmarklet

A browser bookmarklet to manually transfer Mastodon posts to Twitter. Also a bookmarklet to tweet out any web page with its title and URL, since I made that in the process.

To Use

  • Create a new browser bookmark, and copy the contents of the tweet-this-toot.url file as the bookmark URL.

  • Open any Mastodon post in its own web page on its own host server (e.g., by clicking on the date when viewing it in your feed, or by copying the sharing URL). If you're viewing it on a different server's web app, you may need to use the "..." menu to "Open original page".

  • Click or otherwise activate the browser bookmark while you're in the browser tab displaying the Mastodon post.

    NOTE: If this does not work (e.g., MS Edge does not activate Favourites in the current web page's context), you can instead copy the javascript:... URL to the URL bar and activate it that way. But at that point, it might be easier to just manually copy the post to Twitter.

  • In the new Twitter composer window that opens up, edit the suggested tweet text as necessary, then tweet.

Note: cannot be used to cross-post images or media, or to copy threads. However, there will be a link back to the Mastodon post for the rest.

Security and Compatibility

Uses the Twitter web intents URL scheme, and the Mastodon public API.

No special permissions required at either end. It can only access public (global or unlisted) Mastodon posts. You don't need to be logged in to Mastodon; you do need to log in to Twitter.

Works in the default Mastodon web apps; not sure about other fediverse apps. Will eventually break if Mastodon stops supporting the api/v1/ URLs.

/* readable version of the bookmarklet code, for easier editing */
(async function() {
let apiURL=new URL(document.location);
apiURL.pathname=apiURL.pathname.replace(/\/.+\//, "/api/v1/statuses/");
let params;
await fetch(apiURL)
.then((r)=>r.json())
.then((json)=>{
let d=document.createElement("div");
d.innerHTML=json.content.replaceAll("</p><p>", "</p>\n\n<p>").replaceAll("<br />", "<br>\n");
let cw = (json.spoiler_text)? `CW: ${json.spoiler_text}\n\n` : "";
params={
text:`${cw}From ${json.account.url}:\n\n${d.innerText}${(json.media_attachments.length?"\n\nwith media…":"")}`,
url:json.url
}
})
.catch(()=>{
params={text:document.title,url:document.location}
});
const twit = new URL("https://twitter.com/intent/tweet");
twit.search = new URLSearchParams(params);
window.open(twit,"tweet",{noreferrer:1,noopener:1});
})()
javascript:(async function() { let apiURL=new URL(document.location); apiURL.pathname=apiURL.pathname.replace(/\/.+\//, "/api/v1/statuses/"); let params; await fetch(apiURL).then((r)=>r.json()).then((json)=>{let d=document.createElement("div");d.innerHTML=json.content.replaceAll("</p><p>", "</p>\n\n<p>").replaceAll("<br />", "<br>\n"); let cw = (json.spoiler_text)? `CW: ${json.spoiler_text}\n\n` : ""; params={text:`${cw}From ${json.account.url}:\n\n${d.innerText}${(json.media_attachments.length?"\n\nwith media%E2%80%A6":"")}`, url:json.url}}).catch(()=>{params={text:document.title,url:document.location}}); const twit=new URL("https://twitter.com/intent/tweet"); twit.search = new URLSearchParams(params); window.open(twit,"test",{noreferrer:1,noopener:1}); })()
javascript:(()=>{const url= new URL("https://twitter.com/intent/tweet"); url.search = new URLSearchParams({text:document.title,url:document.location}); window.open(url,"test",{noreferrer:1,noopener:1}); })();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment