Advertisement
XT-8147

Twitter - Fix tweet count rounding.user

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