2014-04-04 12:16:00 -07:00
// vim:set sw=8 et:
2014-03-06 18:23:31 -08:00
2014-05-05 11:58:55 -07:00
var umbraAboveBelowOrOnScreen = function ( e ) {
2014-03-05 23:19:09 -05:00
var eTop = e . getBoundingClientRect ( ) . top ;
2014-04-04 12:16:00 -07:00
if ( eTop < window . scrollY ) {
return - 1 ; // above
} else if ( eTop > window . scrollY + window . innerHeight ) {
2014-08-01 16:53:13 -07:00
// if (e.clientWidth != 0) {
// console.warn("e.clientWidth=" + e.clientWidth + " though it appears to be below the screen? e.getBoundingClientRect().top=" + eTop + " window.scrollY=" + window.scrollY + " window.innerHeight=" + window.innerHeight + " e=" + e);
// }
2014-04-04 12:16:00 -07:00
return 1 ; // below
} else {
2014-08-01 16:53:13 -07:00
// if (e.clientWidth != 0) {
// console.warn("e.clientWidth=" + e.clientWidth + " though it appears to be on screen? e.getBoundingClientRect().top=" + eTop + " window.scrollY=" + window.scrollY + " window.innerHeight=" + window.innerHeight + " e=" + e);
// }
2014-04-04 12:16:00 -07:00
return 0 ; // on screen
}
}
2014-03-07 19:37:43 -08:00
// comments - 'a.UFIPagerLink > span, a.UFIPagerLink, span.UFIReplySocialSentenceLinkText'
2014-05-05 11:58:55 -07:00
var UMBRA _THINGS _TO _CLICK _SELECTOR = 'a[href^="/browse/likes"], *[rel="theater"]' ;
2014-12-19 14:17:50 -08:00
//div[class="phm pluginLikeboxStream"] = facebook widget embedded in 3rd party pages
var UMBRA _THINGS _TO _SCROLL _SELECTOR = 'div[class="phm pluginLikeboxStream"]' ;
var NUMBER _FAILED _SCROLL _ATTEMPTS _ON _THING _TO _SCROLL _BEFORE _STOP _SCROLLING = 5 ;
2014-05-05 11:58:55 -07:00
var umbraAlreadyClicked = { } ;
2014-12-19 14:17:50 -08:00
var umbraAlreadyScrolledThing = { } ;
var umbraScrolledThingFailedScrollAttempts = { } ;
2014-08-01 16:53:13 -07:00
var umbraState = { 'idleSince' : null , 'expectingSomething' : null , 'bottomReachedScrollY' : 0 } ;
2014-04-04 12:16:00 -07:00
2014-05-05 11:58:55 -07:00
var umbraIntervalFunc = function ( ) {
2014-12-19 14:17:50 -08:00
var thingsToScroll = document . querySelectorAll ( UMBRA _THINGS _TO _SCROLL _SELECTOR ) ;
2014-12-19 15:52:13 -08:00
var everythingScrolled = true ;
2014-12-19 14:17:50 -08:00
for ( var i = 0 ; i < thingsToScroll . length ; i ++ ) {
var target = thingsToScroll [ i ] ;
2014-12-19 15:52:13 -08:00
if ( ! ( target in umbraAlreadyScrolledThing ) ) {
2014-12-19 14:17:50 -08:00
2014-12-19 15:52:13 -08:00
everythingScrolled = false ;
console . log ( "scrolling to " + target . scrollHeight + " on element with nodeName " + target . nodeName + " with id of " + target . id ) ;
2014-12-19 14:17:50 -08:00
var lastScrollTop = target . scrollTop ;
target . scrollTop = target . scrollHeight ;
umbraState . idleSince = null ;
if ( target . scrollTop >= target . scrollHeight ) {
umbraAlreadyScrolledThing [ target ] = true ;
}
else if ( target . scrollTop == lastScrollTop ) {
if ( umbraScrolledThingFailedScrollAttempts [ target ] ) {
umbraScrolledThingFailedScrollAttempts [ target ] ++ ;
}
else {
umbraScrolledThingFailedScrollAttempts [ target ] = 1 ;
}
2014-12-19 15:13:02 -08:00
if ( umbraScrolledThingFailedScrollAttempts [ target ] >= NUMBER _FAILED _SCROLL _ATTEMPTS _ON _THING _TO _SCROLL _BEFORE _STOP _SCROLLING ) {
2014-12-19 14:17:50 -08:00
umbraAlreadyScrolledThing [ target ] = true ;
}
}
else {
//reset failed count on a successful scroll
umbraScrolledThingFailedScrollAttempts [ target ] = 0 ;
}
}
else {
2014-12-19 15:52:13 -08:00
console . log ( "done scrolling for element with nodeName " + target . nodeName + " with id of " + target . id )
2014-12-19 14:17:50 -08:00
}
2014-12-19 15:13:02 -08:00
umbraState . expectingSomething = null ;
2014-12-19 14:17:50 -08:00
}
2014-12-19 15:52:13 -08:00
if ( thingsToScroll && thingsToScroll . length > 0 && everythingScrolled ) {
if ( umbraState . idleSince == null ) {
umbraState . idleSince = Date . now ( ) ;
}
return ;
}
2014-12-19 14:17:50 -08:00
2014-05-05 18:39:16 -07:00
var closeButtons = document . querySelectorAll ( 'a[title="Close"], a.closeTheater' ) ;
for ( var i = 0 ; i < closeButtons . length ; i ++ ) {
// XXX closeTheater buttons stick around in the dom after closing, clientWidth>0 is one way to check if they're visible
if ( closeButtons [ i ] . clientWidth > 0 ) {
if ( umbraState . expectingSomething == 'closeButton' ) {
console . log ( "found expected close button, clicking on it " + closeButtons [ i ] . outerHTML ) ;
umbraState . expectingSomething = null ;
} else {
console . warn ( "found UNexpected close button, umbraState.expectingSomething=" + umbraState . expectingSomething + " ... clicking on it " + closeButtons [ i ] . outerHTML ) ;
}
closeButtons [ i ] . click ( ) ;
return ;
}
2014-03-05 23:19:09 -05:00
}
2014-05-05 18:39:16 -07:00
if ( umbraState . expectingSomething == 'closeButton' ) {
console . log ( "waiting for close button, haven't seen it yet" ) ;
2014-03-05 23:44:52 -05:00
return ;
}
2014-04-04 12:16:00 -07:00
2014-05-05 11:58:55 -07:00
var thingsToClick = document . querySelectorAll ( UMBRA _THINGS _TO _CLICK _SELECTOR ) ;
2014-03-05 23:19:09 -05:00
var clickedSomething = false ;
2014-04-04 12:16:00 -07:00
var somethingLeftBelow = false ;
2014-05-05 18:39:16 -07:00
var somethingLeftAbove = false ;
2014-04-04 12:16:00 -07:00
var missedAbove = 0 ;
2014-03-05 23:19:09 -05:00
for ( var i = 0 ; i < thingsToClick . length ; i ++ ) {
var target = thingsToClick [ i ] ;
2014-05-05 11:58:55 -07:00
if ( ! ( target in umbraAlreadyClicked ) ) {
var where = umbraAboveBelowOrOnScreen ( target ) ;
2014-04-04 12:16:00 -07:00
if ( where == 0 ) { // on screen
2014-03-05 23:19:09 -05:00
// var pos = target.getBoundingClientRect().top;
// window.scrollTo(0, target.getBoundingClientRect().top - 100);
2014-12-19 15:13:02 -08:00
console . log ( "clicking at " + target . getBoundingClientRect ( ) . top + " on " + target . outerHTML ) ;
2014-05-05 18:39:16 -07:00
if ( target . click != undefined ) {
umbraState . expectingSomething = 'closeButton' ;
2014-04-04 12:16:00 -07:00
target . click ( ) ;
2014-03-10 14:58:16 -04:00
}
2014-03-05 23:19:09 -05:00
target . style . border = '1px solid #0a0' ;
2014-05-05 11:58:55 -07:00
umbraAlreadyClicked [ target ] = true ;
2014-03-05 23:19:09 -05:00
clickedSomething = true ;
2014-05-05 11:58:55 -07:00
umbraState . idleSince = null ;
2014-03-05 23:19:09 -05:00
break ;
2014-04-04 12:16:00 -07:00
} else if ( where > 0 ) {
somethingLeftBelow = true ;
2014-05-05 18:39:16 -07:00
} else if ( where < 0 ) {
somethingLeftAbove = true ;
2014-03-05 23:19:09 -05:00
}
}
}
2014-12-19 14:17:50 -08:00
2014-08-01 16:53:13 -07:00
if ( window . scrollY > umbraState . bottomReachedScrollY ) {
umbraState . bottomReachedScrollY = window . scrollY ;
}
2014-03-05 23:19:09 -05:00
if ( ! clickedSomething ) {
2014-08-01 16:53:13 -07:00
if ( somethingLeftBelow ) {
// console.log("scrolling down because everything on this screen has been clicked but there's more below document.body.clientHeight=" + document.body.clientHeight);
window . scrollBy ( 0 , 300 ) ;
2014-05-05 18:39:16 -07:00
umbraState . idleSince = null ;
2014-08-01 16:53:13 -07:00
} else if ( umbraState . bottomReachedScrollY + window . innerHeight < document . documentElement . scrollHeight ) {
// console.log("scrolling down because we haven't reached the bottom yet document.body.clientHeight=" + document.body.clientHeight);
window . scrollBy ( 0 , 300 ) ;
2014-05-05 11:58:55 -07:00
umbraState . idleSince = null ;
2014-08-01 16:53:13 -07:00
} else if ( somethingLeftAbove ) {
// console.log("scrolling UP because we've already been to the bottom, everything on or below this screen has been clicked, but we missed something above");
window . scrollBy ( 0 , - 600 ) ;
2014-05-05 11:58:55 -07:00
umbraState . idleSince = null ;
} else if ( umbraState . idleSince == null ) {
umbraState . idleSince = Date . now ( ) ;
}
2014-03-05 23:19:09 -05:00
}
}
2014-04-04 12:16:00 -07:00
2014-05-05 11:58:55 -07:00
// If we haven't had anything to do (scrolled, clicked, etc) in this amount of
// time, then we consider ourselves finished with the page.
var UMBRA _USER _ACTION _IDLE _TIMEOUT _SEC = 10 ;
// Called from outside of this script.
var umbraBehaviorFinished = function ( ) {
2014-12-19 15:13:02 -08:00
2014-05-05 11:58:55 -07:00
if ( umbraState . idleSince != null ) {
var idleTimeMs = Date . now ( ) - umbraState . idleSince ;
if ( idleTimeMs / 1000 > UMBRA _USER _ACTION _IDLE _TIMEOUT _SEC ) {
return true ;
}
}
return false ;
}
var umbraIntervalId = setInterval ( umbraIntervalFunc , 200 ) ;