Skip to content

Instantly share code, notes, and snippets.

@gyng
Created December 17, 2025 09:52
Show Gist options
  • Select an option

  • Save gyng/0f926b6d7a5b7b4d9a45bb1e761fde84 to your computer and use it in GitHub Desktop.

Select an option

Save gyng/0f926b6d7a5b7b4d9a45bb1e761fde84 to your computer and use it in GitHub Desktop.
Auto switch to "Following" tab on X/Twitter using MutationObserver for GreaseMonkey. Generated code.
// ==UserScript==
// @name Twitter(X): Select the Following tab on load (using MutationObserver)
// @match https://x.com/*
// @match https://twitter.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
let hasAutoClicked = false;
let lastPathname = window.location.pathname;
const selectFollowingTab = () => {
// 1. Check if we are on Home
if (window.location.pathname !== '/home') {
hasAutoClicked = false; // Reset if we leave home
return;
}
// 2. If we already auto-clicked during this specific visit to /home, stop.
if (hasAutoClicked) return;
const tabs = document.querySelectorAll('div[role="tab"]');
if (tabs.length >= 2) {
const followingTab = tabs[1];
const isSelected = followingTab.getAttribute('aria-selected') === 'true';
if (!isSelected) {
followingTab.click();
hasAutoClicked = true; // Mark as done so user can manual click "For You"
} else {
// If it was already "Following" by default, mark as done anyway
hasAutoClicked = true;
}
}
};
// Watch for both DOM changes (loading) and URL changes (navigation)
const observer = new MutationObserver(() => {
// If the URL changed (e.g., user clicked from Profile to Home)
if (window.location.pathname !== lastPathname) {
lastPathname = window.location.pathname;
hasAutoClicked = false; // Reset the lock for the new page
}
selectFollowingTab();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Initial run
selectFollowingTab();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment