Created
February 11, 2016 21:39
-
-
Save janyksteenbeek/920cceaeb617fe0d93b6 to your computer and use it in GitHub Desktop.
Grab your latest post count from the Google Product Forums using PhantomJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function waitFor(testFx, onReady, timeOutMillis) { | |
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s | |
start = new Date().getTime(), | |
condition = false, | |
interval = setInterval(function() { | |
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) { | |
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code | |
} else { | |
if(!condition) { | |
phantom.exit(1); | |
} else { | |
typeof(onReady) === "string" ? eval(onReady) : onReady(); | |
clearInterval(interval); | |
} | |
} | |
}, 250); | |
}; | |
// This is where the magic starts | |
var url = 'https://productforums.google.com/forum/#!profile/youtube/APn2wQfyTyuz6gxoyO2o6xTtMqAgQP59DqH9lLHtew5nO1TR_WxS2EmCDHwIZht_nGQrIGKTdOM6'; // Link to your profile | |
var page = require('webpage').create(); | |
page.open(url, function (status) { | |
// Check for page load success | |
if (status !== "success") { | |
console.log("Oops"); | |
} else { | |
page.includeJs("https://code.jquery.com/jquery-1.12.0.min.js"); // Hi jQuery | |
waitFor(function() { | |
return page.evaluate(function() { | |
return jQuery(".NUXPAI-x-b").is(":visible"); // Is the graph there yet? | |
}); | |
}, function() { | |
var postsLastMonth = page.evaluate(function() { return jQuery('.gwt-viz-container table tbody tr:last td:last').text() }); // Thats where the posts are | |
console.log(postsLastMonth); | |
phantom.exit(); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment