DeveloperBreeze

X.Com Automation Development Tutorials, Guides & Insights

Unlock 1+ expert-curated x.com automation tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your x.com automation skills on DeveloperBreeze.

How to Create a Chrome Extension for Automating Tweets on X (Twitter)

Tutorial December 10, 2024
javascript css html

let intervalId = null;
const tweets = ["https://developerbreeze.com/post/59"];
let engage = [
  "Want to code like a pro? 🚀 These tutorials will get you there in no time! 💻🔥",
  "Struggling with coding? 🧠 These beginner-friendly tips will blow your mind! 🤯",
];

let currentTweetIndex = 0;

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === "start") {
    if (!intervalId) {
      intervalId = setInterval(() => {
        const currentTweet = tweets[currentTweetIndex];
        const randomEngage = engage[Math.floor(Math.random() * engage.length)];
        const tweet = `${randomEngage} ${currentTweet}`;

        const encodedTweet = encodeURIComponent(tweet);
        const shareUrl = `https://x.com/intent/tweet?text=${encodedTweet}`;

        chrome.tabs.create({ url: shareUrl, active: true }, (tab) => {
          chrome.tabs.onUpdated.addListener(function listener(tabId, info) {
            if (tabId === tab.id && info.status === "complete") {
              chrome.tabs.onUpdated.removeListener(listener);
              chrome.scripting.executeScript({
                target: { tabId },
                files: ["content.js"],
              });
            }
          });
        });

        currentTweetIndex = (currentTweetIndex + 1) % tweets.length;
      }, 300000); // Post every 5 minutes
      sendResponse({ status: "Running" });
    } else {
      sendResponse({ status: "Already Running" });
    }
  } else if (request.action === "stop") {
    clearInterval(intervalId);
    intervalId = null;
    sendResponse({ status: "Stopped" });
  }
});

The content.js file interacts directly with the X website to type and post tweets. Add the following code: