Skip to content

Instantly share code, notes, and snippets.

@gsusI
Last active July 9, 2024 12:31
Show Gist options
  • Save gsusI/f9ffc08a69cd35cf3b08ed63c516415f to your computer and use it in GitHub Desktop.
Save gsusI/f9ffc08a69cd35cf3b08ed63c516415f to your computer and use it in GitHub Desktop.
function expandAllThreads() {
console.log("Expanding threads...");
const expandButtons = document.querySelectorAll('button[aria-label^="Expand thread"]');
console.log(`Found ${expandButtons.length} expand buttons`);
expandButtons.forEach(button => button.click());
const moreRepliesButtons = document.querySelectorAll('button.text-tone-2.text-12:not([aria-expanded="true"])');
console.log(`Found ${moreRepliesButtons.length} 'more replies' buttons`);
moreRepliesButtons.forEach(button => button.click());
setTimeout(() => {
const newExpandButtons = document.querySelectorAll('button[aria-label^="Expand thread"]');
const newMoreRepliesButtons = document.querySelectorAll('button.text-tone-2.text-12:not([aria-expanded="true"])');
if (newExpandButtons.length > 0 || newMoreRepliesButtons.length > 0) {
console.log("Found more buttons to expand, continuing...");
expandAllThreads();
} else {
console.log("All threads expanded, capturing comments...");
captureComments();
}
}, 3000);
}
function scrollAndCapture() {
let lastHeight = 0;
let currentHeight = Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);
function scroll() {
window.scrollTo(0, currentHeight);
setTimeout(() => {
lastHeight = currentHeight;
currentHeight = Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);
if (currentHeight > lastHeight) {
console.log("Page size increased, continuing to scroll...");
scroll();
} else {
console.log("Page size stabilized, expanding threads...");
expandAllThreads();
}
}, 2000);
}
scroll();
}
function captureComments() {
const comments = document.querySelectorAll('shreddit-comment');
console.log(`Found ${comments.length} comments`);
console.log("Processing comments...");
let yaml = '';
const processedComments = new Set();
function processComment(comment) {
const thingId = comment.getAttribute('thingid');
if (processedComments.has(thingId)) {
console.log(`Skipping duplicate comment with thingId: ${thingId}`);
return;
}
processedComments.add(thingId);
const depth = parseInt(comment.getAttribute('depth') || '0', 10);
console.log(`Processing comment at depth ${depth} with thingId: ${thingId}`);
// Extract upvotes
const upvoteElement = comment.querySelector('shreddit-comment-action-row');
const upvotes = upvoteElement ? upvoteElement.getAttribute('score') || '0' : '0';
// Count direct child comments
const answers = Array.from(comment.querySelectorAll('shreddit-comment'))
.filter(child => parseInt(child.getAttribute('depth') || '0', 10) === depth + 1)
.length;
// Extract comment text
const commentContentElement = comment.querySelector('[id$="-comment-rtjson-content"]');
let commentText = '';
if (commentContentElement) {
commentText = commentContentElement.textContent.trim().replace(/\n/g, ' ');
}
yaml += `${' '.repeat(depth)}[${upvotes}][${answers}]${commentText}\n`;
// Process child comments
const childComments = Array.from(comment.querySelectorAll('shreddit-comment'))
.filter(child => parseInt(child.getAttribute('depth') || '0', 10) === depth + 1);
childComments.forEach(childComment => {
processComment(childComment);
});
}
comments.forEach((comment, index) => {
console.log(`Processing top-level comment ${index + 1}...`);
processComment(comment);
});
console.log("YAML output:");
console.log('Format: [upvotes][replies]<comment>');
console.log(yaml);
}
scrollAndCapture();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment