کدم اینه

let first_usdt = 4;
let has_profit;
let timer_time_out = 100; // ms

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

function show_time() {
    const now = new Date();
    const options = {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false // برای 24 ساعت
    };
    const formattedDateTime = now.toLocaleString('en-GB', options);
    console.log("Current date and time:", formattedDateTime);
}

function find_element(xpathExpression) {
    const result = document.evaluate(
        xpathExpression,
        document,
        null,
        XPathResult.FIRST_ORDERED_NODE_TYPE,
        null
    );
    return result.singleNodeValue;
}

function get_elements() {
    return {
        first_input_element: find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[1]/div/input"),
        secound_input_element: find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[3]/div/input"),
        lis_element: find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[1]/div/div/div[3]/div[1]"),
        matic_element: find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[1]/div/div/div[3]/div[4]"),
        usdt_element: find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[1]/div/div/div[3]/div[5]"),
        near_element: find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[1]/div/div/div[3]/div[6]")
    };
}

function get_transfer_value(secound_input_element) {
    return secound_input_element.value;
}

function click_on_element(element) {
    if (element) {
        element.click();
    }
}

function set_value(element, value_item) {
    if (element) {
        element.value = value_item;
        const event = new Event('input', {
            bubbles: true
        });
        element.dispatchEvent(event);
    }
}

function click_sec_near() {
    click_on_element(find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[3]/div/div/div[3]/div[4]"));
}

function click_sec_usdt() {
    click_on_element(find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[3]/div/div/div[3]/div[2]"));
}

function click_swap_button() {
    click_on_element(find_element("/html/body/div[1]/div[1]/div[2]/div[2]/button"));
}

function click_confirm_button() {
    click_on_element(find_element("/html/body/div[1]/div[1]/div[2]/div[3]/div[1]/div/div/div/div[3]/button[2]"));
}

async function get_name_first_crypto() {
    return find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[1]/div/div/div[1]/div").innerText.toLowerCase();
}

async function get_name_second_crypto() {
    return find_element("/html/body/div[1]/div[1]/div[2]/div[2]/div[3]/div/div/div[1]/div").innerText.toLowerCase();
}

async function check_select_true_crypto(first,name_crypto,element) {
    if (first){
        const first_crypto_name = await get_name_first_crypto();
        if (first_crypto_name != name_crypto){
            click_on_element(element);
            await delay(timer_time_out);
        }
    } else {
        const second_crypto_name = await get_name_second_crypto();
        if (second_crypto_name != name_crypto){
            element();
            await delay(timer_time_out);
        }
    }
}

async function process_data() {
    const elements = get_elements();
    let transfer_value;
    const timer_swap_button = 200; // ms
    const timer_swap = 15000; // ms


    click_on_element(elements.usdt_element);
    set_value(elements.first_input_element, first_usdt);

    await delay(timer_time_out);
    await check_select_true_crypto(true,"usdt",elements.usdt_element);
    transfer_value = get_transfer_value(elements.secound_input_element);
    click_swap_button();

    await delay(timer_swap_button);
    click_confirm_button();

    await delay(timer_swap);
    click_on_element(elements.matic_element);
    set_value(elements.first_input_element, transfer_value);

    await delay(timer_time_out);
    await check_select_true_crypto(true,"matic",elements.matic_element);
    transfer_value = get_transfer_value(elements.secound_input_element);
    click_swap_button();

    await delay(timer_swap_button);
    click_confirm_button();

    await delay(timer_swap);
    click_on_element(elements.lis_element);

    await delay(timer_time_out);
    await check_select_true_crypto(true,"lis",elements.lis_element);
    click_sec_near();
    set_value(elements.first_input_element, transfer_value);

    await delay(timer_time_out);
    await check_select_true_crypto(false,"near",click_sec_near);
    transfer_value = get_transfer_value(elements.secound_input_element);
    click_swap_button();

    await delay(timer_swap_button);
    click_confirm_button();

    await delay(timer_swap);
    click_on_element(elements.near_element);

    await delay(timer_time_out);
    await check_select_true_crypto(true,"near",elements.near_element);
    click_sec_usdt();
    set_value(elements.first_input_element, transfer_value);

    await delay(timer_time_out);
    await check_select_true_crypto(false,"usdt",click_sec_usdt);
    transfer_value = get_transfer_value(elements.secound_input_element);
    click_swap_button();

    await delay(timer_swap_button);
    click_confirm_button();

    await delay(timer_swap);

    console.log("swap success");
    show_time();
}

async function get_all_data(callback) {
    const elements = get_elements();
    let transfer_value;


    click_on_element(elements.usdt_element);
    set_value(elements.first_input_element, first_usdt);

    await delay(timer_time_out);
    await check_select_true_crypto(true,"usdt",elements.usdt_element);
    transfer_value = get_transfer_value(elements.secound_input_element);

    click_on_element(elements.matic_element);
    set_value(elements.first_input_element, transfer_value);

    await delay(timer_time_out);
    await check_select_true_crypto(true,"matic",elements.matic_element);
    transfer_value = get_transfer_value(elements.secound_input_element);

    click_on_element(elements.lis_element);
    await delay(timer_time_out);
    await check_select_true_crypto(true,"lis",elements.lis_element);

    click_sec_near();
    set_value(elements.first_input_element, transfer_value);

    await delay(timer_time_out);
    await check_select_true_crypto(false,"near",click_sec_near);
    transfer_value = get_transfer_value(elements.secound_input_element);

    click_on_element(elements.near_element);
    await delay(timer_time_out);
    await check_select_true_crypto(true,"near",elements.near_element);

    click_sec_usdt();
    set_value(elements.first_input_element, transfer_value);

    await delay(timer_time_out);
    await check_select_true_crypto(false,"usdt",click_sec_usdt);
    transfer_value = get_transfer_value(elements.secound_input_element);

    console.log(first_usdt, transfer_value);
    console.log(parseFloat((((transfer_value / first_usdt) * 100) - 100).toFixed(2)), "%");
    show_time();

    if (first_usdt < transfer_value) {
        has_profit = true;
        await process_data();
    }


    // Callback را در اینجا فراخوانی کنید
    callback(has_profit);
}

async function startDataCollection() {
    has_profit = false;
    const timer_run = 1; // به ثانیه
    const timer_rerun = 1;


    // const checkForProfit = async () => {
    //     await get_all_data((hasProfitResult) => {
    //         if (hasProfitResult) {
    //             setTimeout(checkForProfit, timer_rerun * 1000); // 5 ثانیه
    //         } else {
    //             setTimeout(checkForProfit, timer_run * 1000); // بر اساس timer_run
    //         }
    //     });
    // };

    const checkForProfit = async () => {
        await get_all_data((hasProfitResult) => {
            setTimeout(checkForProfit, 5 * 1000);
        });
    };

    checkForProfit();
}

startDataCollection();

و ارورم اینه VM98:153 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'innerText')
    at get_name_first_crypto (<anonymous>:153:92)
    at check_select_true_crypto (<anonymous>:162:41)
    at process_data (<anonymous>:199:11)
    at async get_all_data (<anonymous>:291:9)
    at async checkForProfit (<anonymous>:316:9)