YouTube Shorts Auto Commenter

Automatically posts comments on YouTube Shorts videos with a delay for smooth execution.

// ==UserScript==
// @name         YouTube Shorts Auto Commenter
// @namespace    https://github.com/yashu1wwww
// @version      1.2
// @description  Automatically posts comments on YouTube Shorts videos with a delay for smooth execution.
// @author       Yashawanth R
// @license      MIT
// @match        https://*.youtube.com/shorts/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  // List of comments (from your Python code)
  const comments = [
    "Super!",
    "Amazing one!",
    "What an acting!",
    "Great video!",
    "Have a nice day!",
    "Keep going!",
    "Keep rocking!",
    "All the best buddy!",
    "Next video please!",
    "Wonderful day!",
    "Superb!",
    "Shared with my friends!",
    "Marvelous!",
  ];

  const MAX_COMMENTS = 50; // Number of comments to post
  let commentCount = 0; // Counter for comments

  // Selectors for YouTube Shorts
  const SELECTORS = {
    COMMENT_BUTTON: '#comments-button > ytd-button-renderer > yt-button-shape > label > button > yt-touch-feedback-shape > div > div.yt-spec-touch-feedback-shape__fill',
    COMMENT_BOX: 'ytd-comments ytd-comment-simplebox-renderer div#placeholder-area',
    COMMENT_INPUT: '#contenteditable-root',
    COMMENT_SUBMIT_BUTTON: '#submit-button',
  };

  // Helper function: Delay
  const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

  // Wait for the comment box to be visible
  async function waitForCommentBox() {
    try {
      const commentBox = await new Promise((resolve, reject) => {
        const interval = setInterval(() => {
          const element = document.querySelector("ytd-comments ytd-comment-simplebox-renderer");
          if (element) {
            clearInterval(interval);
            resolve(element);
          }
        }, 500); // Check every 500ms
        setTimeout(() => reject(new Error('Timeout waiting for comment box')), 20000); // Wait max 20 seconds
      });

      console.log("Comment box is visible.");
    } catch (error) {
      console.error('Error waiting for comment box:', error);
      return false;
    }
    return true;
  }

  // Post a comment
  async function postComment() {
    try {
      const commentBox = document.querySelector(SELECTORS.COMMENT_BOX);
      if (commentBox) {
        console.log('Opening comment box...');
        commentBox.click();
        await delay(1000); // Wait for the input box to appear

        const commentInput = document.querySelector(SELECTORS.COMMENT_INPUT);
        if (commentInput) {
          const randomComment = comments[Math.floor(Math.random() * comments.length)]; // Random comment from the list
          console.log(`Adding comment: ${randomComment}`);
          commentInput.innerText = randomComment;

          const submitButton = document.querySelector(SELECTORS.COMMENT_SUBMIT_BUTTON);
          if (submitButton) {
            console.log('Submitting comment...');
            submitButton.click();
            commentCount++;
            return true;
          }
        }
      }
      console.log('Comment box or input not found.');
      return false;
    } catch (error) {
      console.error('Error posting comment:', error);
      return false;
    }
  }

  // Main function
  async function autoComment() {
    console.log('Starting YouTube Shorts Auto Commenter...');
    
    // Wait for 5 seconds to allow the video and elements to load
    console.log('Waiting for 5 seconds...');
    await delay(5000); // Wait for 5 seconds

    while (commentCount < MAX_COMMENTS) {
      const commentSectionOpened = await waitForCommentBox(); // Wait for the comment section to be visible
      if (!commentSectionOpened) break;
      
      const success = await postComment(); // Post a comment
      if (!success) break;

      await delay(4000); // Wait before posting the next comment
    }

    console.log('Finished posting comments.');
  }

  // Run script only on Shorts pages
  if (window.location.href.includes('/shorts/')) {
    setTimeout(autoComment, 3000); // Delay to allow the page to load initially
  }
})();