Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Twitter - Fix tweet count rounding
- // @namespace localhost
- // @description Because 10050 tweets != 10.1K
- // @include https://twitter.com/*
- // @version 1.0
- // @grant none
- // ==/UserScript==
- // There are two pages that show a user's tweet count, and they have different
- // CSS selectors. So I jammed both of them together.
- const realTweetCountSelector = 'a.DashboardProfileCard-statLink, a.ProfileNav-stat';
- const fakeTweetCountSelector = 'span.DashboardProfileCard-statValue, span.ProfileNav-value';
- var realTweetCount, fakeTweetCount;
- // Twitter rounds your tweet count up when displaying a tweet count >=10k
- // I find this a bit ridiculous, so this script rounds it down instead
- // I wish Twitter used IDs on things instead of classes
- // test to see if we should do anything, so we don't spew errors left and right
- if( document.querySelectorAll( realTweetCountSelector ).length > 0 ) {
- console.log( 'Page has a tweet count, seeing if it needs to be fixed' );
- realTweetCount = parseInt( document.querySelectorAll( realTweetCountSelector )[0].getAttribute( 'title' ).split( ' ' )[0].split( ',' ).join( '' ) ); // one-liner HOOOOOOOO!
- fakeTweetCount = document.querySelectorAll( fakeTweetCountSelector )[0];
- console.log( 'real tweet count: ' + realTweetCount );
- console.log( 'fake tweet count: ' + fakeTweetCount.textContent );
- if( realTweetCount >= 10000 ) {
- fakeTweetCount.textContent = ( Math.floor( realTweetCount / 100 ) / 10 ).toString() + 'K';
- console.log( 'fixed fake tweet count: ' + fakeTweetCount.textContent );
- }
- }
- /* Documenting that one-liner, because I'm a responsible programmer:
- *
- * The first thing that happens is getting the tweet count. It's stored in a
- * title attribute, so we query the selector, grab the first item in the result
- * collection (because Twitter uses classes where they really should be using
- * IDs), and grab the title attribute in question.
- *
- * Now that we have the tweet count, we split that string into an array around
- * the space that it contains, and grab the first element of that array, which
- * is the tweet count without the word "Tweets" after it.
- *
- * The number is still digit-grouped by a comma, so to remove that comma, we
- * split the string into an array around the comma, and then join the elements
- * of the array back together using an empty string.
- *
- * Finally, we convert the string we have to an integer, so we can do math with
- * it.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement