Last active
April 6, 2022 07:35
-
-
Save simevidas/2ff2bea9fab4e0d7b382 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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}`); | |
}()); |
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
This code snippet will sort the tweets in your timeline by the number of favorites/retweets.
How to use:
time_top
andtime_bottom
. By default, only tweets between 12h ago and 24h ago are sorted; the rest is removed.)