Advertisement
rccharles

ASC LAST page adjustment Part 2 ver 3

Apr 11th, 2017
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. // ==UserScript==
  2. // @name            ASC adjust to last page on display of posts. Part #2
  3. // @namespace       bubo-bubo/gmscripts
  4. // @description     ASC - Add last page button on list pages
  5. // @include         https://discussions.apple.com/message/*
  6. // @include         https://discussions.apple.com/thread/*
  7. // @include         https://discussions.apple.com/docs/*
  8. // @version         4Apr2017
  9. // @grant           none
  10. // ==/UserScript==
  11.  
  12. /* We may not end up on the last page of a display of posts when Apple hosts
  13.  deleted some posts.
  14.  
  15.  https://discussions.apple.com/docs/DOC-10241?start=45&tstart=0&begin=Prior-1
  16.  https://discussions.apple.com/thread/5469616?start=1020&tstart=0&begin=yes
  17.  
  18.  How the input web address is contructed.
  19.         constructedLink = "https://discussions.apple.com/thread/" +
  20.                           postNumber +
  21.                           "?start=" +
  22.                           lastPageStart +
  23.                           "&tstart=0&begin=yes";
  24.  
  25.  */
  26.  
  27. var debug                   = 2;  // 0 -- no debug
  28.                                   // 1 -- normal debug
  29.                                   // 2 -- intense debug
  30.  
  31.  
  32. var aDate = new Date();
  33. console.log ("--> adjustLastPage: add fast links Part #2. Debug is  " + debug +
  34.              " on " + aDate );
  35.  
  36. // Setup once page is loaded.
  37. if (debug)  console.log('Loaded; Now, initializing');
  38.  
  39.  
  40. var url = window.location.href;
  41. var params = url.split('?');
  42.  
  43. if (debug) console.log("web page parms " + params[1]);
  44.  
  45.  
  46. // See if we have already adjusted this page.
  47.     try
  48.     {
  49.       pageStatus = Window.asc_jx_last_page_load_remember;
  50.      
  51.      }// End of try
  52.     catch(err)
  53.     {
  54.        console.log("looking for variable pageStatus " + err.message );
  55.        //return;
  56.     }
  57. if (debug) console.log("pageStatus is " + pageStatus);
  58.  
  59.  
  60. myPassVar = getURLParameter('begin').toLowerCase();
  61. if (debug) console.log("begin parm is " + myPassVar);
  62.  
  63. if (debug) console.log("myPassVar.substr(0,6) " + myPassVar.substr(0,6));
  64. // Do we need to see if we need to adjust to the last page?
  65. if ( ( myPassVar == "yes"|| myPassVar == "last" || myPassVar.substr(0,6) == "prior-") && pageStatus != "yes")
  66.   {
  67.     // Global remember, so we can check if we already adjusted this page
  68.     Window.asc_jx_last_page_load_remember = "yes";
  69.    
  70.     if (debug>= 2) console.log("Have set Window.asc_jx_last_page_load_remember to  " +
  71.                            Window.asc_jx_last_page_load_remember);
  72.     currentPageStart = getURLParameter('start');
  73.     if (debug) console.log("start parm is " + currentPageStart);
  74.    
  75.     // Check what page we are on.
  76.     var pageNumbers =  $("span.current-page").text();
  77.     if (debug) console.log("pageNumbers " + pageNumbers);
  78.    
  79.  
  80.    
  81.     // Ejaz explans how to pull words from a string
  82.     //   in  http://stackoverflow.com/questions/16223043/get-second-and-third-words-from-string
  83.     // The technique using split() will fail if the string has multiple spaces in it.
  84.     // Don't forget to check your array length using .length property to avoid
  85.     //   getting undefined in your variables.
  86.     var actualPages = pageNumbers.match(/\S+/gi);
  87.     if (debug) console.log("closer "+ actualPages);
  88.    
  89.     var currentPage = parseInt(actualPages[1]);    
  90.     var lastPage = parseInt(actualPages[3]);
  91.     if (debug) console.log("currentPage " + currentPage.toString() );
  92.     if (debug) console.log("lastPage " + lastPage.toString() );
  93.    
  94.     if (myPassVar.substr(0,6) == "prior-")
  95.     {
  96.       /*
  97.       <a href="/thread/7899185?start=90&amp;tstart=0"
  98.       title="Select to go to last page"
  99.       role="button"
  100.       aria-label="Select to go to last page"
  101.       onclick="jspaginate.init('last', '7'); return false;"
  102.       class="js-pagination-next j-paginate-last">last</a>
  103.       */
  104.       var lastInfo = $("a.js-pagination-next.j-paginate-last")
  105.  
  106.       var lastHref = $(lastInfo).attr("href")      
  107.       if (debug>= 2) console.log("lastHref is " + lastHref);
  108.       /* Find where the last page starts.  With this, we can figure out
  109.          the post per page.        
  110.          lastHref is /thread/7899185?start=90&tstart=0
  111.       */
  112.       var pageStartCount = parseInt(lastHref.split("=")[1].split("&")[0].replace(/%2C/g, ''))
  113.       if (debug>= 2) console.log("pageStartCount is " + pageStartCount);
  114.       var itemsPerPage = pageStartCount/(lastPage-1)  // 15    
  115.       if (debug>= 2) console.log("itemsPerPage is " + itemsPerPage);
  116.       var backPrior = parseInt(myPassVar.split("-")[1])
  117.       if (debug>= 2) console.log("backPrior is " + backPrior);    
  118.       if (debug>= 2) console.log("(currentPage + backPrior)  " + (currentPage + backPrior));  
  119.       if ((currentPage + backPrior) !=  lastPage )
  120.         {
  121.           if (debug) console.log( "Need to adjust. lastPage(backPrior *itemsPerPage) is  "  +
  122.                                 ( (lastPage-backPrior-1) * itemsPerPage ) +
  123.                                 " currentPage is " +        
  124.                                  (lastPage-backPrior) );        
  125.           /* count of posts starts at zero whereas page count starts at 1 */
  126.           jspaginate.update(
  127.                            ( (lastPage-backPrior-1) * itemsPerPage ), // target
  128.                            (lastPage-backPrior) // current          
  129.                           )
  130.         }
  131.       else
  132.         {
  133.           if (debug) console.log("on correct page which is " + currentPage);
  134.         }
  135.          
  136.     } // Processing for prior
  137.     else
  138.     {
  139.     // If the current page is differernt than the last page, apple removed some post
  140.     // and we need to adjust to last page.
  141.     if ( currentPage != lastPage )
  142.       {
  143.         if (debug) console.log("mismatched pages ");
  144.         // Adjust to the last page.
  145.         // Handy dandy ASC function is used when user clicks on last button, so use it
  146.         jspaginate.init('last', actualPages[3]);
  147.         if (debug) console.log("Page adjusted! " );
  148.       }  
  149.     }
  150.   }
  151. else
  152.   {
  153.     if (debug) console.log("... Nothing to do! ...");
  154.   }
  155.  
  156.  
  157. // Seems that javascript [ or someone ]
  158. // sends the last value from an assignment statement as the return code to the caller.
  159. done = 0;
  160.  
  161. // ----------------------------------------------------------------------------------------
  162. //
  163. // http://stackoverflow.com/questions/11582512/how-to-get-url-parameters-with-javascript
  164. //
  165. // example:
  166. //   myvar = getURLParameter('myvar');
  167.  
  168. function getURLParameter(name) {
  169.   return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) ||
  170.                             [null, ''])[1].replace(/\+/g, '%20')) || null;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement