improve facebook behavior: when we expect a "close" button to appear, wait for it before moving on to other actions; and when we discover a missed click target above, scroll back up to click on it

This commit is contained in:
Noah Levitt 2014-05-05 18:39:16 -07:00
parent fa6e3eebb2
commit 93b16f28b9

View file

@ -17,25 +17,32 @@ var umbraAboveBelowOrOnScreen = function(e) {
// comments - 'a.UFIPagerLink > span, a.UFIPagerLink, span.UFIReplySocialSentenceLinkText' // comments - 'a.UFIPagerLink > span, a.UFIPagerLink, span.UFIReplySocialSentenceLinkText'
var UMBRA_THINGS_TO_CLICK_SELECTOR = 'a[href^="/browse/likes"], *[rel="theater"]'; var UMBRA_THINGS_TO_CLICK_SELECTOR = 'a[href^="/browse/likes"], *[rel="theater"]';
var umbraAlreadyClicked = {}; var umbraAlreadyClicked = {};
var umbraState = {'idleSince':null}; var umbraState = {'idleSince':null,'expectingSomething':null};
var umbraIntervalFunc = function() { var umbraIntervalFunc = function() {
var closeButton = document.querySelector('a[title="Close"]'); var closeButtons = document.querySelectorAll('a[title="Close"], a.closeTheater');
if (closeButton) { for (var i = 0; i < closeButtons.length; i++) {
console.log("clicking close button " + closeButton.outerHTML); // XXX closeTheater buttons stick around in the dom after closing, clientWidth>0 is one way to check if they're visible
closeButton.click(); 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; return;
} }
var closeTheaterButton = document.querySelector('a.closeTheater'); }
if (closeTheaterButton && closeTheaterButton.offsetWidth > 0) { if (umbraState.expectingSomething == 'closeButton') {
console.log("clicking close button " + closeTheaterButton.outerHTML); console.log("waiting for close button, haven't seen it yet");
closeTheaterButton.click();
return; return;
} }
var thingsToClick = document.querySelectorAll(UMBRA_THINGS_TO_CLICK_SELECTOR); var thingsToClick = document.querySelectorAll(UMBRA_THINGS_TO_CLICK_SELECTOR);
var clickedSomething = false; var clickedSomething = false;
var somethingLeftBelow = false; var somethingLeftBelow = false;
var somethingLeftAbove = false;
var missedAbove = 0; var missedAbove = 0;
for (var i = 0; i < thingsToClick.length; i++) { for (var i = 0; i < thingsToClick.length; i++) {
@ -47,6 +54,7 @@ var umbraIntervalFunc = function() {
// window.scrollTo(0, target.getBoundingClientRect().top - 100); // window.scrollTo(0, target.getBoundingClientRect().top - 100);
console.log("clicking at " + target.getBoundingClientRect().top + " on " + target.outerHTML); console.log("clicking at " + target.getBoundingClientRect().top + " on " + target.outerHTML);
if (target.click != undefined) { if (target.click != undefined) {
umbraState.expectingSomething = 'closeButton';
target.click(); target.click();
} }
target.style.border = '1px solid #0a0'; target.style.border = '1px solid #0a0';
@ -56,22 +64,22 @@ var umbraIntervalFunc = function() {
break; break;
} else if (where > 0) { } else if (where > 0) {
somethingLeftBelow = true; somethingLeftBelow = true;
} else { } else if (where < 0) {
missedAbove++; somethingLeftAbove = true;
} }
} }
} }
if (missedAbove > 0) {
console.log("somehow missed " + missedAbove + " click targets above");
}
if (!clickedSomething) { if (!clickedSomething) {
if (somethingLeftBelow) { if (somethingLeftAbove) {
console.log("scrolling UP because everything on this screen has been clicked but we missed something above");
window.scrollBy(0, -200);
umbraState.idleSince = null;
} else if (somethingLeftBelow) {
console.log("scrolling because everything on this screen has been clicked but there's more below document.body.clientHeight=" + document.body.clientHeight); console.log("scrolling because everything on this screen has been clicked but there's more below document.body.clientHeight=" + document.body.clientHeight);
window.scrollBy(0, 200); window.scrollBy(0, 200);
umbraState.idleSince = null; umbraState.idleSince = null;
} else if (window.scrollY + window.innerHeight + 10 < document.body.clientHeight) { } else if (window.scrollY + window.innerHeight < document.documentElement.scrollHeight) {
console.log("scrolling because we're not to the bottom yet document.body.clientHeight=" + document.body.clientHeight); console.log("scrolling because we're not to the bottom yet document.body.clientHeight=" + document.body.clientHeight);
window.scrollBy(0, 200); window.scrollBy(0, 200);
umbraState.idleSince = null; umbraState.idleSince = null;