Compose Tweet about a website with Shortcuts

The following Javascript snippet composes a Tweet.

  • It fetches the title <meta name="description" of a page.
  • It does some replacements to mention some twitter profiles.
  • It uses the <meta name="keywords" as a list of hashtags.
  • It appends the url of the website to the tweet.
function getMeta(metaName) {
  var metas = document.getElementsByTagName('meta');
  for (let i = 0; i < metas.length; i++) {
    if (metas[i].getAttribute('name') === metaName) {
      return metas[i].getAttribute('content');
    }
  }
  return '';
}
var tweet = document.title + '. ' + getMeta('description');
const replacements = new Map();
replacements.set('Código Bot', '@codigobotfm');
replacements.set('Micronaut', '@micronautfw');
replacements.set('Grails', '@grailsfw');
for (const [key, value] of replacements) {
	tweet = tweet.replace(key, value)
}
tweet += ' #' + getMeta('keywords').replace(',', ' #');
tweet += window.location.href;

Next, create a Shortcut.

A shortcut is a quick way to get one or more tasks done with your apps. The Shortcuts app lets you create your own shortcuts with multiple steps.

It is a simple three step shortcut:

  • It accepts Safari web pages
  • Run a Javascript snippet. Copy the above snippet. Add a last line of code completion(tweet);
  • Open Tweetbot to specified account.

Shortcuts - Accept Safari Web Page - Run Javascript - Tweet

Tags: #automation #shortcuts #javascript #tweetbot #safari