skip to Main Content

This is my VioletMonkey script the problem I am facing is that it clicks invite link but after that nothing happens not logging in console

like after inviteLink.click() the console.log(‘Proceeding Further’) isnt getting logged which means script isn’t doing further interaction


// ==UserScript==
// @name         Telegram Invite Link Auto Clicker
// @namespace    Violentmonkey Scripts
// @version      1.0
// @description  Automatically click Telegram invite links when they are posted in a specific chat channel and join the group
// @author       Your Name
// @match        https://web.telegram.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const TARGET_CHANNEL = 'https://web.telegram.org/k/#-2159974321';

    async function setupMutationObserver() {
        const observer = new MutationObserver((mutationsList, observer) => {
            mutationsList.forEach(mutation => {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE && node.classList.contains('message')) {
                        processMessageContainer(node);
                    }
                });
            });
        });

        const body = document.body;

        if (body) {
            observer.observe(body, { childList: true, subtree: true });
            console.log('Telegram Invite Link Auto Clicker started.');
        } else {
            console.error('Document body not found.');
        }
    }

    async function processMessageContainer(messageContainer) {
        const inviteLink = messageContainer.querySelector('.anchor-url');
        const channelLink = getChannelLink(messageContainer);

        if (inviteLink && channelLink === TARGET_CHANNEL) {
            console.log('New invite link detected in channel:', channelLink);
            console.log('Invite link detected:', inviteLink.href);
            inviteLink.click();
            console.log('Proceeding Further')
            waitForPopupAndClickJoinGroup();
        }
    }

    async function waitForPopupAndClickJoinGroup() {
        console.log('Waiting for popup to appear...');
        const popupElement = await waitForElement('.popup.popup-peer.popup-join-chat-invite.active');
        
        if (popupElement) {
            console.log('Popup element found. Clicking Join Group button...');
            const joinGroupButton = popupElement.querySelector('.popup-button.btn.primary.rp .i18n');
            if (joinGroupButton && joinGroupButton.textContent.trim() === 'Join Group') {
                console.log('Join Group button found. Clicking...');
                joinGroupButton.click();
            } else {
                console.log('Join Group button not found or does not contain expected text.');
            }
        } else {
            console.log('Popup element not found.');
        }
    }

    function getChannelLink(messageContainer) {
        const channelLinkElement = messageContainer.querySelector('.peer a');
        return channelLinkElement ? channelLinkElement.href : '';
    }

    async function waitForElement(selector, timeout = 10000) {
        return new Promise((resolve, reject) => {
            const startTime = Date.now();
            const interval = setInterval(() => {
                const element = document.querySelector(selector);
                if (element) {
                    clearInterval(interval);
                    resolve(element);
                } else if (Date.now() - startTime >= timeout) {
                    clearInterval(interval);
                    reject(new Error(`Timeout waiting for element with selector: ${selector}`));
                }
            }, 500);
        });
    }

    function delay(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    document.addEventListener('DOMContentLoaded', () => {
        setupMutationObserver();
    });

})();

Expecting to automate the group joining by making script to execute further steps after invite link clicked

2

Answers


  1. Please remember the following text:
    When the inviteLink is clicked in your processMessageContainer function, ‘Proceeding Further’ is logged immediately, followed by a call to waitForPopupAndClickJoinGroup(). However, the click action on inviteLink may not always result in an immediate popup or the expected behavior due to the asynchronous nature of web interactions.

    To ensure proper sequencing, it is recommended to await the click action and then proceed to wait for the popup.

    Login or Signup to reply.
  2. Check that CSS selectors like.popup.popup-peer.popup-join-chat-invite.active,.popup-button.btn.primary.rp.i18n, and others match the elements in the Telegram web app.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search