JavaScript to scrape all subs a user has posted or commented in

krummy

Newbie
Joined
Sep 5, 2024
Messages
32
Reaction score
23
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.

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
 
Works great but might add retries in case Reddit rate-limits.
 
meh spent 5 minutes on it to save myself the time of digging manually. If its a tool people may be intrested I could easily make it search lists of users, filter by topic and size yada yada. If theres a way reddits api will specifically say the subs security settings then Ill probably make it anyways.
 
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.

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
You can do this in a PRAW without javascript.
 
You can do this in a PRAW without javascript.
And you can do it in javascript without python? I don't like python and reddits api is easy you could do it in literally anything.
 
Is
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.

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
is it another way of hacker to steal the data of people?
 
Is

is it another way of hacker to steal the data of people?
Nah for finding lists of sub in a category. Go to huge sub in target category find some accs w hella karma scrape their subs and have chat gtp filter it to the target catagory.
 
i have lots of Node js/ java script familiarity with bots and scrapers; i once setup a script for a client to auto forward whole discord server messages to another server... made a few bucks out of it as well....
lemme know if you need help with something specific technicality
 
Back
Top