Advertisement
rccharles

ASC LAST page adjustment Part 2

Mar 10th, 2017
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. // @version         1.0
  8. // @grant           none
  9. // ==/UserScript==
  10.  
  11. /* We may not end up on the last page of a display of posts when Apple hosts
  12.  deleted some posts.
  13.  
  14.  https://discussions.apple.com/thread/5469616?start=1020&tstart=0&begin=yes
  15.  
  16.  How the input web address is contructed.
  17.         constructedLink = "https://discussions.apple.com/thread/" +
  18.                           postNumber +
  19.                           "?start=" +
  20.                           lastPageStart +
  21.                           "&tstart=0&begin=yes";
  22.  
  23.  */
  24.  
  25. var debug                   = 0;  // 0 -- no debug
  26.                                   // 1 -- normal debug
  27.                                   // 2 -- intense debug
  28.  
  29.  
  30. var aDate = new Date();
  31. console.log ("--> adjustLastPage: add fast links. Debug is  " + debug +
  32.              " on " + aDate );
  33.  
  34. // Setup once page is loaded.
  35. if (debug)  console.log('Loaded; Now, initializing');
  36.  
  37.  
  38. var url = window.location.href;
  39. var params = url.split('?');
  40.  
  41. if (debug) console.log("web page parms " + params[1]);
  42.  
  43.  
  44. // See if we have already adjusted this page.
  45.     try
  46.     {
  47.       pageStatus = Window.asc_jx_last_page_load_remember;
  48.      
  49.      }// End of try
  50.     catch(err)
  51.     {
  52.        console.log("looking for variable pageStatus " + err.message );
  53.        return;
  54.     }
  55. if (debug) console.log("pageStatus is " + pageStatus);
  56.  
  57.  
  58. myPassVar = getURLParameter('begin');
  59. if (debug) console.log("begin parm is " + myPassVar);
  60.  
  61. // Do we need to see if we need to adjust to the last page?
  62. if ( myPassVar == "yes" && pageStatus != "yes")
  63.   {
  64.     // Global remember, so we can check if we already adjusted this page
  65.     Window.asc_jx_last_page_load_remember = "yes";
  66.    
  67.     if (debug>= 2) console.log("Have set Window.asc_jx_last_page_load_remember to  " +
  68.                            Window.asc_jx_last_page_load_remember);
  69.     currentPageStart = getURLParameter('start');
  70.     if (debug) console.log("start parm is " + currentPageStart);
  71.    
  72.     // Check what page we are on.
  73.     var pageNumbers =  $("span.current-page").text();
  74.     if (debug) console.log("pageNumbers " + pageNumbers);
  75.    
  76.     // Ejaz explans how to pull words from a string
  77.     //   in  http://stackoverflow.com/questions/16223043/get-second-and-third-words-from-string
  78.     // The technique using split() will fail if the string has multiple spaces in it.
  79.     // Don't forget to check your array length using .length property to avoid
  80.     //   getting undefined in your variables.
  81.     var actualPages = pageNumbers.match(/\S+/gi);
  82.     if (debug) console.log("closer "+ actualPages);
  83.    
  84.     var currentPage = parseInt(actualPages[1]);    
  85.     var lastPage = parseInt(actualPages[3]);
  86.     if (debug) console.log("current page is " + currentPage.toString() );
  87.     if (debug) console.log("last page is " + lastPage.toString() );
  88.    
  89.     // If the current page is differernt than the last page, apple removed some post
  90.     // and we need to adjust to last page.
  91.     if ( currentPage != lastPage )
  92.       {
  93.         if (debug) console.log("mismatched pages ");
  94.         // Adjust to the last page.
  95.         // Handy dandy ASC function is used when user clicks on last button, so use it
  96.         jspaginate.init('last', actualPages[3]);
  97.  
  98.         if (debug) console.log("Page adjusted! " );
  99.       }  
  100.   }
  101. else
  102.   {
  103.     if (debug) console.log("... Nothing to do! ...");
  104.   }
  105.  
  106.  
  107. // Seems that javascript [ or someone ]
  108. // sends the last value from an assignment statement as the return code to the caller.
  109. done = 0;
  110.  
  111. // ----------------------------------------------------------------------------------------
  112. //
  113. // http://stackoverflow.com/questions/11582512/how-to-get-url-parameters-with-javascript
  114. //
  115. // example:
  116. //   myvar = getURLParameter('myvar');
  117.  
  118. function getURLParameter(name) {
  119.   return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) ||
  120.                             [null, ''])[1].replace(/\+/g, '%20')) || null;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement