Last active
August 11, 2021 21:52
-
-
Save kosciolek/f3abcc064c95edc02fd0beb2e7e9c00f to your computer and use it in GitHub Desktop.
Facebook, scrape conversation messages
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
;(async () => { | |
console.info('Type stop() to stop.') | |
const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); | |
const msgSelector = ".j83agx80.jwdofwj8.pby63qed"; | |
const msgTextSelector = ".oo9gr5id[dir=auto]"; | |
const msgAuthorSelector = ".pfnyh3mw.r9r71o1u.m9osqain.fsrhnwul.dkr8dfph"; | |
const scrollableSelector = | |
".buofh1pr.j83agx80.eg9m0zos.ni8dbmo4.cbu4d94t.gok29vw1"; | |
const msgContainerSelector = '[aria-label=Messages]'; | |
const scrollElem = document.querySelector(scrollableSelector); | |
const msgContainer = scrollElem.querySelector(msgContainerSelector); | |
let resumeFlag = 1; | |
window.messages = []; | |
window.stop = () => { | |
console.info('Stopped'); | |
resumeFlag = 0; | |
scrollElem.querySelectorAll(msgSelector).forEach((group) => { | |
const author = group.querySelector(msgAuthorSelector).innerText; | |
const content = group.querySelector(msgTextSelector)?.innerText; | |
window.messages.push({ author, content }); | |
}); | |
const btn = document.createElement('button'); | |
const serialized = JSON.stringify(messages); | |
btn.onclick = () => navigator.clipboard.writeText(serialized); | |
btn.innerHTML = "COPY RECORDED MESSAGES TO THE CLIPBOARD"; | |
btn.style = "position: fixed; z-index: 99999; top: 0; left: 0; right: 0; height: 100px;" | |
document.body.prepend(btn); | |
console.log(serialized); | |
console.log('Press the button to copy messages to the clipboard.') | |
}; | |
while (resumeFlag) { | |
scrollElem.scrollTop = 1000; | |
await sleep(1000); | |
scrollElem.scrollTop = 0; | |
await sleep(1000); | |
console.info(`Approx messages in view: ${msgContainer.childElementCount}`); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment