Skip to content

Instantly share code, notes, and snippets.

@rostegg
Last active August 11, 2021 19:49
Show Gist options
  • Select an option

  • Save rostegg/a7fa9c7decdd0ee6891f5e619b8ad8fa to your computer and use it in GitHub Desktop.

Select an option

Save rostegg/a7fa9c7decdd0ee6891f5e619b8ad8fa to your computer and use it in GitHub Desktop.
Find all messages in web telegram and save them to file
const filter = "the text by which messages will be filtered"
const fileName = "file.json"
function stripHtml(html){
const tmp = document.createElement("DIV")
tmp.innerHTML = html
return tmp.textContent || tmp.innerText || ""
}
function saveAs(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}
const blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
// New web telegram version - `user-last-message` class in search box
const messagesElements = document.getElementsByClassName("im_message_text")
// change filter or map to modify messages if required
const messages = Array.from(messagesElements).
filter(el => el.innerHTML.includes(filter)).
map(el => {
/*el -> div object*/
/*use stripHtml(el.innerHTML) to get text*/
/*
Just some example....
const text = stripHtml(el.innerHTML)
const [url, chapter] = text.split(` - `);
return {url, chapter};
*/
return el;
})
saveAs(messages, fileName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment