Skip to content

Instantly share code, notes, and snippets.

@simevidas
Last active April 6, 2022 07:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simevidas/2ff2bea9fab4e0d7b382 to your computer and use it in GitHub Desktop.
Save simevidas/2ff2bea9fab4e0d7b382 to your computer and use it in GitHub Desktop.
(function(){
'use strict';
// Twitter overwrites console.log; this restores it
console.log = Object.getPrototypeOf(console).log;
let $container = $('.stream-items');
let $items = $container.children('.stream-item');
console.log(`Number of items at the beginning: ${$items.length}`);
let time_top = Date.now() - 1000*60*60* 12; // hours ago
let time_bottom = Date.now() - 1000*60*60* 24; // hours ago
$container.css({ display: 'flex', flexDirection: 'column-reverse' });
$items.each((i, item) => {
let $item = $(item);
let $tweets = $item.find('.tweet');
let all_nums = [];
let all_times = [];
// filter out promoted tweets and remove them from the DOM
if ($tweets.filter('[data-disclosure-type="promoted"]').length > 0) {
$item.remove();
return true; // skip to next iteration
}
$tweets.each((i, tweet) => {
let $tweet = $(tweet);
// get the number or favorites and retweets for this tweet
let tweet_nums = $tweet
.find('.ProfileTweet-actionList')
.children('.ProfileTweet-action--retweet, .ProfileTweet-action--favorite')
.find('.ProfileTweet-actionCount:visible')
.map((i, elem) => Number(elem.textContent)).toArray();
// all numbers of all tweets under one $item are pushed onto the same array
all_nums = all_nums.concat(tweet_nums);
all_times.push(+$tweet.find('._timestamp').attr('data-time-ms'));
});
// 1. if at least one of the $item’s times is within the [time_bottom, time_to] range
for (let time of all_times) {
if (time < time_top && time > time_bottom) {
// 1.1 set the Flexbox order property on the $item to the highest number value
$item.css({ order: Math.max.apply(Math, all_nums) });
return true; // 1.2 and skip to next iteration
}
}
// 2. otherwise, remove this $item
$item.remove();
});
console.log(`Number of items at the end: ${$container.children('.stream-item').length}`);
}());
@simevidas
Copy link
Author

This code snippet will sort the tweets in your timeline by the number of favorites/retweets.

How to use:

  1. Open your Twitter timeline (twitter.com)
  2. Hold down the END key until you’ve loaded the last 24 hours of tweets into the timeline. (Depending on how many users you follow, this could be huge number of tweets. In that case, readjust time_top and time_bottom. By default, only tweets between 12h ago and 24h ago are sorted; the rest is removed.)
  3. Run the above code in the browser’s console. WARNING: On my machine, it takes ~60 seconds to execute in Chrome. (Do not touch anything until you see the “Number of items at the end” message.) Perform this at your own risk. I’m not responsible if your processor overheats and blows up.

@sarukuku
Copy link

You should turn this in to a Chrome Extension :)

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