Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ASCClickerV2 25Dec2018
- Show all posts. When present on the ASC page, this program clicks on "All replies", "Helpful answers" and "Read all replies"
- Valid URL's
- https://discussions.apple.com/thread/8654416
- https://discussions.apple.com/thread/250043017
- https://discussions.apple.com/thread/250040101
- problem... not all post shown tt2's changes are in effect
- https://discussions.apple.com/thread/250044892
- All replies
- https://discussions.apple.com/thread/250038745
- Helpful answers & Read all replies
- https://discussions.apple.com/thread/250038732
- interesting
- https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
- from Greasemonkey hacks: tips & Tools for Remixing the web with Firefox
- by Julien Couvreur
- Code from book with his gracious permission
- Javascript: The Definitive Guide 6th Edition
- $() is a Jquery. See Chapter 19.
- How to get $() to work again.
- https://wiki.greasespot.net/Third-Party_Libraries
- */
- // ==UserScript==
- // @name ASCClickerV2
- // @namespace bubo-bubo/gmscripts
- // @description Show all posts in a thread.
- // @include /https://discussions\.apple\.com/+thread/+.*/
- // @version 25Dec2018
- // @grant none
- // ==/UserScript==
- // Uses Hiroto's event handler design.
- // var $ = unsafeWindow.jQuery;
- var watchdog1, watchdog2;
- var watchInterval = 500; // [ms] Waiting for the user to change page
- var shortWatchInterval = 100; // [ms] Waiting for the system to load new list
- var debug = 2; // 0 -- no debug
- // 1 -- normal debug
- // 2 -- intense debug
- var aDate = new Date();
- console.log ("==++> ASCClicker shows all posts setup. " + aDate);
- /* https://stackoverflow.com/questions/2194992/jquery-is-not-defined
- */
- // Setup once page is loaded.
- // modify thread content view behaviour (in post-load phase)
- /* Most likely will be interactive.
- The script will run after the main page is loaded, but before other resources
- (images, style sheets, etc.) have loaded.
- https://wiki.greasespot.net/Metadata_Block#.40run-at */
- if (debug) console.log("document.readyState is " + document.readyState);
- // register event listeners
- // nothing like starting at the end ;-)
- window.addEventListener('unload', function(e)
- {
- if (debug) console.log('unloading.');
- stopWatchDogButton(watchdog1);
- stopWatchDogButton(watchdog2);
- window.removeEventListener('locationChangeButton',
- locationChangeButtonHandler,
- true);
- window.removeEventListener('waitingInsertLastPageAnchor',
- doClickHandler,
- true);
- if (debug) console.log('removing e.type is '+ e.type + " arg is " +
- arguments.callee);
- // remove anonymous unload. [ Thus unload ]
- window.removeEventListener(e.type,
- arguments.callee,
- true);
- }, true);
- // more stuff for later
- if (debug>=2) console.log('adding locationChangeButton event listner');
- window.addEventListener('locationChangeButton',
- locationChangeButtonHandler,
- true);
- window.addEventListener('waitingInsertLastPageAnchor',
- doClickHandler,
- true);
- // Engage a watch dog
- var prev_href = '';
- if (debug) console.log('prev_href is ' + prev_href );
- // Spin around here watching for the web address to change.
- // Always spinning here.
- // It takes awhile after the page address changes before the list is loaded
- // so we don't need to check that often.
- // cannot figure out why the web address will change. Changes in greasemonkey?
- // anyway we bounce to event locationChangeButton hence locationChangeButtonHandler(e)
- // note: setInterval runs until stopped.
- watchdog1 = setInterval( function()
- {
- var curr_href = window.location.href;
- if (debug >= 2) console.log(' prev_href is ' + prev_href +
- "\n curr_href is " + curr_href);
- if (curr_href != prev_href)
- {
- // Page changed
- if (debug) console.log('======> found new page. ' +
- ' curr_href is ' + curr_href +
- ' <======');
- window.dispatchEvent(new Event('locationChangeButton'));
- prev_href = curr_href;
- if (debug) console.log("stopping. watchdog1 is " + watchdog1)
- stopWatchDogButton(watchdog1);
- }
- },
- watchInterval ); // end of setting watch dog #1
- // last set value is returned as return code.
- var done;
- done = 0;
- // -----------------------------------------------------------------------------
- function locationChangeButtonHandler(e)
- {
- if (debug) console.log('---> locationChangeButtonHandler is observed. '+
- "Debug is " + debug);
- if (debug) console.log(" 1 document.readyState is " + document.readyState);
- // Give a little time for the page to settle down.
- // Spin around here watching for the web address in the last entry to change.
- // Stop spinning once change is found
- var spinCount = 1;
- watchdog2 = setInterval( function()
- {
- console.log(" 2 document.readyState is " + document.readyState);
- console.log ("debug is " + debug);
- try {
- if (document.readyState = "complete") {
- console.log ("document.readyState is complete...");
- sectionSpin = document.querySelector(
- "body div.helpful-all-toggler div.drop-down-header-wrapper");
- console.log ("using querySelector");
- spew(sectionSpin);
- if (sectionSpin) {
- window.dispatchEvent(new Event('waitingInsertLastPageAnchor') );
- stopWatchDogButton(watchdog2);
- }
- } // if complete
- } // end try
- catch(err) {
- console.log ("no luck on " + spinCount + " with error \""
- + err.message + "\"");
- }
- // Let's not run forever
- spinCount++;
- if ( spinCount > 100 ){
- stopWatchDogButton(watchdog2);
- console.log ("---> Out of here after no luck on " + spinCount );
- }
- // stopWatchDogButton(watchdog2);
- }, // end of imbeded funtion
- shortWatchInterval ); // end of setting watchdog2
- if (debug) console.log("end of locationChangeButtonHandler");
- } // end of locationChangeButtonHandler
- // -----------------------------------------------------------------------------
- function doClickHandler () {
- if (debug>=2) console.log("in onlickerHandler");
- processPage();
- }
- /* --------------------------------------------------------------------- */
- /* main line code */
- function processPage () {
- if (debug) console.log ("--> ASCClicker shows all posts. " + aDate);
- try
- {
- // Check on the display status of Helpful answers
- var helpfulAnswersPresent = document.querySelector(
- "body div.helpful-all-toggler:not(.hidden) div.drop-down-header-wrapper")
- spew(helpfulAnswersPresent)
- // Is Helpful answers displayed?>
- if (helpfulAnswersPresent ) {
- if (debug) console.log ("Helpful answers only:-(. All should be displayed ")
- // Helpful answer message is display. Thus, only the helpful posts are displayed.
- // Display all posts ... post haste
- clickAnchorTag(helpfulAnswersPresent) // display the pulldown
- // click on All replies
- var helpfulAnswersAllreplies = document.querySelector(
- "body ul.drop-down-menu.large.center li.menu-item+li.menu-item a.menu-item-link")
- spew(helpfulAnswersAllreplies)
- clickAnchorTag(helpfulAnswersAllreplies)
- if (debug) console.log ("All nswers are being displayed :-(")
- } // end if
- } // end of try
- catch(err)
- {
- console.log ("No Helpful answers. Error is \""+ err.message + "\"" )
- }
- if (debug) console.log ("almost done." )
- } // end of processPage
- /* --------------------------------------------------------------------- */
- /* print out objects:
- https://stackoverflow.com/questions/957537/how-can-i-display-a-javascript-object
- https://code-maven.com/logging-javascript-objects
- */
- function spew (inputObj) {
- if (debug) {
- console.log ("inputObj is: ")
- console.log ( inputObj)
- }
- if (debug>=2) {
- console.log ("typeof inputObj is " + typeof inputObj)
- //this will show object gandalf IN ALL BROWSERS!
- console.log(JSON.stringify(inputObj));
- //this will show object gandalf IN ALL BROWSERS! with beautiful indent
- console.log(JSON.stringify(inputObj, null, 4));
- console.log(Object.keys(inputObj));
- console.log(Object.values(inputObj));
- //There is also this new option if you're using ECMAScript 2016 or newer:
- try {
- Object.keys(inputObj).forEach(e => console.log(`key=${e} value=${inputObj[e]}`));
- }
- catch (err) {
- console.log (" You must need ECMAScript 2016 or newer \""+ err.message + "\"" )
- }
- // alert (inputObj)
- //var object = this.window;
- //console.log(object,'this is window object');
- } // end of if debug
- } // end of function spew
- /* --------------------------------------------------------------------- */
- /* based on:
- https://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript
- */
- function clickAnchorTag(inputObj) {
- spew(inputObj)
- var event = document.createEvent('MouseEvent');
- if (debug>=2) console.log ("proceeding to set new event")
- event = new CustomEvent('click');
- if (debug) console.log ("clicking...")
- inputObj.dispatchEvent(event);
- }
- /* original version
- function clickAnchorTag() {
- var event = document.createEvent('MouseEvent');
- event = new CustomEvent('click');
- var a = document.getElementById('nameOfID');
- a.dispatchEvent(event);
- }
- */
- /* --------------------------------------------------------------------- */
- function stopWatchDogButton(dog) {
- if (debug>=2) console.log("In stopWatchDogButton " + dog)
- clearInterval(dog);
- if (debug>=2) console.log('watchdog is inactive : ' + dog);
- }
Add Comment
Please, Sign In to add comment