Created
July 26, 2025 10:29
-
-
Save do-me/74e2c8047701aebcf35a2184613ab566 to your computer and use it in GitHub Desktop.
Telegram chat mining client-side
This file contains hidden or 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 () => { | |
// --- 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); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Then copy the array from the console and use Gemini to summarize.