Advertisement
rccharles

auto navigation

Mar 24th, 2017
535
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. Part #1.
  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     11Mar2017
  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.   https://discussions.apple.com/community/mac_os/mac_os_x_technologies/content
  23.  
  24. but not:
  25.   https://discussions.apple.com/community/mac_os/mac_os_x_technologies
  26.  
  27.    
  28. Why does apple slow down old devices?            66 18035   February 2, 2017 9:15 PM
  29. in Using iPad by gail from maine                 by gail from maine
  30.  
  31.   https://discussions.apple.com/thread/7527129
  32.   https://discussions.apple.com/thread/7527129?start=15&tstart=0
  33.   https://discussions.apple.com/thread/7527129?start=30&tstart=0
  34.   https://discussions.apple.com/thread/7527129?start=45&tstart=0
  35.   https://discussions.apple.com/thread/7527129?start=60&tstart=0
  36.  
  37. https://discussions.apple.com/community/mac_os/os_x_yosemite/content?filterID=contentstatus%5Bpublished%5D~objecttype~objecttype%5Bthread%5D*/
  38.  
  39.  
  40. var watchdog1, watchdog2;
  41. var watchInterval           = 500; // [ms] Waiting for the user to change page
  42. var shortWatchInterval      = 100; // [ms] Waiting for the system to load new list
  43. var debug                   = 2;  // 0 -- no debug
  44.                                   // 1 -- normal debug
  45.                                   // 2 -- intense debug
  46. var priorLastThread         = '';
  47.  
  48. var aDate = new Date();
  49. console.log ("--> lastReadPageButton: add fast links. Debug is  " + debug +
  50.              " on " + aDate );
  51.  
  52. // Setup once page is loaded.
  53. // modify thread content view behaviour (in post-load phase)
  54.  
  55.  if (debug)  console.log('Loaded; Now, initializing');
  56.  // register event listeners
  57.  window.addEventListener('unload', function(e)
  58.         {
  59.           if (debug)  console.log('unloading.');
  60.           stopWatchDogButton(watchdog1);
  61.           stopWatchDogButton(watchdog2);
  62.          
  63.           window.removeEventListener('locationChangeButton',
  64.                                       locationChangeButtonHandler,
  65.                                       true);
  66.           window.removeEventListener('waitingInsertLastPageAnchor',
  67.                                       insertLastPageAnchor,
  68.                                       true);
  69.           if (debug)  console.log('removing e.type is '+ e.type + " arg is " +
  70.                                   arguments.callee);
  71.           // remove anonymous unload. [ Thus unload ]
  72.           window.removeEventListener(e.type,
  73.                                      arguments.callee,
  74.                                      true);
  75.         }, true);
  76.  
  77.  if (debug)  console.log('adding locationChangeButton event listner');
  78.  window.addEventListener('locationChangeButton',
  79.                          locationChangeButtonHandler,
  80.                          true);
  81.  window.addEventListener('waitingInsertLastPageAnchor',
  82.                          insertLastPageAnchor,
  83.                          true);
  84.  // Engage a watch dog
  85.  var prev_href = '';
  86.  if (debug)  console.log('prev_href is ' + prev_href );
  87.  // Spin around here watching for the web address to change.
  88.  // Always spinning here.
  89.  // It takes awhile after the page address changes before the list is loaded
  90.  // so we don't need to check that often.
  91.  watchdog1 = setInterval( function()
  92.         {
  93.           var curr_href = window.location.href;
  94.           if (debug >= 2)  console.log('   prev_href is ' + prev_href +
  95.                                  "\n     curr_href is " + curr_href);
  96.           if (curr_href != prev_href)
  97.             {
  98.               // Page changed
  99.               if (debug)  console.log('---> found new page. ' +
  100.                                       '  curr_href is ' + curr_href +
  101.                                       '  Dispatching short event.');
  102.               window.dispatchEvent(new Event('locationChangeButton'));
  103.               prev_href = curr_href;
  104.             }
  105.          },
  106.          watchInterval); // end of setting watch dog #1
  107.  
  108.  
  109. // -----------------------------------------------------------------------------
  110. function locationChangeButtonHandler(e)
  111.        {
  112.          if (debug)  console.log('---> locationChangeButtonHandler is observed. '+
  113.                                  "Debug is  " + debug);    
  114.          // Give a little time for the page to settle down.
  115.          // Spin around here watching for the web address in the last entry to change.
  116.          // Stop spinning once change is found
  117.          watchdog2 = setInterval( function()
  118.                 {    
  119.                   var trSectionSpin = new Array();
  120.                  
  121.                   trSectionSpin = $("table tr.js-browse-item td.j-td-title");
  122.                   if (debug>=2) console.log("finding title" );
  123.                   // .size() is in Jquery and other many other lib
  124.                   rememberLastThread = $("a:first",
  125.                                           trSectionSpin[trSectionSpin.size()-1]
  126.                                         ).attr("href");
  127.                   if (debug) console.log("last thread on page is " + rememberLastThread);
  128.                   if (priorLastThread != rememberLastThread)
  129.                     {
  130.                       // Page changed
  131.                       if (debug)  console.log('---> found new page: ' +
  132.                                               rememberLastThread + ' Dispatching ' +
  133.                                               'insert anchors event.');
  134.                       window.dispatchEvent(new Event('waitingInsertLastPageAnchor'));
  135.                       priorLastThread = rememberLastThread;
  136.                       stopWatchDogButton(watchdog2);
  137.                     }
  138.                 },
  139.                    shortWatchInterval); // end of setting watch dog
  140.          
  141.         }
  142.  
  143. // -----------------------------------------------------------------------------
  144. function stopWatchDogButton(dog) {
  145.     clearInterval(dog);
  146.     if (debug) console.log('Cleared interval for : ' + dog);
  147. }
  148.  
  149. // -----------------------------------------------------------------------------
  150. function zapPageInAnchor(passOption)
  151.   {
  152.     console.log("clicked.");
  153.     /* What was clicked? */
  154.    /* var actualValue = passedOption.options[passOption.selectedIndex].text;
  155.     if (debug) console.log("actualValue is " + actualValue); */
  156.    
  157.   }
  158. // -----------------------------------------------------------------------------
  159.  
  160. function insertLastPageAnchor()
  161.   {
  162.    var replyCounts = new Array();
  163.    var titleCounts = new Array();
  164.    var done = 0;
  165.    var entrySize = 15;  // number of discussion listed on web page
  166.    var lastPageStart = 0;
  167.    var constructedLink = "";
  168.    var insertA = "";
  169.    var rememberLastThread = "";
  170.    var debugStop = 0;
  171.  
  172.    /* debugStop++;
  173.     if ( debugStop > 5 ) throw new Error();*/
  174.  
  175.     if (debug) console.log('\nInserting last page anchors');/**/
  176.     try
  177.     {
  178.       $("table tr.js-browse-item td.j-td-replies").each(  
  179.         function( index )  
  180.           {  
  181.             replyCounts[index] = $( this ).text();
  182.             if (debug>=2) console.log("reply index is " + index +
  183.                                       " for " + $( this ).text() );
  184.           }  
  185.        ); // End of each
  186.       }
  187.     catch(err)
  188.     {
  189.        console.log("within insertLastPageAnchor got error of " + err.message );
  190.        return;
  191.     }
  192.  
  193.     if (debug) console.log("here...");
  194.     if (debug) for ( i = 0;i<replyCounts.length;++i )
  195.       {
  196.         console.log("reply " + i + " is " + replyCounts[i] );
  197.       }
  198.  
  199.     if (debug) console.log("Find the titles");
  200.     var trSection = new Array();
  201.     trSection = $("table tr.js-browse-item td.j-td-title");
  202.     $("a:first", trSection ).each(  
  203.       function( index )  
  204.         {  
  205.           titleCounts[index] = $( this ).text();
  206.           if (debug) console.log("------  " + $( this ).html() + " ------");
  207.  
  208.           postNumber = $( this ).attr("href").split("/")[2].trim();
  209.           if (debug) console.log("a post number is " + postNumber);
  210.  
  211.           mathFloorInt = Math.floor(replyCounts[index]/entrySize);
  212.           if (debug) console.log ("repies = " + replyCounts[index] + " mathFloorInt is " +
  213.                        mathFloorInt );
  214.           if (mathFloorInt)
  215.           {
  216.             lastPageStart = mathFloorInt * entrySize;
  217.             if (debug) console.log ("... lastPageStart is " + lastPageStart +
  218.                                     " mathFloorInt = " + mathFloorInt +
  219.                                     " entrySize is " + entrySize );
  220.             constructedLink = "https://discussions.apple.com/thread/" +
  221.                           postNumber +
  222.                           "?start=" +
  223.                           lastPageStart +
  224.                           "&tstart=0&begin=last";
  225.              if (debug) console.log("link is " + constructedLink);
  226.              /* Make last link the default for the title link */
  227.              $(this).attr("href",constructedLink);
  228.           /*   insertA = '<span class="rcUserASCModLastReadPage">' +
  229.                        '<a style=" color: SlateBlue; ' +
  230.                        'margin-left: 15px; border: 1px dotted black;" ' +
  231.                        'href="' +
  232.                        constructedLink +
  233.                        '">Last Page</a>' +
  234.                        '</span>';*/
  235.            /*  <select>
  236.                <option value="volvo">Volvo</option>
  237.                <option value="saab">Saab</option>
  238.                <option value="mercedes">Mercedes</option>
  239.                </select>
  240.            */
  241.            /*
  242.            var chr = String.fromCharCode(97 + n);
  243.                      'var searchFor = \'a[href=\' + searchString + \']\';' +
  244.                      'if (debug) console.log(\'searchFor is \' + searchFor);' +
  245.                      var doubleQuote = \'\\"\';' +
  246.                      'var doubleQuote = \'d\\"q\';' +
  247.                           'try '+
  248.                        '{ '+
  249.                        '  $(\'a\').attr(\"href\",actualValue); ' +
  250.                        '} '+
  251.                        'catch(e)'+
  252.                        '{'+
  253.                        '  console.log(\'e is \' + e);' +
  254.                        '}'+
  255.              https://discussions.apple.com/thread/7889277?
  256.              012345678901234567890123456789012345678901234567890123456789
  257.              0         1         2         3         4         5 */
  258.              insertA = '<select class="rcUserASCItemSelect"' +
  259.                        'onchange="var debug = 1;' +
  260.                        'if (debug) console.log(\' clicked.\');' +
  261.                        'var actualText = this.options[this.selectedIndex].text;' +
  262.                        'if (debug) console.log(\'actualText is \' + actualText);' +
  263.                        'var actualValue = this.options[this.selectedIndex].value;' +
  264.                        'if (debug) console.log(\'actualValue is \' + actualValue);' +
  265.                        'var searchString = actualValue.substr(0, 45);' +
  266.                        'if (debug) console.log(\'searchString is \' + searchString);' +
  267.                        'var doubleQuote = String.fromCharCode(34);' +
  268.                        'var searchFor = \'a[href^=\' + doubleQuote + searchString + doubleQuote + \']\';' +
  269.                        'if (debug) console.log(\'searchFor is \' + searchFor);' +
  270.                        'try{$(searchFor).attr(\'href\',actualValue);}catch(e){console.log(\'e is \'+e);}' +
  271.                        'if (debug) console.log(\'Update completed \');' +
  272.                        '">' +            
  273.                        '<option value="'+
  274.                        constructedLink   +
  275.                        '" selected>Last</option>'
  276.              
  277.              /* ansylis by indection
  278.                 68/15 is 4.53
  279.                 last should be 4
  280.                 pull down should be 3,2,1
  281.                 60/15 is 4
  282.                 pull down should be 3,2,1
  283.                 59 is 3.93
  284.                 pull down should be 2,1
  285.                 16/15 is 1.06
  286.                 pull down nothing in pull down ...
  287.                 15/15 is 1
  288.                 14/15 is 0.93
  289.               */
  290.              var iStart = Math.floor(lastPageStart/entrySize)
  291.              if (debug) console.log("iStart is " + iStart);
  292.              if (iStart > 1 ) {
  293.                /* the Last entry for highest page number */
  294.                iStart--
  295.                if (debug) console.log("iStart is " + iStart);
  296.                for (var i = iStart ; i>0 ; i--)
  297.                  {
  298.                    pageStart = i * entrySize;
  299.                    pageConstructedLink = "https://discussions.apple.com/thread/" +
  300.                        postNumber +
  301.                        "?start=" +
  302.                        pageStart +
  303.                        "&tstart=0&begin=" +
  304.                        i.toString();
  305.                    if (debug) console.log("link for " + i.toString() + " is " +
  306.                        pageConstructedLink);
  307.                    var displayPage = (i + 1).toString() ;
  308.                    if (debug) console.log("displayPage is " + displayPage);
  309.                    insertA = insertA +
  310.                      '<option value="' +
  311.                      pageConstructedLink +'">' +
  312.                      displayPage +
  313.                      '</option>';
  314.                  }
  315.                }  // end of if
  316.             if (debug) console.log("Add beginning " );
  317.             /* put in beginning option in case user what to go to the beginning of the display of posts */
  318.             pageStart = "0";
  319.             var displayPage = "1";
  320.             pageConstructedLink = "https://discussions.apple.com/thread/" +
  321.                 postNumber +
  322.                 "?start=" +
  323.                 pageStart +
  324.                 "&tstart=0&begin=" +
  325.                 displayPage;
  326.             if (debug) console.log("link for is " + pageConstructedLink);
  327.             /* Add Option */
  328.             insertA = insertA +
  329.                 '<option value="' +
  330.                 pageConstructedLink +
  331.                 '">' +
  332.                 "beginning" +
  333.                 '</option>';
  334.             insertA = insertA + "</select>"
  335.             if (debug) console.log("inserted is " + insertA);
  336.             // Place in the document to insert selection.
  337.             try
  338.             {
  339.                $( this ).after( insertA );  
  340.             }            
  341.             catch(e)
  342.             {
  343.                 console.log('e is '+e);
  344.             }
  345.            
  346.           } // end of if (mathfloor)        
  347.         } // end of anonymous function
  348.      ); // end of .each
  349.  
  350.  
  351.   } // end of function insertLastPageAnchor
  352.  
  353. // Seems that javascript [ or someone ]
  354. // sends the last value from an assignment statement as the return code to the caller.
  355. done = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement