Last active
December 7, 2024 03:11
-
-
Save doronguttman/684ebc228c9651026fee5d8b95a21428 to your computer and use it in GitHub Desktop.
telegram export
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
var setTimeout; | |
(function (setTimeout) { | |
function async(delay) { | |
return new Promise((resolve) => setTimeout(resolve, delay)); | |
} | |
setTimeout.async = async; | |
})(setTimeout || (setTimeout = {})); | |
async function getMessageDateGroupHtml($messageDateGroup) { | |
let lastLoadedMessageId = ""; | |
do { | |
const lastMessage = $messageDateGroup.querySelector(`div.message-list-item:last-child`); | |
if (!lastMessage) { | |
return $messageDateGroup.outerHTML; | |
} | |
const lastMessageId = lastMessage.getAttribute(`data-message-id`) || ""; | |
if (lastMessageId === lastLoadedMessageId) { | |
return $messageDateGroup.outerHTML; | |
} | |
lastLoadedMessageId = lastMessageId; | |
lastMessage.scrollIntoView(); | |
await setTimeout.async(3000); | |
} while (!window.break); | |
return ""; | |
} | |
function getHead() { | |
return [ | |
"https://web.telegram.org/a/main.b833bcf83ed335f108ad.css", | |
"https://web.telegram.org/a/5980.b98189b7c15090a3bbcd.css", | |
"https://web.telegram.org/a/BundleMain.30f44ba659f3100b6f72.css", | |
] | |
.map((url) => `<link rel="stylesheet" type="text/css" href="${url}">`) | |
.concat(`<style> | |
body { | |
overflow: auto; | |
} | |
.message-list-item a { | |
color: blue !important; | |
} | |
.message-content { | |
height: max-content; | |
} | |
</style>`) | |
.join(``); | |
} | |
async function generateExport(messageDateGroupHtmls) { | |
const tab = window.open(``, `_blank`); | |
await setTimeout.async(2000); | |
if (!tab) { | |
throw new Error("Failed to open new tab"); | |
} | |
tab.document.body.innerHTML = messageDateGroupHtmls.join(``); | |
tab.document.head.innerHTML = getHead(); | |
tab.document.title = `Telegram Export ${new Date().toISOString()}`; | |
tab.document.body.querySelectorAll(`.Spoiler--concealed`).forEach((el) => { | |
el.classList.remove(`Spoiler--concealed`); | |
el.classList.remove(`Spoiler--animated`); | |
}); | |
} | |
async function* paginateMessageDateGroups() { | |
const processedDates = new Set(); | |
do { | |
const messageDateGroups = Array.from(document.querySelectorAll(`div.message-date-group`)); | |
const preProcessingCount = processedDates.size; | |
for (const messageDateGroup of messageDateGroups) { | |
const date = messageDateGroup.querySelector(`.sticky-date`)?.textContent?.trim() || | |
""; | |
if (processedDates.has(date)) { | |
continue; | |
} | |
console.log(`yielding ${date}`); | |
yield messageDateGroup; | |
processedDates.add(date); | |
} | |
if (preProcessingCount === processedDates.size) { | |
return; | |
} | |
await setTimeout.async(3000); | |
} while (!window.break && processedDates.size < 10); | |
} | |
async function execute() { | |
console.log(`execute`); | |
const messageDateGroupHtmls = []; | |
for await (const messageDateGroup of paginateMessageDateGroups()) { | |
const messageDateGroupHtml = await getMessageDateGroupHtml(messageDateGroup); | |
messageDateGroupHtmls.push(messageDateGroupHtml); | |
} | |
await generateExport(messageDateGroupHtmls); | |
} | |
execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment