Skip to content

Instantly share code, notes, and snippets.

@realamirhe
Created December 27, 2022 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realamirhe/5d88014e7840b788a003756d7d7b8a55 to your computer and use it in GitHub Desktop.
Save realamirhe/5d88014e7840b788a003756d7d7b8a55 to your computer and use it in GitHub Desktop.
Crawl discord images and text
window.discordRef = Object.keys(window.discordRef).length ? window.discordRef : {};
function debounceEvent(callback, time) {
let interval;
return () => {
clearTimeout(interval);
interval = setTimeout(() => {
interval = null;
callback(arguments);
}, time);
};
}
function prompt_extractor(li) {
const id = li.id.split("chat-messages-").pop();
const time = li.querySelector("time").dateTime;
let src = li.querySelector('img[src*="attachments"]')?.src
if (src) {
src = new URL(src);
src.search = "";
src = src.toString();
}
const userid = li.querySelector('[id^="message-username"]')?.id.split("message-username-").pop() || null;
const msg = li.querySelector('[id^="message-content"]');
const texts = [...msg.childNodes].map((i) => {
if (i.tagName === "STRONG") return { prompt: i.innerText };
if (i.nodeName === "#text") return { command: i.textContent };
if (i.tagName === "SPAN" && [...i.classList].includes("mention")) return { user: i.innerText };
// span.timestamp edited!
// return i;
});
return { time, src, userid, texts, id };
}
const crawlDiscord = () => {
window.discordRef = [...document.querySelectorAll('li[id^="chat-messages"]')]
.map(prompt_extractor)
.reduce((acc, message) => {
acc[message.time + message.userid] = message;
return acc;
}, window.discordRef);
console.log(`${Object.keys(window.discordRef).length} has been collected!`);
};
// const crawler = debounceEvent(crawlDiscord, 0);
// window.removeEventListener("scroll", () => console.log('scroll'));
// window.addEventListener("scroll", crawler);
const i = setInerval(crawlDiscord, 1000)
const close = clearInterval(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment