Guest User

Remaining Words

a guest
Dec 5th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2.  
  3. // @name        FIMFiction - Remaining Words and Reading Time
  4.  
  5. // @namespace   Selbi, rewritten by Queue
  6.  
  7. // @include     http*://fimfiction.net/*
  8.  
  9. // @include     http*://www.fimfiction.net/*
  10.  
  11. // @version     3
  12.  
  13. // ==/UserScript==
  14.  
  15. var wpm=188;
  16.  
  17. var all_Unsorted_Lists = document.getElementsByTagName('ul');
  18.  
  19. for (var i=0; i<all_Unsorted_Lists.length; i++)
  20. {
  21.     if (all_Unsorted_Lists[i].className == "chapters")
  22.     {
  23.         //Grabs the elements of the word counts.
  24.         var chapter_Word_Counts_By_Element=all_Unsorted_Lists[i].getElementsByClassName('word_count');
  25.  
  26.         //Grabs the elements, which display if a chapter has been read.
  27.         var chapter_Read_Status = all_Unsorted_Lists[i].getElementsByClassName('chapter-read-icon');
  28.  
  29.         //We know how many chapters there are because we just grabbed them, so we're putting the number in a variable.
  30.         var number_Of_Chapters=chapter_Read_Status.length;
  31.  
  32.         //We'll update these when we check which chapters have been read.
  33.         var number_Of_Read_Chapters=0;
  34.         var number_Of_Read_Words=0;
  35.  
  36.         //This will let us rip the actual word counts from the html of the word count elements.
  37.         var word_Count_Regex = new RegExp("[0-9,]+");
  38.  
  39.         //This is where we'll store the word counts in number form.
  40.         var int_Word_Counts=[];
  41.  
  42.         //This converts and stores the word counts to intergers.
  43.         for(var ii=0;ii<chapter_Word_Counts_By_Element.length;ii++)
  44.         {
  45.             //This converts and stores the word counts to intergers.
  46.             int_Word_Counts[ii]=parseInt(word_Count_Regex.exec(chapter_Word_Counts_By_Element[ii].innerHTML)[0].replace(",",""));
  47.  
  48.             //We only want the estimated time applied to the chapters, not the total, which is at the end of the list.
  49.             if(ii<chapter_Word_Counts_By_Element.length-1)
  50.             {
  51.                 //This posts the estimated reading time next to each chapter.
  52.                 var temp="[ "+convertToTime(int_Word_Counts[ii])+" ] "+chapter_Word_Counts_By_Element[ii].innerHTML;
  53.                 chapter_Word_Counts_By_Element[ii].innerHTML=temp;
  54.             }
  55.         }
  56.  
  57.         //Here we check which chapters have been read.
  58.         for(var ii=0;ii<chapter_Read_Status.length;ii++)
  59.         {
  60.             //The check.
  61.             if(chapter_Read_Status[ii].className=="chapter-read-icon chapter-read")
  62.             {
  63.                 number_Of_Read_Chapters++;
  64.                 //Looks up the number of words in the chapter and adds it to the read total.
  65.                 number_Of_Read_Words+=int_Word_Counts[ii];
  66.             }
  67.         }
  68.         //Grabs the total word count from the array.
  69.         var total_Word_Count=int_Word_Counts[chapter_Word_Counts_By_Element.length-1];
  70.         //Calculates the percentage read from the number of words read vs total words.
  71.         var percentage_Read=(number_Of_Read_Words/total_Word_Count).toFixed(2)*100;
  72.         //Builds the HTML for the final entry.
  73.         var temp=percentage_Read+"% complete. "+number_Of_Read_Chapters+" chapters of "+number_Of_Chapters+" chapters read. [ "+convertToTime(number_Of_Read_Words)+" / "+ convertToTime(total_Word_Count)+" ] "+chapter_Word_Counts_By_Element[chapter_Word_Counts_By_Element.length-1].innerHTML;
  74.         //Inserts the HTML.
  75.         chapter_Word_Counts_By_Element[chapter_Word_Counts_By_Element.length-1].innerHTML=temp;
  76.     }
  77. }
  78. //Selbi's function slightly modified for readability and formatting.
  79. function convertToTime(word_Count)
  80. {
  81.  
  82.     var read_Time = (Math.ceil(word_Count/wpm));
  83.  
  84.     var post_Time;
  85.  
  86.     if (read_Time > 60)
  87.     {
  88.  
  89.         post_Time = "~ " + ((Math.ceil(read_Time/6))/10).toFixed(1) + " h";
  90.  
  91.     }
  92.     else
  93.     {
  94.  
  95.         post_Time ="~ " + read_Time + " min";
  96.  
  97.     }
  98.  
  99.     return post_Time;
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment