Skip to content

Instantly share code, notes, and snippets.

@rickcnagy
Created April 24, 2014 23:52
Show Gist options
  • Save rickcnagy/11273501 to your computer and use it in GitHub Desktop.
Save rickcnagy/11273501 to your computer and use it in GitHub Desktop.
Making a standard outline for my console js scripts. Work in progress.
function main() {
var selector = "button:contains(Delete)"
new Iterator(selector, true, true).loop();
}
Iterator.prototype.loop = function() {
this.elem.click();
$("button:contains(Yes, delete)").click();
this.reLoop();
};
// ==========
// = Static =
// ==========
function Iterator(selector, alwaysRefresh, useFirst) {
this.selector = selector;
this.alwaysRefresh = alwaysRefresh === null ? false : alwaysRefresh;
this.alwaysUseFirst = useFirst === null ? false : useFirst;
this.currentIndex = 0;
this.endNow = false;
this.elems = $(this.selector);
this.nextElem();
$(window).keypress(function(e) {
if (e.which === 3) this.endNow = true;
});
}
// Call this each time to restart the loop
Iterator.prototype.reLoop = function() {
this.nextElem();
this.afterLoad(this.loop);
};
Iterator.prototype.nextElem = function() {
if (!this.alwaysUseFirst) {
this.currentIndex ++;
}
if (this.alwaysRefresh) {
this.elems = $(this.selector);
}
this.elem = this.elems.get(this.currentIndex);
};
Iterator.prototype.quit = function() {
while ($("*:contains('Close')" ).length) {
$( "*:contains('Close'):last" ).click();
}
throw new Error("Aborted JS");
};
Iterator.prototype.afterLoad = function(callback, param) {
if (this.endNow) this.quit();
else {
var load = setInterval(function() {
if (!$("*[class^='load']:visible:not('.ribbonSelectorWidget *')").length) {
clearInterval(load);
callback(param);
}
}, 10, param);
}
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment