Advertisement
rccharles

Show all asc posts

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