Skip to content

Instantly share code, notes, and snippets.

@do-me
Created July 26, 2025 10:29
Show Gist options
  • Save do-me/74e2c8047701aebcf35a2184613ab566 to your computer and use it in GitHub Desktop.
Save do-me/74e2c8047701aebcf35a2184613ab566 to your computer and use it in GitHub Desktop.
Telegram chat mining client-side
(async () => {
// --- Configuration ---
const scrollDelay = 2000; // 2 seconds. Increase if your connection is slow.
const maxRetries = 5; // Will stop after 5 consecutive scrolls with no new messages.
// --- Script Logic ---
let allMessages = [];
const messageContainer = document.querySelector('.MessageList.custom-scroll');
const seenMessageIds = new Set();
if (!messageContainer) {
console.error("Error: Could not find the message container with selector '.MessageList.custom-scroll'. Please make sure the chat is active.");
return;
}
// Function to find new messages, extract text, and add to our set of seen messages
const getNewMessages = () => {
const messageElements = document.querySelectorAll('.message-list-item');
const newMessages = [];
messageElements.forEach(msgElement => {
const messageId = msgElement.getAttribute('data-message-id');
const textContentElement = msgElement.querySelector('.text-content');
// Process only if it's a new message with text content
if (messageId && !seenMessageIds.has(messageId) && textContentElement) {
// Clean up the text: remove newlines and extra spaces
const messageText = textContentElement.innerText.trim().replace(/\s*\n\s*/g, ' ');
if (messageText) {
newMessages.push(messageText);
seenMessageIds.add(messageId);
}
}
});
return newMessages;
};
// Function to scroll the message container to the very bottom
const scrollToBottom = () => {
// We set scrollTop to a very large number to ensure it scrolls to the absolute bottom
messageContainer.scrollTop = messageContainer.scrollHeight;
};
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
let noNewMessagesCount = 0;
console.log("--- Starting chat mining. Will scroll to the bottom repeatedly. ---");
while (noNewMessagesCount < maxRetries) {
const newMessages = getNewMessages();
if (newMessages.length > 0) {
allMessages.push(...newMessages); // Append new messages to the end of the array
noNewMessagesCount = 0; // Reset the counter since we found new content
console.log(`--- Found ${newMessages.length} new messages. Total collected: ${allMessages.length} ---`);
console.log("Current full array of messages:", allMessages);
} else {
noNewMessagesCount++;
console.log(`No new messages found. Attempt ${noNewMessagesCount} of ${maxRetries}...`);
}
// Scroll down to trigger loading more messages
scrollToBottom();
// Wait for the new messages to load
await delay(scrollDelay);
}
console.log("--- Mining finished! ---");
console.log(`Stopped after ${maxRetries} attempts with no new messages.`);
console.log("Total messages collected:", allMessages.length);
console.log("Final array of all messages:", allMessages);
})();
@do-me
Copy link
Author

do-me commented Jul 26, 2025

Useful for summarizing large group chats. Telegram has a sliding window mechanism that prevents loading all messages at once to html, so chunking is required.

Scrolling down atm; reversing is easy with:

    const scrollToTop = () => {
        messageContainer.scrollTop = 0;
    };

Then copy the array from the console and use Gemini to summarize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment