Last active
August 29, 2015 14:04
-
-
Save brainkim/57e950d5eca79db08175 to your computer and use it in GitHub Desktop.
Scrape those hubspot email templates (probably doesn't work anymore?)
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
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