Cleanup instagram timeout and state handling

This commit is contained in:
Adam Miller 2014-09-17 16:26:53 -07:00
parent 7afdd7b50b
commit 916f1b990e

View file

@ -2,77 +2,68 @@
// //
// vim:set sw=8 et: // vim:set sw=8 et:
// //
var UMBRA_USER_ACTION_IDLE_TIMEOUT_SEC = 10;
var umbraState = {'idleSince':null,'done':null}; var umbraState = {'idleSince':null,'expectingSomething':null,'done':false};
var umbraIntervalID = setInterval(umbraScrollInterval,50);
var umbraImages;
var umbraImageID=0;
var umbraImageCount=0;
var intervalID = setInterval(scrollInterval,50); function umbraScrollInterval() {
var images;
var imageID=0;
var imageCount=0;
function scrollInterval() {
scroll();
//if not at the bottom //if not at the bottom, keep scrolling
if(window.scrollY + window.innerHeight < document.documentElement.scrollHeight) { if(window.scrollY + window.innerHeight < document.documentElement.scrollHeight) {
umbraState.idleSince=Date.now(); window.scrollBy(0,50);
umbraState.expectingSomething=null;
umbraState.idleSince=null;
} }
else { else {
var more = document.querySelectorAll("span.more-photos a.more-photos-enabled"); var more = document.querySelectorAll("span.more-photos a.more-photos-enabled");
if(more.length>0) { if(more.length>0 && umbraState.expectingSomething==null) {
more[0].click(); more[0].click();
umbraState.expectingSomething="load more";
umbraState.idleSince=Date.now(); umbraState.idleSince=Date.now();
} }
else if(document.querySelectorAll("span.more-photos a.more-photos-disabled").length>0) { //finally done scrolling/loading else if(document.querySelectorAll("span.more-photos a.more-photos-disabled").length>0 || umbraTimeoutExpired() ) { //done scrolling/loading
clearInterval(intervalID); clearInterval(umbraIntervalID);
umbraState.idleSince=null; umbraImages = document.querySelectorAll("li.photo div.photo-wrapper a.bg[data-reactid]");
images = document.querySelectorAll("li.photo div.photo-wrapper a.bg[data-reactid]");
if(images && images !=='undefined' && images.length>0 ) { //click first image
images[0].click(); if(umbraImages && umbraImages !=='undefined' && umbraImages.length>0 ) {
imageID++; umbraImages[0].click();
imageCount=images.length; umbraImageID++;
umbraImageCount=umbraImages.length;
} }
intervalID = setInterval(clickPhotosInterval,200); intervalID = setInterval(umbraClickPhotosInterval,200);
} }
} }
} }
function clickPhotosInterval() { function umbraClickPhotosInterval() {
rightArrow = document.querySelectorAll("a.mmRightArrow"); rightArrow = document.querySelectorAll("a.mmRightArrow");
if(imageID>=imageCount) { if(umbraImageID>=umbraImageCount) {
clearInterval(intervalID); clearInterval(umbraIntervalID);
umbraState.done=true; umbraState.done=true
umbraState.idleSince=(Date.now()-50000);//ready to exit
} }
else { else {
rightArrow[0].click(); rightArrow[0].click();
imageID++; umbraImageID++;
umbraState.idleSince=Date.now();
} }
} }
function scroll() { function umbraTimeoutExpired () {
window.scrollBy(0,50);
}
// 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.done!=null && umbraState.done==true) {
return true;
}
if (umbraState.idleSince != null) { if (umbraState.idleSince != null) {
var idleTimeMs = Date.now() - umbraState.idleSince; var idleTimeMs = Date.now() - umbraState.idleSince;
if (idleTimeMs / 1000 > UMBRA_USER_ACTION_IDLE_TIMEOUT_SEC) { return (idleTimeMs/1000 > UMBRA_USER_ACTION_IDLE_TIMEOUT_SEC);
return true;
}
} }
return false; return false;
} }
// Called from outside of this script.
var umbraBehaviorFinished = function() {
return umbraState.done;
}