rccharles

Show all the asc posts

Jan 16th, 2019
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.    ASCClickerV2 25Dec2018
  3.  
  4.    Show all posts.  When present on the ASC page, this program clicks on "All replies", "Helpful answers" and "Read all replies"
  5.  
  6.    
  7.    Valid URL's
  8.        https://discussions.apple.com/thread/8654416
  9.        https://discussions.apple.com/thread/250043017
  10.        https://discussions.apple.com/thread/250040101
  11.        problem... not all post shown tt2's changes are in effect
  12.        https://discussions.apple.com/thread/250044892
  13.      All replies
  14.        https://discussions.apple.com/thread/250038745
  15.      Helpful answers & Read all replies
  16.        https://discussions.apple.com/thread/250038732
  17.  
  18.     interesting
  19.       https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
  20.    
  21.     from Greasemonkey hacks: tips & Tools for Remixing the web with Firefox
  22.     by Julien Couvreur
  23.     Code from book with his gracious permission
  24.  
  25.     Javascript: The Definitive Guide 6th Edition
  26.     $() is a Jquery.  See Chapter 19.
  27.    
  28.     How to get $() to work again.
  29.     https://wiki.greasespot.net/Third-Party_Libraries
  30.  
  31. */
  32.  
  33. // ==UserScript==
  34. // @name        ASCClickerV2
  35. // @namespace   bubo-bubo/gmscripts
  36. // @description Show all posts in a thread.
  37. // @include     /https://discussions\.apple\.com/+thread/+.*/
  38. // @version     25Dec2018
  39. // @grant       none
  40. // ==/UserScript==
  41.  
  42.  
  43. // Uses Hiroto's event handler design.
  44. // var $ = unsafeWindow.jQuery;
  45. var watchdog1, watchdog2;
  46. var watchInterval           = 500; // [ms] Waiting for the user to change page
  47. var shortWatchInterval      = 100; // [ms] Waiting for the system to load new list
  48. var debug                   = 2;   // 0 -- no debug
  49.                                    // 1 -- normal debug
  50.                                    // 2 -- intense debug
  51.  
  52. var aDate = new Date();
  53. console.log ("==++> ASCClicker shows all posts setup. " + aDate);
  54.  
  55. /* https://stackoverflow.com/questions/2194992/jquery-is-not-defined
  56.  */
  57.  
  58. // Setup once page is loaded.
  59. // modify thread content view behaviour (in post-load phase)
  60.  
  61. /* Most likely will be interactive.
  62.     The script will run after the main page is loaded, but before other resources
  63.     (images, style sheets, etc.) have loaded.
  64.     https://wiki.greasespot.net/Metadata_Block#.40run-at  */
  65. if (debug) console.log("document.readyState is " + document.readyState);
  66.  
  67. // register event listeners
  68. // nothing like starting at the end ;-)
  69.  window.addEventListener('unload', function(e)
  70.         {
  71.           if (debug) console.log('unloading.');
  72.           stopWatchDogButton(watchdog1);
  73.           stopWatchDogButton(watchdog2);
  74.          
  75.           window.removeEventListener('locationChangeButton',
  76.                                       locationChangeButtonHandler,
  77.                                       true);
  78.           window.removeEventListener('waitingInsertLastPageAnchor',
  79.                                       doClickHandler,
  80.                                       true);
  81.           if (debug) console.log('removing e.type is '+ e.type + " arg is " +
  82.                                   arguments.callee);
  83.           // remove anonymous unload. [ Thus unload ]
  84.           window.removeEventListener(e.type,
  85.                                      arguments.callee,
  86.                                      true);
  87.         }, true);
  88.  
  89. // more stuff for later
  90.  if (debug>=2)  console.log('adding locationChangeButton event listner');
  91.  window.addEventListener('locationChangeButton',
  92.                          locationChangeButtonHandler,
  93.                          true);
  94.  window.addEventListener('waitingInsertLastPageAnchor',
  95.                           doClickHandler,
  96.                          true);
  97.  // Engage a watch dog
  98.  var prev_href = '';
  99.  if (debug)  console.log('prev_href is ' + prev_href );
  100.  
  101.  // Spin around here watching for the web address to change.
  102.  // Always spinning here.
  103.  // It takes awhile after the page address changes before the list is loaded
  104.  // so we don't need to check that often.
  105.  
  106.  // cannot figure out why the web address will change. Changes in greasemonkey?
  107.  // anyway we bounce to event locationChangeButton hence locationChangeButtonHandler(e)
  108.  // note: setInterval runs until stopped.
  109.  watchdog1 = setInterval( function()
  110.         {
  111.           var curr_href = window.location.href;
  112.           if (debug >= 2) console.log('   prev_href is ' + prev_href +
  113.                                  "\n     curr_href is " + curr_href);
  114.           if (curr_href != prev_href)
  115.             {
  116.               // Page changed
  117.               if (debug) console.log('======> found new page. ' +
  118.                                       '  curr_href is ' + curr_href +
  119.                                       ' <======');
  120.               window.dispatchEvent(new Event('locationChangeButton'));
  121.               prev_href = curr_href;
  122.               if (debug) console.log("stopping. watchdog1 is " + watchdog1)
  123.               stopWatchDogButton(watchdog1);
  124.             }
  125.            
  126.          },
  127.          watchInterval ); // end of setting watch dog #1
  128.  
  129. // last set value is returned as return code.
  130. var done;
  131. done = 0;
  132.  
  133. // -----------------------------------------------------------------------------
  134. function locationChangeButtonHandler(e)
  135.        {
  136.          if (debug)  console.log('---> locationChangeButtonHandler is observed. '+
  137.                                  "Debug is  " + debug);  
  138.          if (debug)  console.log(" 1 document.readyState is " + document.readyState);
  139.          // Give a little time for the page to settle down.
  140.          // Spin around here watching for the web address in the last entry to change.
  141.          // Stop spinning once change is found
  142.          var spinCount = 1;
  143.          watchdog2 = setInterval( function()
  144.                 {    
  145.                   console.log(" 2 document.readyState is " + document.readyState);
  146.                   console.log ("debug is " + debug);
  147.  
  148.                   try {                  
  149.                    
  150.                     if (document.readyState = "complete") {
  151.                        console.log ("document.readyState is complete...");
  152.                        sectionSpin  = document.querySelector(
  153.                                       "body div.helpful-all-toggler div.drop-down-header-wrapper");
  154.                        console.log ("using querySelector");
  155.                        spew(sectionSpin);
  156.                       if (sectionSpin) {
  157.                         window.dispatchEvent(new Event('waitingInsertLastPageAnchor') );
  158.                         stopWatchDogButton(watchdog2);
  159.                       }
  160.                     }  // if complete
  161.                   }  // end try
  162.                   catch(err) {
  163.                     console.log ("no luck on " + spinCount + " with error \""
  164.                                 + err.message + "\"");
  165.                   }
  166.                   // Let's not run forever
  167.                   spinCount++;
  168.                   if ( spinCount > 100 ){
  169.                     stopWatchDogButton(watchdog2);
  170.                     console.log ("---> Out of here after no luck on " + spinCount );
  171.                   }
  172.                       // stopWatchDogButton(watchdog2);
  173.                 }, // end of imbeded funtion
  174.                 shortWatchInterval ); // end of setting watchdog2
  175.          
  176.                 if (debug) console.log("end of locationChangeButtonHandler");
  177.         }  // end of locationChangeButtonHandler
  178.  
  179. // -----------------------------------------------------------------------------
  180. function doClickHandler () {
  181. if (debug>=2)  console.log("in onlickerHandler");
  182. processPage();
  183. }
  184. /* --------------------------------------------------------------------- */
  185. /* main line code */
  186. function processPage () {
  187. if (debug) console.log ("--> ASCClicker shows all posts. " + aDate);
  188. try
  189.   {
  190.       // Check on the display status of Helpful answers
  191.       var helpfulAnswersPresent = document.querySelector(
  192.                           "body div.helpful-all-toggler:not(.hidden) div.drop-down-header-wrapper")
  193.       spew(helpfulAnswersPresent)
  194.       // Is Helpful answers displayed?>
  195.       if (helpfulAnswersPresent ) {
  196.         if (debug) console.log ("Helpful answers only:-(. All should be displayed ")
  197.         // Helpful answer message is display.  Thus, only the helpful posts are displayed.
  198.         // Display all posts ... post haste
  199.         clickAnchorTag(helpfulAnswersPresent)   // display the pulldown
  200.  
  201.         // click on All replies
  202.         var helpfulAnswersAllreplies = document.querySelector(
  203.                           "body ul.drop-down-menu.large.center li.menu-item+li.menu-item a.menu-item-link")
  204.         spew(helpfulAnswersAllreplies)
  205.         clickAnchorTag(helpfulAnswersAllreplies)
  206.         if (debug) console.log ("All nswers are being displayed :-(")
  207.       }  // end if
  208.   } // end of try
  209. catch(err)
  210.   {
  211.     console.log ("No Helpful answers.  Error is \""+ err.message + "\"" )
  212.   }
  213.  
  214. if (debug) console.log ("almost done." )
  215. }  // end of processPage
  216.  
  217. /* --------------------------------------------------------------------- */
  218. /* print out objects:
  219.       https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object
  220.       https://code-maven.com/logging-javascript-objects
  221. */
  222. function spew (inputObj) {
  223.   if (debug) {
  224.     console.log ("inputObj is: ")
  225.     console.log ( inputObj)
  226.   }
  227.   if (debug>=2) {
  228.     console.log ("typeof inputObj is " + typeof inputObj)
  229.     //this will show object gandalf IN ALL BROWSERS!
  230.     console.log(JSON.stringify(inputObj));
  231.     //this will show object gandalf IN ALL BROWSERS! with beautiful indent
  232.     console.log(JSON.stringify(inputObj, null, 4));
  233.     console.log(Object.keys(inputObj));
  234.     console.log(Object.values(inputObj));
  235.  
  236.     //There is also this new option if you're using ECMAScript 2016 or newer:
  237.     try {
  238.       Object.keys(inputObj).forEach(e => console.log(`key=${e}  value=${inputObj[e]}`));
  239.     }
  240.     catch (err) {
  241.       console.log (" You must need ECMAScript 2016 or newer \""+ err.message + "\"" )
  242.     }
  243.     // alert (inputObj)
  244.     //var object = this.window;
  245.     //console.log(object,'this is window object');
  246.   }  // end of if debug
  247. } // end of function spew
  248.  
  249. /* --------------------------------------------------------------------- */
  250. /* based on:
  251.      https://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript
  252. */
  253. function clickAnchorTag(inputObj) {
  254.     spew(inputObj)
  255.     var event = document.createEvent('MouseEvent');
  256.     if (debug>=2) console.log ("proceeding to set new event")
  257.     event = new CustomEvent('click');
  258.     if (debug) console.log ("clicking...")
  259.     inputObj.dispatchEvent(event);
  260. }
  261.  
  262.  
  263. /* original version
  264.  
  265. function clickAnchorTag() {
  266.     var event = document.createEvent('MouseEvent');
  267.     event = new CustomEvent('click');
  268.     var a = document.getElementById('nameOfID');
  269.     a.dispatchEvent(event);
  270. }
  271. */
  272.  
  273. /* --------------------------------------------------------------------- */
  274.  
  275. function stopWatchDogButton(dog) {
  276.     if (debug>=2) console.log("In stopWatchDogButton " + dog)
  277.     clearInterval(dog);
  278.     if (debug>=2) console.log('watchdog is inactive : ' + dog);
  279. }
Add Comment
Please, Sign In to add comment