Auto Reply Komubot

Auto click on button

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Auto Reply Komubot
// @namespace    http://tampermonkey.net/
// @version      2025-12-12.1
// @description  Auto click on button
// @author       Thaibm
// @match        https://mezon.ai/chat/direct/message/1836948396888297472/3
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mezon.ai
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  console.log("[TM] Flexible auto-click script started");

  let lastClickedId = null;

  function checkLastMessage() {
    console.log("[TM] Check last message auto-click script started");
    const items = document.querySelectorAll(".message-list-item");
    if (!items.length) return;

    // Lấy message cuối cùng
    const last = items[items.length - 1];
    const msgId = last.id;

    // Nếu đã click message này rồi -> bỏ qua
    if (msgId === lastClickedId) return;

    // Tìm tất cả button đúng format
    const buttons = last.querySelectorAll(
      "button.px-5.py-1.rounded.bg-buttonPrimary.text-white.font-medium"
    );

    // Nếu không có button -> bỏ qua
    if (buttons.length === 0) return;

    // Chọn random 1 button
    const btn = buttons[Math.floor(Math.random() * buttons.length)];

    console.log(
      `[TM] Clicking button "${btn.textContent.trim()}" in message ${msgId}`
    );

    btn.click();
    lastClickedId = msgId; // đánh dấu đã click
  }

  // Chạy lần đầu sau khi DOM load
  setTimeout(checkLastMessage, 1500);

  let debounceTimer = null;

  function initObserver() {
    const wrap = document.querySelector(".messages-wrap");
    if (!wrap) {
      // Retry sau khi DOM load đủ
      setTimeout(initObserver, 300);
      return;
    }

    const observer = new MutationObserver(() => {
      if (debounceTimer) clearTimeout(debounceTimer);

      debounceTimer = setTimeout(() => {
        checkLastMessage();
      }, 150);
    });

    observer.observe(wrap, {
      childList: true,
      subtree: true,
    });

    console.log("[TM] Optimized observer initialized");
  }

  initObserver();
})();