Bookmarklet for Mastodon


I was a bit annoyed at using Mastodon on the web while logged out.


Specifically, if you visit a user's web page, you naturally start at the top of the page. But, the most recent post will be below their profile information and below any pinned posts. So, you have to scroll before any new information shows.


I tried looking for an HTML anchor that I could save to a bookmark. But, I did not find one.


Instead, I asked a large language model to write some JavaScript that would scroll to the first post. And, I put that into a bookmarklet.



Bookmarklet Code


If you save the following JavaScript code to the 'Address' field of a web browser bookmark, then clicking on that bookmark will scroll to the first new Mastodon post.


I know this works in Firefox and Vivaldi. It probably works in Chrome. It might not work in Safari.


javascript:(function(){
  const feedContainer = document.querySelector('div[role="feed"].item-list');
  if (feedContainer) {
    const firstArticle = feedContainer.querySelector('article');
    if (firstArticle) {
      firstArticle.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }
  }
})();


If you're confused, here are all the steps:



Clicking on the bookmark should scroll the newest Mastodon post into view.



Background


Mastodon HTML Code


A basic Mastodon user profile has all the posts organized in a HTML <div/> element like this:


<div role="feed" class="item-list">
	<article>   </article>
	<article>   </article>
	<article>   </article>
</div>


So, we'll just use some basic JavaScript to select the correct HTML element and scroll it into view.


Use the querySelector() method to select the first element that matches a CSS selector.


The JavaScript generated by Gemini uses querySelector() twice:


// Select the <div/> element. 
document.querySelector('div[role="feed"].item-list');

// And, select, the first <article/> element. 
feedContainer.querySelector('article');


Use the scrollIntoView() method to scroll that post into view.


firstArticle.scrollIntoView();


The language model I chatted with used constants and some method options.


You could also use this more basic version without the constants and options. This works because you can chain methods off of any object including the built-in "document" object.


javascript:(function(){
  document.querySelector('div[role="feed"].item-list').querySelector('article').scrollIntoView();
})();



Bookmarklets


If you're not familiar with bookmarklets, basically, you can save some JavaScript in a browser bookmark. And then you can execute that code when you click on the bookmark.


The "JavaScript:" URI scheme is what allows the browser to interpret the code saved in the bookmark's address field.


When I work on a new bookmarklet, I get the code working in a web browser's JavaScript console. Then, I place that code into a template. Be careful to remove all comments from the code.


javascript:(function(){
  // New bookmarklet code goes here. 
  // No JavaScript comments (like this one) should be saved to your bookmark's Address field.
})();



Google Gemini 2.5 Custom (AI Mode)


Here is the chat I had with Google's Gemini large language model. I initially searched for help and then clicked on the 'AI Mode' tab currently available on Google's search results page.



Question One


Input


Is there a HTML anchor for the first post under the pinned posts on a Mastodon web page?



Output


Mastodon's web interface doesn't typically provide a dedicated HTML anchor directly for the first non-pinned post on a timeline or user profile page.


How Mastodon handles pinned posts:



Workarounds:



Important Notes:



In summary, while there isn't a pre-built HTML anchor for the first post after the pinned posts, you can achieve similar functionality using custom embeds, CSS selectors, or by interacting with the Mastodon API.



Question Two


Input


Is there a JavaScript method that will scroll a part of the DOM into view?



Output


Yes, there are several JavaScript methods to scroll a part of the DOM into view. The most common and recommended method is element.scrollIntoView().


element.scrollIntoView()



scrollIntoViewOptions Object


This object allows you to control the scrolling behavior with properties like:



Example:


const element = document.getElementById('myElement'); // Replace 'myElement' with the ID of your element
element.scrollIntoView({ behavior: 'smooth', block: 'center' });


Use code with caution.


This code snippet will smoothly scroll the element with the ID "myElement" to the center of the viewport.


Other methods:



Note: While window.scrollTo() and window.scrollBy() can be used to scroll to an element, scrollIntoView() is generally preferred as it is specifically designed for scrolling an element into view, and it handles the ancestor containers automatically.



Question Three


Input


Please show me a JavaScript snippet that would scroll the first non-pinned post on Mastodon web page into view.



Output


Disclaimer: Directly manipulating the HTML structure of a website you don't own (like the Mastodon web interface) can be unreliable due to potential changes in the website's design. This script relies on the current HTML structure of Mastodon, which may change in future updates. However, this JavaScript snippet demonstrates a general approach to finding and scrolling an element based on its position after certain elements (like pinned posts).


// Find the pinned posts container (assuming it exists and has a specific class)
// Replace '.timeline-pinned-posts' with the actual class name
const pinnedPostsContainer = document.querySelector('.timeline-pinned-posts'); 

if (pinnedPostsContainer) {
  // Find the parent element that contains both pinned and non-pinned posts
  const timelineContainer = pinnedPostsContainer.parentElement;

  if (timelineContainer) {
    // Get all the posts within the timeline container
    // Replace '.status' with the class name for individual posts
    const allPosts = timelineContainer.querySelectorAll('.status'); 

    // Find the first non-pinned post
    let firstNonPinnedPost = null;
    let foundPinned = false;
    for (const post of allPosts) {
      if (post.closest('.timeline-pinned-posts')) {
        // This is a pinned post, continue searching
        foundPinned = true;
      } else if (foundPinned) {
        // This is the first post after the pinned posts
        firstNonPinnedPost = post;
        break; // Stop searching once the first non-pinned post is found
      }
    }

    // Scroll the first non-pinned post into view
    if (firstNonPinnedPost) {
      firstNonPinnedPost.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }
  }
}


Use code with caution.


The following explains the JavaScript code:



To use the code:



Important points:




Question Four


Input


Mastodon uses a <div/> with attributes like role="feed" and class="item-list" And, that <div/> contains <article/> elements. Please show me JavaScript to scroll the first <article/> tag into view.



Output


Yes, using the scrollIntoView() method is the right approach here.


Assuming you're talking about the Mastodon web interface's main timeline feed, where posts are presented as <article> elements within a container div having role="feed" and class="item-list", here's how you can scroll the first <article> tag within that container into view:


// Find the feed container div
const feedContainer = document.querySelector('div[role="feed"].item-list');

if (feedContainer) {
  // Find the first article tag within the feed container
  const firstArticle = feedContainer.querySelector('article');

  if (firstArticle) {
    // Scroll the first article into view
    firstArticle.scrollIntoView({ behavior: 'smooth', block: 'start' });
  }
}


Use code with caution.


The following explains the code:





How to use:



Important Note:


This script assumes the Mastodon web interface's timeline's specific HTML structure (div with role="feed" and class="item-list" containing <article> tags). If Mastodon's HTML structure changes, this script may need adjustments.



Question Five


Input


I have a basic question. Why should I use `const` for the initial variable?



Output


In JavaScript, you should generally use const for variables whose values are not expected to change, and let for variables whose values might be reassigned later in the code. var should be avoided in modern JavaScript development due to its scope issues and potential for unexpected behavior.


Here's why const is the preferred initial variable declaration in many cases:


Readability and Intent:



Prevents Accidental Reassignment:



Optimizations:



Block Scoping:



In summary, using const by default offers several benefits for writing clean, readable, and maintainable JavaScript code:



Remember: const prevents the reassignment of a variable's binding, but it does not make the value itself immutable. If the variable holds an object or array, its properties or elements can still be modified. If you want to ensure the contents of an object are immutable, you can use Object.freeze().



References


querySelector() Method | W3Schools

scrollIntoView() method | MDN

CSS Selectors Reference | W3Schools

CSS Selectors Tutorial | W3Schools

Bookmarklet | Wikipedia

URI Schemes | Wikipedia



Created: Monday, June 16, 2025

Updated: Monday, June 16, 2025



/gemlog/