Last active
July 13, 2017 16:50
-
-
Save brandonmp/1483936fb06862e32271cf7349bc7dd4 to your computer and use it in GitHub Desktop.
copy facebook messenger history to clipboard in chrome console
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
// this works in the chrome console at the messenger.com interface, eg | |
// https://www.messenger.com/t/yourfriendsname | |
/* first scroll to top, then copy all msgs, timestamps and names. | |
* it doesn't scroll "to the top" strictly speaking, but instead just | |
* scrolls a few times (that's all we needed for our use case). | |
* you can adjust the # of times it scrolls by extending | |
* SCROLL_COUNT in scrollToTop(). | |
* results are in TSV format with no header row, eg, | |
* July 1st, 6:06pm\tBrandon\totherwise ur wasting ur time\n | |
* note vars don't have 'var' or 'const'. this is b/c we are going from msg to msg and | |
* executing this code, so we can't re-declare vars each time. */ | |
messagesContainerNode = document.querySelectorAll('h3')[1].parentNode; | |
// have to pass window.copy in from top scope. not sure why but can't | |
// access it in the getMessages function | |
scrollToTop = async (doc, parentNode, copy_function) => { | |
var SCROLL_COUNT = [0, 1, 2, 3, 4, 5, 6, 7] | |
var sleep = () => new Promise(resolve => setTimeout(resolve, 1000)); | |
var parentNode, refNode; | |
// scrollIntoView on the first element in the container doesn't scroll | |
// high enough to trigger infinite scroll, so we'll prepend our own node in the container | |
var newEle = doc.createElement('div'); | |
newEle.style.height = 1; | |
newEle.style.width = 1; | |
for (let i in SCROLL_COUNT) { | |
// re-insert the target scroll node each time since container. unsure if it's necessary though | |
refNode = parentNode.firstChild; | |
parentNode.insertBefore(newEle, refNode); | |
newEle.scrollIntoView(); | |
await sleep(); | |
} | |
parentNode.firstChild.remove(); | |
return copy_function; | |
}; | |
getMessages = msgContainerNode => { | |
msgs = [...msgContainerNode.children][2]; | |
return Array.from(msgs.querySelectorAll('[data-tooltip-content]')) | |
.map(n => { | |
const text = n.textContent; | |
if (!text) return null; | |
const timestamp = n.getAttribute('data-tooltip-content'); | |
const name = | |
n.parentNode && | |
n.parentNode.parentNode && | |
n.parentNode.parentNode.firstChild && | |
n.parentNode.parentNode.firstChild.textContent; | |
return `${timestamp}\t${name}\t${text}\n`; | |
}) | |
.filter(n => !!n) | |
.join(''); | |
}; | |
scrollToTop(document, messagesContainerNode, copy).then(copy_function => { | |
m = getMessages(messagesContainerNode); | |
console.log('done'); | |
copy_function(m); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment