Advertisement
rccharles

ASC LAST page adjustment Part 1 ver 2

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