Skip to content

Instantly share code, notes, and snippets.

@samir-dahal
Last active June 14, 2025 12:18
Show Gist options
  • Save samir-dahal/58e015ee91691416d4778dffebc13330 to your computer and use it in GitHub Desktop.
Save samir-dahal/58e015ee91691416d4778dffebc13330 to your computer and use it in GitHub Desktop.
Set the Twitter detail page to automatically default to the "Most liked" filter.
//UPDATE: Pls use this new script as of June 2025
//https://gist.github.com/KyleF0X/f191fd223586973f3bf67cce394a98f6
(function () {
const statusMatch = /https:\/\/x\.com\/[^\/]+\/status\/\d+/;
const MOST_RELEVANT = "most relevant";
const MOST_LIKED = "most liked";
let toNavigateUrl = null;
function findSpanByText(text) {
var aTags = document.getElementsByTagName("span");
for (var i = 0; i < aTags.length; i++) {
if (aTags[i].textContent.toLowerCase() == text) {
return aTags[i];
}
}
return null;
}
function performAction(elm) {
const sortReply = findSpanByText(MOST_RELEVANT);
if (sortReply) {
console.log("Found sort reply button");
sortReply.click(); //show sorting hovercard
setTimeout(() => {
const mostLiked = findSpanByText(MOST_LIKED);
if (mostLiked) {
console.log('found most liked');
}
mostLiked?.click(); //sort by most liked
}, 500)
}
}
function runWhenReady(readySelector, callback) {
var numAttempts = 0;
var tryNow = function () {
var elem = document.querySelector(readySelector);
if (elem && window.location.href === toNavigateUrl) {
console.log('Found: ' + elem);
callback(elem);
} else {
numAttempts++;
if (numAttempts >= 34) {
console.warn('Giving up after 34 attempts. Could not find: ' + readySelector);
} else {
setTimeout(tryNow, 250 * Math.pow(1.1, numAttempts));
}
}
};
tryNow();
}
window.navigation.addEventListener("navigate", (event) => {
if (!statusMatch.test(event.destination.url)) return;
toNavigateUrl = event.destination.url;
runWhenReady("article[data-testid='tweet']", performAction);
});
})()
@KyleF0X
Copy link

KyleF0X commented Jun 14, 2025

βœ… Fixed Script Available!

Hi @samir-dahal, thanks for the original script! It stopped working due to Twitter UI changes, so I've created an updated version that works with the current Twitter/X interface.

What changed in Twitter's UI:

  1. Sort button is now icon-only (no text) - it's a sliders icon
  2. Text changed from "Most Relevant"/"Most Liked" to "Relevancy"/"Latest"/"Likes"
  3. Button structure is more complex with nested SVG elements

Key fixes in the updated script:

  • Detects sort button by exact SVG path instead of text
  • Looks for "Likes" instead of "Most Liked"
  • Properly navigates DOM tree to find clickable elements
  • Uses MutationObserver for better browser compatibility

πŸ”— Fixed version available here:

https://gist.github.com/KyleF0X/f191fd223586973f3bf67cce394a98f6

The updated script is tested and working as of June 2025. It automatically sorts replies by "Likes" on every tweet you view.

Hope this helps anyone else who was looking for a fix! πŸš€

@samir-dahal
Copy link
Author

samir-dahal commented Jun 14, 2025

Hi @KyleF0X, thanks for the update.
I've added a comment on top of my script file, so people will navigate to your gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment