Last active
July 12, 2017 17:42
-
-
Save simevidas/a88463cb508e93bb37d7 to your computer and use it in GitHub Desktop.
A JS snippet for highlighting tweets with lots of RTs or hearts
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'; | |
let cap = 100; // 100+ RTs or hearts produces max yellow bg color | |
$('.tweet', '.stream-items').each((i, tweet) => { | |
let all_nums = $(tweet) | |
.find('.ProfileTweet-actionList .ProfileTweet-actionCount:visible') | |
.map((j, elem) => Number(elem.textContent)).toArray(); | |
let num = Math.min(Math.max.apply(Math, all_nums.concat(0)), cap); | |
let str = Math.floor(255 * (1 - num/cap)).toString(16); | |
if (str.length === 1) str = '0' + str; | |
$(tweet).css({ 'background-color': '#FFFF' + str }); | |
}); | |
}()); |
And you only need one find()
: $(tweet).find('.ProfileTweet-actionList .ProfileTweet-actionCount:visible')
Caching $(foo)
is a micro-optimization. It only takes about 5 microseconds, so you can have dozens of them in a code block without affecting performance.
I’ve switched to one .find()
; seems to work fine.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cache the
$(tweet)
you're calling it twice in line #7 and #17