Skip to content

Instantly share code, notes, and snippets.

@KristerV
Last active August 31, 2018 12:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KristerV/873bf14e9dc73aeed0d7db11b257fb79 to your computer and use it in GitHub Desktop.
Save KristerV/873bf14e9dc73aeed0d7db11b257fb79 to your computer and use it in GitHub Desktop.
Scrape Facebook Group
/*
This will expand all of the posts and comments that is in the view. It will take a lot of time!
To use just paste this in your console (F12).
*/
var previousScrollheight = 0
scrollDown = function() {
var currentScrollheight = document.body.scrollHeight
var canScroll = currentScrollheight - previousScrollheight
previousScrollheight = document.body.scrollHeight
if (canScroll !== 0) {
window.scrollTo(0,currentScrollheight);
}
return canScroll
}
expandComments = function() {
var foundLinks = 0
document.querySelectorAll(':not(.text_exposed) > .text_exposed_hide > .text_exposed_link > .see_more_link').forEach(function(item) {
item.click()
foundLinks++
})
document.querySelectorAll('[href="#"]').forEach(function(item){
var whitelist = ["See more", "More option", "more comment", "replie"]
var blacklist = ["Hide", " hidden", "u_0_19"]
var blacklistRegexp = new RegExp(blacklist.join("|"), "i")
var whitelistResult = new RegExp(whitelist.join("|"), "i").test(item.innerHTML)
var blacklistResultHTML = !(blacklistRegexp.test(item.innerHTML))
var blacklistResultClass = !(blacklistRegexp.test(item.className))
var blacklistResultId = !(blacklistRegexp.test(item.id))
if (whitelistResult && blacklistResultHTML && blacklistResultClass && blacklistResultId) {
foundLinks++
item.click()
}
})
return foundLinks
}
procedure = function() {
setTimeout(function(){
var scrolled = scrollDown()
var expanded = expandComments()
console.log("Scrolled:", scrolled, ". Expanded:", expanded)
if (scrolled !== 0 && expanded !== 0) {
procedure()
} else {
console.log("Finished procedure")
setTimeout(function(){
startOver()
}, 1000 * 60 * 1)
}
}, 10000)
}
var tries = 0
startOver = function() {
if (tries < 10) {
console.log("Try:", tries)
tries++
procedure()
} else {
console.log("Finished whole program.")
}
}
startOver()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment