Advertisement
rccharles

ASC LAST page adjustment Part 1 ver 3

Apr 11th, 2017
490
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. // @include     https://discussions.apple.com/people/*/content?filterID=*
  8. // @version     3Apr2017
  9. // @grant       none
  10. // ==/UserScript==
  11.  
  12. var howToOpen = "no";  // "new" Open the display of posts in a new page or tab.  
  13.                         // How the user set their web browser determines which one.
  14.                         // Setting anyother value like "no" will open the link as before.
  15.  
  16.  
  17. // Uses Hiroto's event handler design.
  18.  
  19. /*
  20.    Add link to last page of long discussions.
  21.  
  22. Supported lists:
  23.   https://discussions.apple.com/content?filterID=following~objecttype~objecttype[thread]
  24.   https://discussions.apple.com/community/ipad/ipad_in_business_and_education/content?filterID=contentstatus
  25.     %5Bpublished%5D%7Eobjecttype%7Eobjecttype%5Bthread%5D
  26.   https://discussions.apple.com/community/mac_os/os_x_el_capitan/content?filterID=contentstatus[published]
  27.     ~objecttype~objecttype[thread]
  28.   https://discussions.apple.com/community/mac_os/mac_os_x_technologies/content
  29.   https://discussions.apple.com/people/rccharles/content?filterID=following~objecttype~objecttype%5Bdocument%5D
  30.  
  31. but not [no content tag]:
  32.   https://discussions.apple.com/community/mac_os/mac_os_x_technologies
  33.  
  34.    
  35. Why does apple slow down old devices?            66 18035   February 2, 2017 9:15 PM
  36. in Using iPad by gail from maine                 by gail from maine
  37.  
  38.   https://discussions.apple.com/thread/7527129
  39.   https://discussions.apple.com/thread/7527129?start=15&tstart=0
  40.   https://discussions.apple.com/thread/7527129?start=30&tstart=0
  41.   https://discussions.apple.com/thread/7527129?start=45&tstart=0
  42.   https://discussions.apple.com/thread/7527129?start=60&tstart=0
  43.  
  44. User tips
  45.   https://discussions.apple.com/docs/DOC-10241
  46.   https://discussions.apple.com/docs/DOC-10241#start=25
  47.   https://discussions.apple.com/docs/DOC-10241#start=50
  48.  
  49. https://discussions.apple.com/community/mac_os/os_x_yosemite/content?filterID=contentstatus%5Bpublished%5D~objecttype~objecttype%5Bthread%5D*/
  50.  
  51.  
  52.  
  53.  
  54. var watchdog1, watchdog2;
  55. var watchInterval           = 500; // [ms] Waiting for the user to change page
  56. var shortWatchInterval      = 100; // [ms] Waiting for the system to load new list
  57. var debug                   = 2;   // 0 -- no debug
  58.                                    // 1 -- normal debug
  59.                                    // 2 -- intense debug
  60. var priorLastThread         = '';
  61.  
  62.  
  63. var aDate = new Date();
  64. console.log ("--> lastReadPageButton: add pulldown with links. Debug is  " + debug +
  65.              " on " + aDate );
  66.  
  67. // Setup once page is loaded.
  68. // modify thread content view behaviour (in post-load phase)
  69.  
  70.  if (debug)  console.log('Loaded; Now, initializing');
  71.  // register event listeners
  72.  window.addEventListener('unload', function(e)
  73.         {
  74.           if (debug)  console.log('unloading.');
  75.           stopWatchDogButton(watchdog1);
  76.           stopWatchDogButton(watchdog2);
  77.          
  78.           window.removeEventListener('locationChangeButton',
  79.                                       locationChangeButtonHandler,
  80.                                       true);
  81.           window.removeEventListener('waitingInsertLastPageAnchor',
  82.                                       insertLastPageAnchor,
  83.                                       true);
  84.           if (debug)  console.log('removing e.type is '+ e.type + " arg is " +
  85.                                   arguments.callee);
  86.           // remove anonymous unload. [ Thus unload ]
  87.           window.removeEventListener(e.type,
  88.                                      arguments.callee,
  89.                                      true);
  90.         }, true);
  91.  
  92.  if (debug>=2)  console.log('adding locationChangeButton event listner');
  93.  window.addEventListener('locationChangeButton',
  94.                          locationChangeButtonHandler,
  95.                          true);
  96.  window.addEventListener('waitingInsertLastPageAnchor',
  97.                          insertLastPageAnchor,
  98.                          true);
  99.  // Engage a watch dog
  100.  var prev_href = '';
  101.  if (debug)  console.log('prev_href is ' + prev_href );
  102.  // Spin around here watching for the web address to change.
  103.  // Always spinning here.
  104.  // It takes awhile after the page address changes before the list is loaded
  105.  // so we don't need to check that often.
  106.  watchdog1 = setInterval( function()
  107.         {
  108.           var curr_href = window.location.href;
  109.           if (debug >= 2)  console.log('   prev_href is ' + prev_href +
  110.                                  "\n     curr_href is " + curr_href);
  111.           if (curr_href != prev_href)
  112.             {
  113.               // Page changed
  114.               if (debug)  console.log('======> found new page. ' +
  115.                                       '  curr_href is ' + curr_href +
  116.                                       ' <======');
  117.               window.dispatchEvent(new Event('locationChangeButton'));
  118.               prev_href = curr_href;
  119.             }
  120.          },
  121.          watchInterval); // end of setting watch dog #1
  122.  
  123.  
  124. // -----------------------------------------------------------------------------
  125. function locationChangeButtonHandler(e)
  126.        {
  127.          if (debug)  console.log('---> locationChangeButtonHandler is observed. '+
  128.                                  "Debug is  " + debug);    
  129.          // Give a little time for the page to settle down.
  130.          // Spin around here watching for the web address in the last entry to change.
  131.          // Stop spinning once change is found
  132.          watchdog2 = setInterval( function()
  133.                 {    
  134.                   var trSectionSpin = new Array();
  135.                  
  136.                   trSectionSpin = $("table tr.js-browse-item td.j-td-title");
  137.                   if (debug>=2) console.log("finding title" );
  138.                   // .size() is in Jquery and other many other lib
  139.                   rememberLastThread = $("a:first",
  140.                                           trSectionSpin[trSectionSpin.size()-1]
  141.                                         ).attr("href");
  142.                   if (debug>=2) console.log("last thread on page is " + rememberLastThread);
  143.                   rememberLastThread = parseHref(rememberLastThread);
  144.                   if (debug) console.log("adjusted last thread on page is " + rememberLastThread);                                        
  145.                  
  146.                   if (priorLastThread != rememberLastThread)
  147.                     {
  148.                       // Page changed
  149.                       if (debug)  console.log('---> found new page: ' +
  150.                                               rememberLastThread + ' Dispatching ' +
  151.                                               'insert anchors event.');
  152.                                      
  153.                       window.dispatchEvent(new Event('waitingInsertLastPageAnchor'));
  154.                       priorLastThread = rememberLastThread;
  155.                       stopWatchDogButton(watchdog2);
  156.                     }
  157.                 },
  158.                    shortWatchInterval); // end of setting watch dog
  159.          
  160.         }
  161.  
  162. // -----------------------------------------------------------------------------
  163. function parseHref(hrefString) {
  164.          /* input: ASC thread URL
  165.             output: thread id or doc id */
  166.                /*
  167.              from ASC:
  168.              <a href="/thread/7903386 ">Find and delete duplicates within Photos</a>
  169.              after I change the href:
  170.              <a href="https://discussions.apple.com/thread/7900219?start=15&amp;tstart=0&amp;begin=last">
  171.              Applescript to merge CSV files</a>
  172.              <a href="/docs/DOC-10241 ">Minimal CSS tweaks for ASC</a>
  173.              /docs/DOC-10962
  174.              /thread/7903386
  175.              https://discussions.apple.com/thread/
  176.              /thread/7903386
  177.              012345678901234567890123456789012345678901234567890123456789
  178.              0         1         2         3         4         5 */
  179.     var threadOrDocId = "";
  180.     if (debug>=2) console.log('input hrefString is ' + hrefString);  
  181.     if (hrefString.substr(0, 8) == "/thread/")
  182.       {
  183.         threadOrDocId = hrefString.split("/")[2];
  184.         if (debug>=2) console.log('ASC threadOrDocId is  ' + threadOrDocId);
  185.       }
  186.     else if (hrefString.substr(0, 37) == "https://discussions.apple.com/thread/" )
  187.       {            
  188.         threadOrDocId = hrefString.split("?")[0].split("/")[4];
  189.         if (debug>=2) console.log('my altered threadOrDocId is  ' + threadOrDocId);    
  190.       }
  191.     // Check for doc id
  192.     else if (hrefString.substr(0, 6) == "/docs/" )
  193.       {
  194.         threadOrDocId = hrefString.split("/")[2]  // in this case it's doc id ;-)
  195.         if (debug>=2) console.log('my altered docId is  ' + threadOrDocId);  
  196.       }    
  197.     return threadOrDocId;
  198. }
  199.  
  200. // -----------------------------------------------------------------------------
  201. function stopWatchDogButton(dog) {
  202.     clearInterval(dog);
  203.     if (debug) console.log('Cleared interval for : ' + dog);
  204. }
  205.  
  206. // -----------------------------------------------------------------------------
  207. function zapPageInAnchor(passOption)
  208.   {
  209.     console.log("clicked.");
  210.     /* What was clicked? */
  211.    /* var actualValue = passedOption.options[passOption.selectedIndex].text;
  212.     if (debug) console.log("actualValue is " + actualValue); */
  213.    
  214.   }
  215. // -----------------------------------------------------------------------------
  216.  
  217. function insertLastPageAnchor()
  218.   {
  219.    var replyCounts = new Array();
  220.    var titleCounts = new Array();
  221.    var done = 0;
  222.    var lastPageStart = 0;
  223.    var constructedLink = "";
  224.    var insertA = "";
  225.    var rememberLastThread = "";
  226.    var debugStop = 0;
  227.    var entrySize = 0;  // 15 or 25 is the number of discussion listed on web page
  228.  
  229.     if (debug) console.log('\nInserting last page anchors');/**/
  230.    
  231.     try
  232.     {
  233.       $("table tr.js-browse-item td.j-td-replies").each(  
  234.         function( index )  
  235.           {  
  236.             replyCounts[index] = $( this ).text();
  237.            
  238.             if (debug>=2) console.log("reply index is " + index +
  239.                                       " for " + $( this ).text() );
  240.           }  
  241.        ); // End of each
  242.       }
  243.     catch(err)
  244.     {
  245.        console.log("within insertLastPageAnchor got error of " + err.message );
  246.        return;
  247.     }
  248.  
  249.     if (debug>=2) console.log("here...");
  250.     if (debug>=2) for ( i = 0;i<replyCounts.length;++i )
  251.       {
  252.         console.log("reply " + i + " is " + replyCounts[i] );
  253.       }
  254.  
  255.     if (debug) console.log("Find the titles");
  256.     var trSection = new Array();
  257.     trSection = $("table tr.js-browse-item td.j-td-title");
  258.     $("a:first", trSection ).each(  
  259.       function( index )  
  260.         {  
  261.           titleCounts[index] = $( this ).text();
  262.           /* Display Discussion Title */
  263.           if (debug) console.log("------  " + $( this ).html() + " ------");
  264.  
  265.           /* Open in new tab or page? */
  266.           if (howToOpen == "new")
  267.            {
  268.              $(this).attr("target","_blank");
  269.            }
  270.  
  271.           postNumber = $( this ).attr("href").split("/")[2].trim();
  272.           /* Need to decide for each entry because some lists are mixed */
  273.           if (postNumber.substr(0,3).toLowerCase() == "doc" )
  274.           {
  275.             entrySize = 25; // list of user tips
  276.           }
  277.           else
  278.           {
  279.             entrySize = 15; // list of threads
  280.           }
  281.           if (debug) console.log("a post number is " +
  282.                        postNumber +
  283.                        " entrySize is " +
  284.                        entrySize );
  285.           mathFloorInt = Math.floor(replyCounts[index]/entrySize);
  286.           if (debug) console.log ("repies = " + replyCounts[index] + " mathFloorInt is " +
  287.                        mathFloorInt );
  288.           /* Do we need to insert a pulldown? */
  289.           if (mathFloorInt)
  290.           {
  291.             lastPageStart = mathFloorInt * entrySize;
  292.             if (debug) console.log ("... lastPageStart is " + lastPageStart +
  293.                                     " mathFloorInt = " + mathFloorInt +
  294.                                     " entrySize is " + entrySize );
  295.             /*
  296.               https://discussions.apple.com/docs/DOC-11422
  297.             */
  298.             if (postNumber.toLowerCase().substr(0,3) == "doc")
  299.               {
  300.                 constructedLink = "https://discussions.apple.com/docs/" +
  301.                           postNumber +
  302.                           "?start=" +
  303.                           lastPageStart +
  304.                           "&begin=last";
  305.               }
  306.             else
  307.               {
  308.                 constructedLink = "https://discussions.apple.com/thread/" +
  309.                           postNumber +
  310.                           "?start=" +
  311.                           lastPageStart +
  312.                           "&tstart=0&begin=last";
  313.               }
  314.            
  315.              if (debug) console.log("link is " + constructedLink);
  316.              /* Make last link the default for the title link */        
  317.              $(this).attr("href",constructedLink);
  318.              
  319.  
  320.  
  321.            /*  How the select tag looks:
  322.                <select>
  323.                  <option value="volvo">Volvo</option>
  324.                  <option value="saab">Saab</option>
  325.                  <option value="mercedes">Mercedes</option>
  326.                </select>
  327.            */
  328.            /*
  329.  
  330.              https://discussions.apple.com/thread/7889277?
  331.              012345678901234567890123456789012345678901234567890123456789
  332.              0         1         2         3         4         5 */
  333.              insertA = '<select class="rcUserASCItemSelect"' +
  334.                        'onchange="var debug = 1;' +
  335.                        'if (debug) console.log(\' clicked.\');' +
  336.                        'var actualText = this.options[this.selectedIndex].text;' +
  337.                        'if (debug) console.log(\'actualText is \' + actualText);' +
  338.                        'var actualValue = this.options[this.selectedIndex].value;' +
  339.                        'if (debug) console.log(\'actualValue is \' + actualValue);' +
  340.                        'var searchString = actualValue.substr(0, 45);' +
  341.                        'if (debug) console.log(\'searchString is \' + searchString);' +
  342.                        'var doubleQuote = String.fromCharCode(34);' +
  343.                        'var searchFor = \'a[href^=\' + doubleQuote + searchString + doubleQuote + \']\';' +
  344.                        'if (debug) console.log(\'searchFor is \' + searchFor);' +
  345.                        'try{$(searchFor).attr(\'href\',actualValue);}catch(e){console.log(\'e is \'+e);}' +
  346.                        'if (debug) console.log(\'Update completed \');' +
  347.                        '">' +            
  348.                        '<option value="'+
  349.                        constructedLink   +
  350.                        '" selected>Last</option>'
  351.              
  352.              /* ansylis by induction
  353.                 68/15 is 4.53
  354.                 last should be 4
  355.                 pull down should be 3,2,1
  356.                 60/15 is 4
  357.                 pull down should be 3,2,1
  358.                 59 is 3.93
  359.                 pull down should be 2,1
  360.                 16/15 is 1.06
  361.                 pull down nothing in pull down ...
  362.                 15/15 is 1
  363.                 14/15 is 0.93
  364.               */
  365.              var iStart = Math.floor(lastPageStart/entrySize)
  366.              if (debug) console.log("iStart, number of pages, is " + iStart);
  367.              if (iStart > 1 ) {
  368.                /* the Last entry for highest page number */
  369.                iStart--
  370.                if (debug>=2) console.log("iStart is " + iStart);
  371.                for (var i = iStart ; i>0 ; i--)
  372.                  {
  373.                    pageStart = i * entrySize;
  374.                    if (postNumber.toLowerCase().substr(0,3) == "doc")
  375.                    {
  376.                      pageConstructedLink  = "https://discussions.apple.com/docs/" +
  377.                        postNumber +
  378.                        "?start=" +
  379.                        pageStart +
  380.                        "&begin="
  381.                    }
  382.                    else
  383.                    {
  384.                      pageConstructedLink  = "https://discussions.apple.com/thread/" +
  385.                        postNumber +
  386.                        "?start=" +
  387.                        pageStart +
  388.                        "&tstart=0&begin="
  389.                    }
  390.                        
  391.                        var hiddenString = '';
  392.                        var displayPage = '';                
  393.                        /* Hack out generic names for pages proceding the last page.
  394.                           This allows part#2 to fix the page position. */
  395.                        if (i == iStart && iStart > 4 )  // was 4
  396.                          {
  397.                             hiddenString = "Prior-1";
  398.                             displayPage =  "Prior-1";
  399.                          }
  400.                        else if ( i == (iStart-1) && iStart > 8 )
  401.                          {
  402.                             hiddenString = "Prior-2";
  403.                             displayPage =  "Prior-2";
  404.                          }
  405.                        else if (i == (iStart-2) && iStart > 12 )
  406.                          {                      
  407.                             hiddenString = "Prior-3";
  408.                             displayPage =  "Prior-3";
  409.                          }
  410.                        else
  411.                          {
  412.                             hiddenString = i.toString();
  413.                             displayPage = (i + 1).toString()
  414.                          }
  415.                    pageConstructedLink = pageConstructedLink + hiddenString ;  
  416.                    if (debug>=2) console.log("link for " +
  417.                        i.toString() + " is " +
  418.                        pageConstructedLink +
  419.                        " iStart is " +
  420.                        iStart );                                    
  421.                    if (debug>=2) console.log(                  
  422.                        "hiddenString is " +
  423.                        hiddenString +
  424.                        "displayPage is " +
  425.                        displayPage );
  426.                    insertA = insertA +
  427.                      '<option value="' +
  428.                      pageConstructedLink +
  429.                      '">' +
  430.                      displayPage +
  431.                      '</option>';
  432.                  }
  433.                }  // end of if
  434.             if (debug>=2) console.log("Add beginning " );
  435.             /* put in beginning option in case user what to go to the beginning of the display of posts */
  436.             pageStart = "0";
  437.             var displayPage = "1";
  438.             if (postNumber.toLowerCase().substr(0,3) == "doc")
  439.             {
  440.               pageConstructedLink  = "https://discussions.apple.com/docs/" +
  441.                 postNumber +
  442.                 "&begin=" +
  443.                 displayPage;
  444.             }
  445.             else
  446.             {
  447.               pageConstructedLink  = "https://discussions.apple.com/thread/" +
  448.                  postNumber +
  449.                 "?start=" +
  450.                 pageStart +
  451.                 "&tstart=0&begin=" +
  452.                 displayPage;
  453.             }
  454.            
  455.             if (debug) console.log("link for is " + pageConstructedLink);
  456.             /* Add Option */
  457.             insertA = insertA +
  458.                 '<option value="' +
  459.                 pageConstructedLink +
  460.                 '">' +
  461.                 "beginning" +
  462.                 '</option>';
  463.             insertA = insertA + "</select>"
  464.             if (debug>=2) console.log("inserted is " + insertA);
  465.             // Place in the document to insert selection.
  466.             try
  467.             {
  468.                $( this ).after( insertA );  
  469.             }            
  470.             catch(e)
  471.             {
  472.                 console.log('e is '+e);
  473.             }
  474.            
  475.           } // end of if (mathfloor)        
  476.         } // end of anonymous function
  477.      ); // end of .each
  478.  
  479.  
  480.   } // end of function insertLastPageAnchor
  481.  
  482. // Seems that javascript [ or someone ]
  483. // sends the last value from an assignment statement as the return code to the caller.
  484. done = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement