Usage: go to the users profile you want to scrape and paste the script in the console section of the inspect window. Or run it anywhere you can run JavaScript and enter username when prompted.
Show some love yall
JavaScript:
/**
* Reddit Subreddit Post Collector (JSON API Version)
*
* This script collects all subreddits a user has posted in using Reddit's JSON API
* which is more reliable than DOM scraping.
*
* Usage:
* 1. Enter a username when prompted
* 2. Script will fetch all available posts and comments
*/
async function collectUserSubreddits() {
// Get username from prompt or URL
let username;
if (window.location.pathname.includes('/user/')) {
username = window.location.pathname.split('/')[2];
} else {
username = prompt("Enter Reddit username (without u/):");
}
if (!username) {
console.error("No username provided");
return;
}
console.log(`Starting to collect subreddits for u/${username}...`);
// Initialize data structures
const subreddits = new Set();
let after = null;
let totalProcessed = 0;
const contentTypes = ["submitted", "comments"];
// Process each content type (posts and comments)
for (const contentType of contentTypes) {
console.log(`Fetching user ${contentType}...`);
let keepFetching = true;
after = null;
while (keepFetching) {
try {
// Build the API URL
let apiUrl = `https://www.reddit.com/user/${username}/${contentType}.json?limit=100`;
if (after) {
apiUrl += `&after=${after}`;
}
// Fetch the data
const response = await fetch(apiUrl);
if (!response.ok) {
console.error(`Error fetching data: ${response.status} ${response.statusText}`);
keepFetching = false;
continue;
}
const data = await response.json();
const items = data.data.children;
if (items.length === 0) {
console.log(`No more ${contentType} to process`);
keepFetching = false;
continue;
}
// Process the items
items.forEach(item => {
const subredditName = item.data.subreddit_name_prefixed ||
(item.data.subreddit ? `r/${item.data.subreddit}` : null);
if (subredditName) {
subreddits.add(subredditName);
}
});
totalProcessed += items.length;
console.log(`Processed ${items.length} ${contentType}, total: ${totalProcessed}, unique subreddits: ${subreddits.size}`);
// Update pagination
after = data.data.after;
if (!after) {
console.log(`Reached end of ${contentType}`);
keepFetching = false;
} else {
// Add a small delay to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 1000));
}
} catch (error) {
console.error('Error processing data:', error);
keepFetching = false;
}
}
}
// Display results
console.log('=== RESULTS ===');
console.log(`User u/${username} has posted in ${subreddits.size} subreddits:`);
const sortedSubreddits = Array.from(subreddits).sort();
sortedSubreddits.forEach((sub, index) => {
console.log(`${index + 1}. ${sub}`);
});
// Create a copy-paste friendly version
console.log('\nCopy-paste friendly list:');
console.log(sortedSubreddits.join('\n'));
// Create JSON data
console.log('\nJSON data:');
console.log(JSON.stringify({
username: username,
total_subreddits: subreddits.size,
subreddits: sortedSubreddits,
date_collected: new Date().toISOString()
}, null, 2));
}
// Run the function
collectUserSubreddits();
Show some love yall