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
- 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 withid="save-post"instead. - Do not touch UI elements that do not exist any more. In my case, I first went the
ifand iteration route, then in the end switched to usingquerySelectorAll()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
Share this:
- Share on Mastodon (Opens in new window) Mastodon
- Share on Bluesky (Opens in new window) Bluesky
- Share on Tumblr
- Share on Reddit (Opens in new window) Reddit
- Share on Threads (Opens in new window) Threads
- Tweet
- Share on Telegram (Opens in new window) Telegram
- Share on Nextdoor (Opens in new window) Nextdoor
- Share on WhatsApp (Opens in new window) WhatsApp
- Print (Opens in new window) Print
- Email a link to a friend (Opens in new window) Email
Related
This entry was posted on 2026/09/22 at 12:00 and is filed under Bookmarklet, CSS, Development, HTML, JavaScript/ECMAScript, Mastodon, Scripting, SocialMedia, Software Development, Web Browsers, Web Development, WordPress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.






Leave a Comment