2014-05-05 11:58:55 -07:00
// {"url_regex":"^https?://(?:www\\.)?facebook\\.com/.*$", "request_idle_timeout_sec":30}
//
2014-04-04 12:16:00 -07:00
// vim:set sw=8 et:
2014-05-05 11:58:55 -07:00
//
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 ) {
return 1 ; // below
} else {
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"]' ;
var umbraAlreadyClicked = { } ;
2014-05-05 18:39:16 -07:00
var umbraState = { 'idleSince' : null , 'expectingSomething' : null } ;
2014-04-04 12:16:00 -07:00
2014-05-05 11:58:55 -07:00
var umbraIntervalFunc = function ( ) {
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-03-07 19:37:43 -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-04-04 12:16:00 -07:00
2014-03-05 23:19:09 -05:00
if ( ! clickedSomething ) {
2014-05-05 18:39:16 -07:00
if ( somethingLeftAbove ) {
console . log ( "scrolling UP because everything on this screen has been clicked but we missed something above" ) ;
2014-06-04 12:34:20 -07:00
window . scrollBy ( 0 , - 500 ) ;
2014-05-05 18:39:16 -07:00
umbraState . idleSince = null ;
} else if ( somethingLeftBelow ) {
2014-03-05 23:19:09 -05:00
console . log ( "scrolling because everything on this screen has been clicked but there's more below document.body.clientHeight=" + document . body . clientHeight ) ;
2014-04-04 12:16:00 -07:00
window . scrollBy ( 0 , 200 ) ;
2014-05-05 11:58:55 -07:00
umbraState . idleSince = null ;
2014-05-05 18:39:16 -07:00
} else if ( window . scrollY + window . innerHeight < document . documentElement . scrollHeight ) {
2014-03-05 23:19:09 -05:00
console . log ( "scrolling because we're not to the bottom yet document.body.clientHeight=" + document . body . clientHeight ) ;
2014-04-04 12:16:00 -07:00
window . scrollBy ( 0 , 200 ) ;
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 ( ) {
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 ) ;