2015-09-24 12:34:55 -07:00
|
|
|
var umbraBehavior = {
|
2015-09-15 11:53:53 -07:00
|
|
|
IDLE_TIMEOUT_SEC : 10,
|
|
|
|
idleSince : null,
|
|
|
|
alreadyClicked : {},
|
|
|
|
|
|
|
|
intervalFunc : function() {
|
|
|
|
var clickedSomething = false;
|
|
|
|
var somethingLeftBelow = false;
|
|
|
|
var somethingLeftAbove = false;
|
|
|
|
var cssSelector = "${click_css_selector}";
|
2015-09-14 17:57:01 -07:00
|
|
|
|
|
|
|
var iframes = document.querySelectorAll("iframe");
|
|
|
|
var documents = Array(iframes.length + 1);
|
|
|
|
documents[0] = document;
|
2015-09-15 11:53:53 -07:00
|
|
|
|
2015-09-14 17:57:01 -07:00
|
|
|
for (var i = 0; i < iframes.length; i++) {
|
|
|
|
documents[i+1] = iframes[i].contentWindow.document;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var j = 0; j < documents.length; j++) {
|
2015-09-15 11:53:53 -07:00
|
|
|
|
2015-09-15 18:03:08 -07:00
|
|
|
var clickTargets = documents[j].querySelectorAll(cssSelector);
|
2015-09-15 11:53:53 -07:00
|
|
|
|
|
|
|
for ( var i = 0; i < clickTargets.length; i++) {
|
2015-09-17 16:24:41 -07:00
|
|
|
if (clickTargets[i].umbraClicked) {
|
2015-09-15 11:53:53 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-24 12:34:55 -07:00
|
|
|
var where = this.aboveBelowOrOnScreen(clickTargets[i]);
|
2015-09-15 11:53:53 -07:00
|
|
|
|
|
|
|
if (where == 0) {
|
2015-09-15 18:03:08 -07:00
|
|
|
console.log("clicking on " + clickTargets[i].outerHTML);
|
|
|
|
// do mouse over event on click target
|
|
|
|
// since some urls are requsted only on
|
|
|
|
// this event - see
|
|
|
|
// https://webarchive.jira.com/browse/AITFIVE-451
|
|
|
|
var mouseOverEvent = document.createEvent('Events');
|
|
|
|
mouseOverEvent.initEvent("mouseover",true, false);
|
|
|
|
clickTargets[i].dispatchEvent(mouseOverEvent);
|
|
|
|
clickTargets[i].click();
|
|
|
|
clickedSomething = true;
|
|
|
|
this.idleSince = null;
|
|
|
|
clickTargets[i].umbraClicked = true;
|
|
|
|
|
|
|
|
break; //break from clickTargets loop, but not from iframe loop
|
2015-09-15 11:53:53 -07:00
|
|
|
} else if (where > 0) {
|
|
|
|
somethingLeftBelow = true;
|
|
|
|
} else if (where < 0) {
|
|
|
|
somethingLeftAbove = true;
|
2015-09-14 17:57:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 11:53:53 -07:00
|
|
|
if (!clickedSomething) {
|
|
|
|
if (somethingLeftAbove) {
|
2015-09-24 12:34:55 -07:00
|
|
|
// console.log("scrolling UP because everything on this screen has been clicked but we missed something above");
|
2015-09-15 11:53:53 -07:00
|
|
|
window.scrollBy(0, -500);
|
|
|
|
this.idleSince = null;
|
|
|
|
} else if (somethingLeftBelow) {
|
2015-09-24 12:34:55 -07:00
|
|
|
// console.log("scrolling because everything on this screen has been clicked but there's more below document.body.clientHeight="
|
|
|
|
// + document.body.clientHeight);
|
2015-09-15 11:53:53 -07:00
|
|
|
window.scrollBy(0, 200);
|
|
|
|
this.idleSince = null;
|
|
|
|
} else if (window.scrollY + window.innerHeight < document.documentElement.scrollHeight) {
|
2015-09-24 12:34:55 -07:00
|
|
|
// console.log("scrolling because we're not to the bottom yet document.body.clientHeight="
|
|
|
|
// + document.body.clientHeight);
|
2015-09-15 11:53:53 -07:00
|
|
|
window.scrollBy(0, 200);
|
|
|
|
this.idleSince = null;
|
|
|
|
} else if (this.idleSince == null) {
|
|
|
|
this.idleSince = Date.now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 17:57:01 -07:00
|
|
|
if (!this.idleSince) {
|
|
|
|
this.idleSince = Date.now();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-09-15 11:53:53 -07:00
|
|
|
start : function() {
|
2015-09-14 17:57:01 -07:00
|
|
|
var that = this;
|
2015-09-15 11:53:53 -07:00
|
|
|
this.intervalId = setInterval(function() {
|
|
|
|
that.intervalFunc()
|
|
|
|
}, 250);
|
2015-09-14 17:57:01 -07:00
|
|
|
},
|
|
|
|
|
2015-09-15 11:53:53 -07:00
|
|
|
isFinished : function() {
|
2015-09-14 17:57:01 -07:00
|
|
|
if (this.idleSince != null) {
|
|
|
|
var idleTimeMs = Date.now() - this.idleSince;
|
|
|
|
if (idleTimeMs / 1000 > this.IDLE_TIMEOUT_SEC) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2015-09-24 12:34:55 -07:00
|
|
|
|
|
|
|
aboveBelowOrOnScreen : function(e) {
|
|
|
|
var eTop = e.getBoundingClientRect().top;
|
|
|
|
if (eTop < window.scrollY) {
|
|
|
|
return -1; // above
|
|
|
|
} else if (eTop > window.scrollY + window.innerHeight) {
|
|
|
|
return 1; // below
|
|
|
|
} else {
|
|
|
|
return 0; // on screen
|
|
|
|
}
|
|
|
|
},
|
2015-01-26 16:58:12 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Called from outside of this script.
|
2015-09-15 11:53:53 -07:00
|
|
|
var umbraBehaviorFinished = function() {
|
2015-09-24 12:34:55 -07:00
|
|
|
return umbraBehavior.isFinished()
|
2015-09-15 11:53:53 -07:00
|
|
|
};
|
2015-01-26 16:58:12 -08:00
|
|
|
|
2015-09-24 12:34:55 -07:00
|
|
|
umbraBehavior.start();
|