The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 717 other subscribers

Improving the JavaScript Bookmarklet to enable Mastodon publishing for a WordPress.com post that is edited in the Classic Editor

Posted by jpluimers on 2026/09/22

A while back (actually: a long time ago), in JavaScript Bookmarklet to enable Mastodon publishing for a WordPress.com post that is edited in the Classic Editor I promised to improve that code.

Actually I did improve the gist, but I forgot to write the blog post on it.

So below is the old code, new code, and explanation of the improvements.

Old code

This was the first Bookmarklet iteration:

javascript:(function(){
  publicizeFormEditHref = document.getElementById('publicize-form-edit');
  publicizeFormEditHref.click();
  mastodonCheckboxes = document.getElementsByClassName('wpas-submit-mastodon');
  mastodonCheckboxes[0].checked = true;
  publicizeFormHideHref = document.getElementById('publicize-form-hide');
  publicizeFormHideHref.click();
  updateButtonHref = document.getElementById('publish');
  updateButtonHref.click();
})();

The drawbacks were many:

  • no checks on UI elements to exist
  • no checks on the post state (draf, published, scheduled).

New code

With checks and balances, it became this:

javascript:(function(){
  publicizeFormEditHref = document.getElementById('publicize-form-edit'); /* "edit" href */
  if(publicizeFormEditHref) {
    publicizeFormEditHref.click();
  }
  mastodonCheckboxes = document.getElementsByClassName('wpas-submit-mastodon'); /* any Mastodon checkboxes (one can have zero or more accounts configured) */
  Array.from(mastodonCheckboxes).forEach((mastodonCheckbox) => {
    mastodonCheckbox.checked = true;
  });
  publicizeFormHideHref = document.getElementById('publicize-form-hide'); /* "OK" button disguised as href */
  if (publicizeFormHideHref) {
    publicizeFormHideHref.click();
  }
  savePostButton = document.getElementById('save-post'); /* button exists when post has status "draft" */
  if (savePostButton) {
    savePostButton.click();
  }
  else {
    updateButton = document.getElementById('publish'); /* button exists when post has status "draft", "scheduled", or "published"; when "draft" a click() will either "schedule" or "publish" depending on the schedule timestamp being in the future or past */
    if (updateButton) {
      updateButton.click();
    }
  }
})();

Note the comments are all within /* / */ pairs as single line // comments do not work in Bookmarklets (they fail with “unexpected end of input”, see [Wayback/Archive] javascript – “Uncaught SyntaxError: Unexpected end of input” When converting to bookmarklet – Stack Overflow – thanks [Wayback/Archive] User Cody Smith – Stack Overflow).

Note I didn’t use [Wayback/Archive] Bookmarklet Creator with Script Includer – Peter Coles, I just added the Bookmarklet using the built-in Chrome bookmark editor. Apparently that one too makes one long line of code despite showing line break characters.

Oh well: learning things the hard way is still learning (:

Newest code

Explanation

  1. If there is a button with HTML code <input type="submit" name="save" id="save-post" value="Save Draft" class="button">, then the post is in DRAFT state, so do not click the button with HTML code <input type="submit" name="publish" id="publish" class="button button-primary button-large" value="Publish"> as it will inadvertently publish the post. Click the “Save Draft” button with id="save-post" instead.
  2. Do not touch UI elements that do not exist any more. In my case, I first went the if and iteration route, then in the end switched to using querySelectorAll() for all parts that are retrieving UI elements. Some find the last change more consistent, others like to keep it at the first stage.

–jeroen

Leave a Comment