Skip to content

Instantly share code, notes, and snippets.

@brainkim
Last active August 29, 2015 14:04
Show Gist options
  • Save brainkim/57e950d5eca79db08175 to your computer and use it in GitHub Desktop.
Save brainkim/57e950d5eca79db08175 to your computer and use it in GitHub Desktop.
Scrape those hubspot email templates (probably doesn't work anymore?)
function getLink(id) {
var links = $('.zoomer-cover a')
.map(function() { return $(this).attr('href'); })
.filter(function() { return this.indexOf(id) > -1; });
return links[0];
}
function extractRowData(row) {
var id = $(row).data('content-id');
var link = getLink(id);
var name = $(row).find('a.name').text();
return {
id: id,
link: link,
name: name,
};
}
function getTableData(done) {
var trs = $('#main_table__email tbody tr');
var i = 0;
var results = [];
(function loop() {
setTimeout(function() {
if (i < trs.length) {
var row = trs[i];
$(row).click();
setTimeout(function() {
i++;
results.push(extractRowData(row));
loop();
});
}
else {
done(results);
}
}, 0);
})();
}
function hasNext() {
return $('#main_table__email_next').attr('class').indexOf('disabled') === -1;
}
function nextPage(done) {
var results = [];
(function loop() {
getTableData(function(newResults) {
Array.prototype.push.apply(results, newResults);
var nextButton = $('#main_table__email_next');
if (hasNext()) {
nextButton.click();
setTimeout(loop, 0);
}
else {
done(results);
}
});
})();
}
nextPage(function(results) {
window.results = results;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment