// ==UserScript==
// @name         GMGN-Get-Top
// @namespace    http://tampermonkey.net/
// @version      2024-10-19
// @description  try to take over the world!
// @author       You
// @match        https://gmgn.ai/sol/token/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gmgn.ai
// @grant        none
// ==/UserScript==


function funcDownload(content, filename) {
    console.log('begin download');
    // 创建隐藏的可下载链接
    let eleLink = document.createElement('a');
    eleLink.download = filename;
    eleLink.style.display = 'none';
    // 字符内容转变成blob地址
    let blob = new Blob([content]);
    eleLink.href = URL.createObjectURL(blob);
    // 触发点击
    document.body.appendChild(eleLink);
    eleLink.click();
    // 然后移除
    document.body.removeChild(eleLink);
}


function sleep(sleepdelay) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(sleepdelay);
        }, sleepdelay);
    })
}
var number_chars_obj = {
    "k": 3,
    "m": 6,
    "b": 9,
    "t": 12,
    "p": 15
};


var number_chars_arr = ["k", "m", "b", "t", "p"];




function get_thead_arr_toptrader() {

    // let titlearr = _thead.innerText.trim().split(/\r?\n/);
    let titlearr = ['Maker', 'SOL', 'Buy_sol','Buy_token', 'Sell_sol', 'Sell_token', 'Pnl_sol', 'Pnl_per'];
    return titlearr;
}

 
 

function autoScroll_toptrader() {
    console.log("enter autoScroll_toptrader");

    let interval;
    let scroll_times = 15;

    // let div_has_tran_table = document.querySelector("div.chakra-tabs__tab-panels.css-2zi8qu");
    // let tbody = div_has_tran_table.querySelector("div.css-1o2kb31 tbody.g-table-tbody");

    div_has_tran_table = document.querySelector("div.chakra-tabs__tab-panels.css-2zi8qu");
    tbody = div_has_tran_table.querySelector("div.ag-body.ag-layout-normal");


    async function down() {
        interval = setInterval(async () => {

            let tbodytrs;

            if (scroll_times > 0) {

                await sleep(150);
                tbodytrs = tbody.children;
                tbodytrs[tbodytrs.length - 1].scrollIntoView({ behavior: "smooth", block: "center" });
                await get_tbody_data_toptrader();


            } else {
                clearInterval(interval);
                
                return;

            }
            scroll_times--;


        

            await sleep(300);
        }, 1200)

    }

    down()

}





var sTop = 0;
var res_data_toptrader = [];
// window.addEventListener('load', async () => {

(async function () {
    console.log("enter load");
    await sleep(6000);
    console.log("create button");

    var mydivbox = document.createElement("div");


    

    var button_get_toptrader = document.createElement("button");//创建 获取 top trader 一个按钮
    button_get_toptrader.className = "tam-mybutton-gettran"
    button_get_toptrader.textContent = "top_sol"; //按钮内容
    button_get_toptrader.style.width = "90px"; //按钮宽度
    button_get_toptrader.style.height = "30px"; //按钮高度
    button_get_toptrader.style.align = "center"; //文本居中
    button_get_toptrader.style.color = "blue"; //按钮文字颜色
    button_get_toptrader.style.background = "#717171"; //按钮底色
    button_get_toptrader.style.border = "1px solid #717171"; //边框属性

    mydivbox.appendChild(button_get_toptrader);

    

    mydivbox.style.position = "fixed";
    mydivbox.style.top = "180px";
    mydivbox.style.right = "40px";
    document.body.appendChild(mydivbox);


    
    button_get_toptrader.addEventListener("click", clickButton_get_toptrader); //监听按钮点击事件



})();

async function clickButton_get_toptrader() {
    res_data_toptrader = [];

    console.log("enter clickButton_toptrader res_data_toptrader=", res_data_toptrader);
    let div_chakra_tabs = document.querySelector('[role="tablist"]');
    let arr_buttons_chakra = Array.from(div_chakra_tabs.querySelectorAll("button"));
    let tab_top_trader_click = undefined;
    for (let button of arr_buttons_chakra) {
        if (button.innerText.includes("Traders")) {
            tab_top_trader_click = button;
            break;
        }
    }
    if (tab_top_trader_click == undefined) {
        console.log("no tab_top_trader_click");
        return;
    }
    console.log("  tab_top_trader_click click=");
    await tab_top_trader_click.click();
    await sleep(7000);

    let div_has_tran_table;

    let _thead;

 
    div_has_tran_table = document.querySelector("div.chakra-tabs__tab-panels.css-2zi8qu");
    _thead = div_has_tran_table.querySelector("div.css-c6kdal div.ag-header-row.ag-header-row-column");
    let arr_th = _thead.children;
    let tab_click_USD_toptrader = arr_th[5].querySelector("div.css-1vimx2u");
    if (tab_click_USD_toptrader.innerText.includes("USD")) {
        await tab_click_USD_toptrader.click();
        await sleep(4000);
    }

    let titlearr = get_thead_arr_toptrader();
    res_data_toptrader.push(titlearr);
    autoScroll_toptrader();

}





