Advertisement
Guest User

FFnet Review Indexing

a guest
Jul 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Review Indexing Script
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Indexing Reviews for https://fanlore.org/w/index.php?title=Dreaming_of_Sunshine
  6. // @author       You
  7. // @match        https://www.fanfiction.net/r/*
  8. // @grant        none
  9. // @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.   'use strict';
  14.  
  15.   Date.prototype.getMonthName = function(lang) {
  16.     lang = lang && (lang in Date.locale) ? lang : 'en';
  17.     return Date.locale[lang].month_names[this.getMonth()];
  18.   };
  19.   Date.prototype.getMonthNameShort = function(lang) {
  20.       lang = lang && (lang in Date.locale) ? lang : 'en';
  21.       return Date.locale[lang].month_names_short[this.getMonth()];
  22.   };
  23.   Date.locale = {
  24.       en: {
  25.          month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  26.          month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  27.       }
  28.   };
  29.  
  30.   // Adding a function to arrays to get the last element.
  31.   if (!Array.prototype.last){
  32.     Array.prototype.last = function(){
  33.         return this[this.length - 1];
  34.     };
  35.   };
  36.  
  37.   // Adding the format function to Strings.
  38.   String.prototype.format = function() {
  39.     var formatted = this;
  40.     for (var i = 0; i < arguments.length; i++) {
  41.       var regexp = new RegExp('\\{'+i+'\\}', 'gi');
  42.       formatted = formatted.replace(regexp, arguments[i]);
  43.     }
  44.     return formatted;
  45.   };
  46.  
  47.  
  48.   // There's no where on the page that has the author information for the story. Could potentially load up another page, but eh.
  49.   // Set this value if you want to automate it; otherwise leave it as is and search/replace later.
  50.   var author = "(AUTHOR)"
  51.  
  52.   // Get Page URL
  53.   var pageURL = self.location;
  54.  
  55.   // find the table element containing the reviews
  56.   var reviewTable = $('.table')[0];
  57.   // Get the story title - table . thead . tr . th . a(href) - 'https://www.fanfiction.net/s/7347955/1/Dreaming-of-Sunshine'
  58.   var storyTitleHRef = reviewTable.children[0].children[0].children[0].children[1].href;
  59.   var storyTitle = storyTitleHRef.split('/').last();
  60.   // get the list of individual reviews - table . tbody . list<tr>
  61.   var reviews = reviewTable.children[1].children;
  62.  
  63.   for(let i = 0; i < reviews.length; i++) {
  64.     // Get the attributes we want
  65.     // Small... thing. Contains date and chapter
  66.     var chapter = $(reviews[i].children[0]).children('small')[0].textContent.split('.')[0].trim();
  67.     var date = new Date(0);
  68.     date.setUTCSeconds(parseInt($(reviews[i].children[0]).children('small')[0].children[0].dataset.xutime));
  69.     // Username is either just text if it's anon, or this value is blank and the next element is an aref
  70.     var username = reviews[i].children[0].childNodes[2].textContent.trim();
  71.     if(username === "") {
  72.       username = reviews[i].children[0].childNodes[3].href.split('/').last();
  73.     }
  74.    
  75.     var indexString = "{0}, [{1} {2} by {3} - Review for {4}] Posted {5} {6} {7}".format(username, pageURL, storyTitle, author, chapter, date.getDate(), date.getMonthName(), date.getFullYear());
  76.     var indexSpan = "<span style='color:gray;font-style:italic'>" + indexString + "</span>";
  77.    
  78.     reviews[i].children[0].innerHTML += indexSpan;
  79.   }
  80. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement