Advertisement
rccharles

Link to last page in list pages

Feb 22nd, 2017
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            ASC Display links to last page on all pages of discussion.
  3. // @namespace       bubo-bubo/gmscripts
  4. // @description     ASC - Add last page button on list pages
  5. // @include     https://discussions.apple.com/content?*
  6. // @include     https://discussions.apple.com/community/*/content?*
  7. // @version     1
  8. // @grant       none
  9. // ==/UserScript==
  10.  
  11. // Uses Hiroto's event handler design.
  12.  
  13. /*
  14.    Add link to last page of long discussions.
  15.  
  16. Supported lists:
  17.   https://discussions.apple.com/content?filterID=following~objecttype~objecttype[thread]
  18.   https://discussions.apple.com/community/ipad/ipad_in_business_and_education/content?filterID=contentstatus
  19.     %5Bpublished%5D%7Eobjecttype%7Eobjecttype%5Bthread%5D
  20.   https://discussions.apple.com/community/mac_os/os_x_el_capitan/content?filterID=contentstatus[published]
  21.     ~objecttype~objecttype[thread]
  22.  
  23. but not:
  24.   https://discussions.apple.com/community/lounge/full_host_bar
  25.  
  26.    
  27. Why does apple slow down old devices?            66 18035   February 2, 2017 9:15 PM
  28. in Using iPad by gail from maine                 by gail from maine
  29.  
  30.   https://discussions.apple.com/thread/7527129
  31.   https://discussions.apple.com/thread/7527129?start=15&tstart=0
  32.   https://discussions.apple.com/thread/7527129?start=30&tstart=0
  33.   https://discussions.apple.com/thread/7527129?start=45&tstart=0
  34.   https://discussions.apple.com/thread/7527129?start=60&tstart=0
  35.  
  36. https://discussions.apple.com/community/mac_os/os_x_yosemite/content?filterID=contentstatus%5Bpublished%5D~objecttype~objecttype%5Bthread%5D*/
  37.  
  38.  
  39. var watchdog1, watchdog2;
  40. var watchInterval           = 500; // [ms] Waiting for the user to change page
  41. var shortWatchInterval      = 100; // [ms] Waiting for the system to load new list
  42. var debug                   = 0;  // 0 -- no debug
  43.                                   // 1 -- normal debug
  44.                                   // 2 -- intense debug
  45. var priorLastThread         = '';
  46.  
  47. var aDate = new Date();
  48. console.log ("--> lastReadPageButton: add fast links. Debug is  " + debug +
  49.              " on " + aDate );
  50.  
  51. // Setup once page is loaded.
  52. // modify thread content view behaviour (in post-load phase)
  53.  
  54.  if (debug)  console.log('Loaded; Now, initializing');
  55.  // register event listeners
  56.  window.addEventListener('unload', function(e)
  57.         {
  58.           if (debug)  console.log('unloading.');
  59.           stopWatchDogButton(watchdog1);
  60.           stopWatchDogButton(watchdog2);
  61.          
  62.           window.removeEventListener('locationChangeButton',
  63.                                       locationChangeButtonHandler,
  64.                                       true);
  65.           window.removeEventListener('waitingInsertLastPageAnchor',
  66.                                       insertLastPageAnchor,
  67.                                       true);
  68.           if (debug)  console.log('removing e.type is '+ e.type + " arg is " +
  69.                                   arguments.callee);
  70.           // remove anonymous unload. [ Thus unload ]
  71.           window.removeEventListener(e.type,
  72.                                      arguments.callee,
  73.                                      true);
  74.         }, true);
  75.  
  76.  if (debug)  console.log('adding locationChangeButton event listner');
  77.  window.addEventListener('locationChangeButton',
  78.                          locationChangeButtonHandler,
  79.                          true);
  80.  window.addEventListener('waitingInsertLastPageAnchor',
  81.                          insertLastPageAnchor,
  82.                          true);
  83.  // Engage a watch dog
  84.  var prev_href = '';
  85.  if (debug)  console.log('prev_href is ' + prev_href );
  86.  // Spin around here watching for the web address to change.
  87.  // Always spinning here.
  88.  // It takes awhile after the page address changes before the list is loaded
  89.  // so we don't need to check that often.
  90.  watchdog1 = setInterval( function()
  91.         {
  92.           var curr_href = window.location.href;
  93.           if (debug >= 2)  console.log('   prev_href is ' + prev_href +
  94.                                  "\n     curr_href is " + curr_href);
  95.           if (curr_href != prev_href)
  96.             {
  97.               // Page changed
  98.               if (debug)  console.log('---> found new page. ' +
  99.                                       '  curr_href is ' + curr_href +
  100.                                       '  Dispatching short event.');
  101.               window.dispatchEvent(new Event('locationChangeButton'));
  102.               prev_href = curr_href;
  103.             }
  104.          },
  105.          watchInterval); // end of setting watch dog #1
  106.  
  107.  
  108. // -----------------------------------------------------------------------------
  109. function locationChangeButtonHandler(e)
  110.        {
  111.          if (debug)  console.log('---> locationChangeButtonHandler is observed. '+
  112.                                  "Debug is  " + debug);    
  113.          // Give a little time for the page to settle down.
  114.          // Spin around here watching for the web address in the last entry to change.
  115.          // Stop spinning once change is found
  116.          watchdog2 = setInterval( function()
  117.                 {    
  118.                   var trSectionSpin = new Array();
  119.                  
  120.                   trSectionSpin = $("table tr.js-browse-item td.j-td-title");
  121.                   if (debug>=2) console.log("finding title" );
  122.                   // .size() is in Jquery and other many other lib
  123.                   rememberLastThread = $("a:first",
  124.                                           trSectionSpin[trSectionSpin.size()-1]
  125.                                         ).attr("href");
  126.                   if (debug) console.log("last thread on page is " + rememberLastThread);
  127.                   if (priorLastThread != rememberLastThread)
  128.                     {
  129.                       // Page changed
  130.                       if (debug)  console.log('---> found new page: ' +
  131.                                               rememberLastThread + ' Dispatching ' +
  132.                                               'insert anchors event.');
  133.                       window.dispatchEvent(new Event('waitingInsertLastPageAnchor'));
  134.                       priorLastThread = rememberLastThread;
  135.                       stopWatchDogButton(watchdog2);
  136.                     }
  137.                 },
  138.                    shortWatchInterval); // end of setting watch dog
  139.          
  140.         }
  141.  
  142. // -----------------------------------------------------------------------------
  143. function stopWatchDogButton(dog) {
  144.     clearInterval(dog);
  145.     if (debug) console.log('Cleared interval for : ' + dog);
  146. }
  147.  
  148. // -----------------------------------------------------------------------------
  149. var replyCounts = new Array();
  150. var titleCounts = new Array();
  151. var done = 0;
  152. var entrySize = 15;  // number of discussion listed on web page
  153. var lastPageStart = 0;
  154. var constructedLink = "";
  155. var insertA = "";
  156. var rememberLastThread = "";
  157. var debugStop = 0;
  158.  
  159. function insertLastPageAnchor()
  160.   {
  161.    /* debugStop++;
  162.     if ( debugStop > 5 ) throw new Error();*/
  163.  
  164.     if (debug) console.log('\nInserting last page anchors');/**/
  165.     try
  166.     {
  167.       $("table tr.js-browse-item td.j-td-replies").each(  
  168.         function( index )  
  169.           {  
  170.             replyCounts[index] = $( this ).text();
  171.             console.log("index is " + index );
  172.             if (debug>=2) console.log("reply index is " + index +
  173.                                       " for " + $( this ).text() );
  174.           }  
  175.        ); // End of each
  176.       }
  177.     catch(err)
  178.     {
  179.        console.log("within insertLastPageAnchor got error of " + err.message );
  180.        return;
  181.     }
  182.  
  183.     console.log("here...");
  184.     if (debug) for ( i = 0;i<replyCounts.length;++i )
  185.       {
  186.         console.log("reply " + i + " is " + replyCounts[i] );
  187.       }
  188.  
  189.     if (debug) console.log("Find the titles");
  190.     var trSection = new Array();
  191.     trSection = $("table tr.js-browse-item td.j-td-title");
  192.     $("a:first", trSection ).each(  
  193.       function( index )  
  194.         {  
  195.           titleCounts[index] = $( this ).text();
  196.           if (debug) console.log("------  " + $( this ).html() + " ------");
  197.  
  198.           postNumber = $( this ).attr("href").split("/")[2].trim();
  199.           if (debug) console.log("a post number is " + postNumber);
  200.  
  201.           mathFloorInt = Math.floor(replyCounts[index]/entrySize);
  202.           console.log ("repies = " + replyCounts[index] + " mathFloorInt is " +
  203.                        mathFloorInt );
  204.           if (mathFloorInt)
  205.           {
  206.             lastPageStart = mathFloorInt * entrySize;
  207.             constructedLink = "https://discussions.apple.com/thread/" +
  208.                           postNumber +
  209.                           "?start=" +
  210.                           lastPageStart +
  211.                           "&tstart=0";
  212.              if (debug) console.log("link is " + constructedLink);
  213.    
  214.              insertA = '<span class="rcUserASCModLastReadPage">' +
  215.                        '<a style=" color: SlateBlue; ' +
  216.                        'margin-left: 15px; border: 1px dotted black;" ' +
  217.                        'href="' +
  218.                        constructedLink +
  219.                        '">Last Page</a>' +
  220.                        '</span>';
  221.              if (debug) console.log("inserted is " + insertA);
  222.              $( this ).after( insertA );  
  223.            }          
  224.         } // end of anonymous function
  225.      ); // end of .each
  226.  
  227.  
  228.   } // end of function insertLastPageAnchor
  229.  
  230. // Seems that javascript [ or someone ]
  231. // sends the last value from an assignment statement as the return code to the caller.
  232. done = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement