Knolix - Smart Auto Click Tree

Automatically clicks the BTC tree or "Harvest Now" button when ready. Returns to homepage if redirected.

// ==UserScript==
// @name         Knolix - Smart Auto Click Tree
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  Automatically clicks the BTC tree or "Harvest Now" button when ready. Returns to homepage if redirected.
// @author       Rubystance
// @license      MIT
// @match        https://knolix.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=knolix.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const COIN_THRESHOLD = 60;
    const REDIRECT_DELAY_MS = 5000;
    const TREE_SELECTOR = '#btctree';
    const BITCOIN_IMG_SELECTOR = 'img[id^="bitcoin"]';
    const HARVEST_BUTTON_SELECTOR = '#claim_submit';
    const HOMEPAGE_PATH = '/';

    function isTreeFull() {
        const coins = document.querySelectorAll(BITCOIN_IMG_SELECTOR);
        return coins.length >= COIN_THRESHOLD;
    }

    function clickElement(selector, message) {
        const el = document.querySelector(selector);
        if (el) {
            console.log(`[Knolix Bot] ${message}`);
            el.click();
        }
    }

    function handleHomePage() {
        console.log('[Knolix Bot] Monitoring page for tree and button...');

        const observer = new MutationObserver(() => {
            if (isTreeFull()) {
                clickElement(TREE_SELECTOR, 'Tree is full. Clicking...');
            } else {
                clickElement(HARVEST_BUTTON_SELECTOR, '"Harvest Now" button detected. Clicking...');
            }
        });

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

        setInterval(() => {
            if (isTreeFull()) {
                clickElement(TREE_SELECTOR, 'Tree is full. Clicking (interval fallback)...');
            } else {
                clickElement(HARVEST_BUTTON_SELECTOR, '"Harvest Now" button detected (interval fallback). Clicking...');
            }
        }, 10 * 1000);
    }

    function handleRedirect() {
        console.log(`[Knolix Bot] Not on homepage. Redirecting in ${REDIRECT_DELAY_MS / 1000}s...`);
        setTimeout(() => {
            window.location.href = HOMEPAGE_PATH;
        }, REDIRECT_DELAY_MS);
    }

    if (location.pathname === HOMEPAGE_PATH) {
        handleHomePage();
    } else {
        handleRedirect();
    }
})();