Merge pull request #27 from internetarchive/i2

update Instagram behavior, mostly css selectors
This commit is contained in:
Noah Levitt 2016-11-14 12:40:55 -08:00 committed by GitHub
commit 2b0a47c914

View File

@ -33,7 +33,7 @@ var umbraInstagramBehavior = {
return; return;
} }
var moreButtons = document.querySelectorAll(".PhotoGridMoreButton:not(.pgmbDisabled)"); var moreButtons = document.querySelectorAll("a._oidfu");
if (moreButtons.length > 0) { if (moreButtons.length > 0) {
console.log("clicking load more button"); console.log("clicking load more button");
moreButtons[0].click(); moreButtons[0].click();
@ -41,26 +41,22 @@ var umbraInstagramBehavior = {
return; return;
} }
if (this.idleSince == null) { if (this.idleSince === null) {
console.log("nothing to do at the moment, might be waiting for something to load, setting this.idleSince=Date.now()"); console.log("nothing to do at the moment, might be waiting for something to load, setting this.idleSince=Date.now()");
this.idleSince = Date.now(); this.idleSince = Date.now();
return; return;
} else { } else {
var doneButtons = document.querySelectorAll(".PhotoGridMoreButton.pgmbDisabled"); if ((Date.now() - this.idleSince) > 9000) {
if (Date.now() - this.idleSince > 9000 || (doneButtons.length > 0 && doneButtons[0].innerText === "All items loaded") ) {
console.log("finished loading-thumbs, it appears we have reached the bottom"); console.log("finished loading-thumbs, it appears we have reached the bottom");
this.state = "clicking-first-thumb"; this.state = "clicking-first-thumb";
this.idleSince = null; this.idleSince = null;
return;
} else {
// console.log("still might be waiting for something to load...");
return;
} }
return;
} }
} }
if (this.state === "clicking-first-thumb") { if (this.state === "clicking-first-thumb") {
var images = document.querySelectorAll("a.pgmiImageLink"); var images = document.querySelectorAll("div._ovg3g");
if (images && images !== "undefined") { if (images && images !== "undefined") {
this.imageCount = images.length; this.imageCount = images.length;
if (images.length > 0) { if (images.length > 0) {
@ -68,6 +64,7 @@ var umbraInstagramBehavior = {
images[0].click(); images[0].click();
this.idleSince = null; this.idleSince = null;
this.state = "waiting-big-image"; this.state = "waiting-big-image";
this.bigImagesLoaded++;
return; return;
} }
} }
@ -78,63 +75,30 @@ var umbraInstagramBehavior = {
} }
if (this.state === "waiting-big-image") { if (this.state === "waiting-big-image") {
if(this.currentBigImage == null) { if(this.bigImagesLoaded < this.imageCount) {
var imageFrame = document.querySelectorAll("div.Modal div.Item div.iMedia div.Image"); var rightArrow = document.querySelectorAll(".coreSpriteRightPaginationArrow");
if (imageFrame.length > 0 && imageFrame[0].getAttribute("src") !== this.previousBigImage ) { if (rightArrow.length > 0) {
this.currentBigImage = new Image(); // console.log("clicking right arrow");
this.currentBigImage.src = imageFrame[0].getAttribute("src"); rightArrow[0].click();
//console.log("this.currentBigImage.naturalWidth=" + this.currentBigImage.naturalWidth + " this.currentBigImage.src=" + this.currentBigImage.src); this.idleSince = null;
return; this.bigImagesLoaded++;
} else if(this.idleSince == null ) {
console.log("waiting for image frame to load");
this.idleSince = Date.now();
return;
} }
} else if (this.currentBigImage.src !== this.previousBigImage && this.currentBigImage.naturalWidth !== 0) {
console.log("next big image appears loaded, will click right arrow next time");
this.state = "click-next-big-image";
this.previousBigImage = this.currentBigImage.src;
this.currentBigImage = null;
this.bigImagesLoaded++;
this.idleSince = null;
if (this.bigImagesLoaded >= this.imageCount) {
console.log("looks like we're done, we've loaded all " + this.bigImagesLoaded + " of " + this.imageCount + " big images");
this.state = "finished";
this.idleSince = Date.now();
}
return;
} else if(this.idleSince == null) {
console.log("Waiting for big image to load");
this.idleSince = Date.now();
return;
}
}
if (this.state === "click-next-big-image") {
var rightArrow = document.querySelectorAll("a.mmRightArrow");
if (rightArrow.length > 0) {
// console.log("clicking right arrow");
rightArrow[0].click();
this.state = "waiting-big-image";
this.idleSince = null;
return;
} else { } else {
console.warn("no right arrow to click?? weird"); console.log("looks like we're done, we've loaded all " + this.bigImagesLoaded + " of " + this.imageCount + " big images");
this.state = "finished";
this.idleSince = Date.now(); this.idleSince = Date.now();
return;
} }
return;
} }
}, },
start: function() { start: function() {
var that = this; var that = this;
this.intervalId = setInterval(function(){ that.intervalFunc() }, 50); this.intervalId = setInterval(function(){ that.intervalFunc(); }, 300);
}, },
isFinished: function() { isFinished: function() {
if (this.idleSince != null) { if (this.idleSince !== null) {
var idleTimeMs = Date.now() - this.idleSince; var idleTimeMs = Date.now() - this.idleSince;
if (idleTimeMs / 1000 > this.IDLE_TIMEOUT_SEC) { if (idleTimeMs / 1000 > this.IDLE_TIMEOUT_SEC) {
clearInterval(this.intervalId); clearInterval(this.intervalId);
@ -142,10 +106,10 @@ var umbraInstagramBehavior = {
} }
} }
return false; return false;
}, }
}; };
// Called from outside of this script. // Called from outside of this script.
var umbraBehaviorFinished = function() { return umbraInstagramBehavior.isFinished() }; var umbraBehaviorFinished = function() { return umbraInstagramBehavior.isFinished(); };
umbraInstagramBehavior.start(); umbraInstagramBehavior.start();