Skip to content

Instantly share code, notes, and snippets.

@amerberg
Last active August 29, 2015 14:13
Show Gist options
  • Save amerberg/1acefa3ccb4ad1ac4f89 to your computer and use it in GitHub Desktop.
Save amerberg/1acefa3ccb4ad1ac4f89 to your computer and use it in GitHub Desktop.
Monitor Quora questions
(function(){
//Initialize counts of how many are already monitored,
//how many we're newly monitoring, how many we failed to monitor
var already = 0;
var succeeded = 0;
var failed = 0;
var interrupt = false;
$(document).keyup(function(e){if(e.keyCode==27){interrupt = true;}});
$("body").append("<div id='monitor_counts' style='position:fixed; top:100px; left: 5px;'></div>");
$("#monitor_counts").append("<p class='already'>Already monitoring: <span class='num'>0</span></p>")
.append("<p class='succeeded'>Newly monitoring: <span class='num'>0</span></p>")
.append("<p class='failed'>Failed to monitor: <span class='num'>0</span></p>");
//function to monitor questions from links wrapped in a jQuery object
function monitorSelection(links) {
links.each(function(index){
var container = $(this).parents(".UserContentListItem");
var url = $(this).attr("href");
container.append("<iframe height='100px'></iframe>");
container.append("<span class='monitor'></span>");
var monitorStatus=$(".monitor", container);
var iframe = $("iframe", container);
var selector = ".hover_menu_contents a.checkable_item span:contains('Monitor')";
iframe.load(function(){
var iframe=$(this);
var ijQuery=iframe.get(0).contentWindow.jQuery;
var label = ijQuery(selector);
var checkbox = label.siblings("input");
if(checkbox.is(":checked")){
monitorStatus.text("Already monitoring.").addClass("completed");
already++;
$(".already .num").text(already.toString());
iframe.remove();
}
else{
function checkMonitorStatus(time) {
var checkbox = ijQuery(selector).siblings("input");
if(checkbox.is(":checked")) {
iframe.remove();
monitorStatus.text("Now monitoring!").addClass("completed");
succeeded ++;
$(".succeeded .num").text(succeeded.toString());
}
else if(time>0){
setTimeout(function(){checkMonitorStatus(time-1)}, 1000);
monitorStatus.text("Attempting to monitor: " + (time-1).toString() + "s remaining");
}
else {
iframe.remove();
monitorStatus.text("Failed to monitor!").addClass("completed");
failed ++;
$(".failed .num").text(failed.toString());
}
}
label.click();
//We'll give it 20 seconds before giving up. The number is arbitrary.
checkMonitorStatus(20);
}
});
iframe.attr("src", url);
});
}
//Monitor all questions on the page.
function monitorAll() {
if($(".UserContentListItem:has(.monitor:not(.completed))").length>0){
setTimeout(monitorAll, 1000);
}
else {
monitorSelection($(".UserContentListItem:not(:has(.monitor)) a.question_link"));
if(!interrupt) {
window.scrollTo(0, document.body.scrollHeight);
setTimeout(monitorAll, 3000);
}
}
}
monitorAll();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment