// ==UserScript==
// @name         get_filter_pump_token_gmgn
// @namespace    http://tampermonkey.net/
// @version      2025-02-22
// @description  try to take over the world!
// @author       You
// @match        https://gmgn.ai/meme/9Z7Wz20p?chain=sol&tab=complete
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gmgn.ai
// @grant        none
// ==/UserScript==


// https://gmgn.ai/meme/9Z7Wz20p?chain=sol
// https://gmgn.ai/meme/9Z7Wz20p?chain=sol&tab=complete

function funcDownload(content, filename) {
    // 创建隐藏的可下载链接
    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 is_getting_data = false;

var pre_click_timestamp = 0;
var pre_save_data_timestamp = 0;
var now_timestamp = 0;



async function click_avoid_sleep() {
    // console.log("enter click_avoid_sleep");

    if (is_getting_data == true) {
        return;
    }
    if (now_timestamp - pre_click_timestamp >= 1 * 60 * 1000) {
        // if (now_timestamp - pre_click_timestamp >= 20 * 1000) {
        pre_click_timestamp = now_timestamp;
        // let th = document.querySelector("div.ds-dex-table-th");

        let ele_th = document.querySelectorAll('div.g-table-header thead tr th.g-table-cell')[5];

        // let button_click = th.children[5].querySelector("button");
        ele_th.click();
    }

}




async function get_tbody_data() {
    // console.log("get_tbody_data")

    now_timestamp = Date.now();

    if (is_getting_data == true) {


        return;
    }


    if (now_timestamp - pre_save_data_timestamp >= 1 * 60 * 60 * 1000) {
        // if (now_timestamp - pre_save_data_timestamp >= 60 * 1000) {
        pre_save_data_timestamp = now_timestamp;
    } else {
        return;
    }



    is_getting_data = true;
    for (let i = 0; i < 5; i++) {
        // console.log('scoll bottom i=', i);
        let tbody = document.querySelector('div.g-table-tbody-virtual.g-table-tbody  div.g-table-tbody-virtual-holder div.g-table-tbody-virtual-holder-inner')


        let arr_token_row = Array.from(tbody.children);
        await get_current_view_data(arr_token_row);


        let top_butttom = arr_token_row[arr_token_row.length - 1].getBoundingClientRect().top;
        // console.log('-1 ', arr_token_row[parseInt(arr_token_row.length - 1)].innerText.split(/\s+/)[0])

        let new_scrollTop = top_butttom;

        let ele_scroll = document.querySelector(" div.g-table-tbody-virtual.g-table-tbody  div.g-table-tbody-virtual-holder");

        let bef_sTop = ele_scroll.scrollTop;
        ele_scroll.scrollTop = new_scrollTop;
        await sleep(500);
        let aft_sTop = ele_scroll.scrollTop;
        console.log(bef_sTop, aft_sTop);


        if (bef_sTop >= aft_sTop) {
            break;
        }
    }


    for (let i = 0; i < 3; i++) {
        let ele_scroll = document.querySelector(" div.g-table-tbody-virtual.g-table-tbody  div.g-table-tbody-virtual-holder");

        ele_scroll.scrollTop = 0;

        await sleep(500);
    }
    is_getting_data = false;

    save_resdata();

}
function keepAlnum_for_gmgn(string) {
    return string.replace(/[^a-zA-Z0-9\.\s\+\-]/g, ' ');
}
async function get_current_view_data(arr_token_row) {

    for (let idx = 0; idx < arr_token_row.length; idx++) {
        let arr_cur = [];
        let arr_td = Array.from(arr_token_row[idx].children);
        for (let idx2 = 0; idx2 < arr_td.length; idx2++) {
            if (idx2 == 0) {
                // Token
                let a_ele = arr_td[idx2].querySelector('a');
                // arr_cur.push(arr_td[idx2].querySelector('a').href);
                let url_ = a_ele.href;
                let title = a_ele.querySelector('[title]').getAttribute('title');
                title = keepAlnum_for_gmgn(title);

                // title = "RONASZN"
                arr_cur.push(url_);
                arr_cur.push(title);


            }
            else if (idx2 == 1) {
                // Age
                arr_cur.push(arr_td[idx2].innerText);
            }
            else if (idx2 == 2) {
                // Liq /initial

            }
            else if (idx2 == 3) {
                //  MC
                arr_cur.push(arr_td[idx2].innerText);
            }
            else if (idx2 == 4) {
                // holders
                arr_cur.push(arr_td[idx2].innerText);
            }
            else if (idx2 == 5) {
                // 24txns

            }
            else if (idx2 == 6) {
                // 24vol

            }
            else if (idx2 == 7) {
                // price

            } else if (idx2 == 8) {
                // 1m%
            }
            else if (idx2 == 9) {
                // 5m%

            }
            else if (idx2 == 10) {
                // 1h%

            } else if (idx2 == 11) {
                // degen audit

                //  'nomint', 'blacklist', 'burnt', 'top 10', 'insiders'

                let arr_ = arr_td[idx2].innerText.split(/\s+/);

                arr_cur.push(arr_[0]);
                arr_cur.push(arr_[2]);
                arr_cur.push(arr_[4]);
                arr_cur.push(arr_[6]);
                arr_cur.push(arr_[9]);
            } else if (idx2 == 12) {
                // dev

                arr_cur.push(arr_td[idx2].innerText.trim().replace(/[\n-]+/g, '_'));
            }

        }
        arr_res_data.push(arr_cur);

    }

}


function save_resdata() {

    // 一个小时保存一次

    if (arr_res_data.length == 0) {
        // res 为空
        // console.log("arr_res_data length is 0");
        return;
    }


    let arr_title = ['token_url', 'token_name', 'age', 'mc', 'holders', 'nomint', 'blacklist', 'burnt', 'top 10', 'insiders', 'dev'];


    arr_res_data.unshift(arr_title);
    arr_res_data.forEach((val, index, arr) => { arr[index] = val.join() + "\n" });

    let str_cur_time = "" + Date.now();
    funcDownload(arr_res_data.join(""), str_cur_time + "_gmgn_filter" + ".csv");
    arr_res_data = [];

}

function autoScroll() {
    console.log("enter autoScroll");
    async function fetchData() {
        await get_tbody_data();
        await click_avoid_sleep()
        setTimeout(fetchData, 20 * 1000);
    }

    setTimeout(fetchData, 20 * 1000);

}


var arr_res_data = [];

// window.addEventListener('load', () => {

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

    let btn_close = document.querySelector('body div.css-12rtj2z div.css-pt4g3d');


    if (btn_close) {

        await btn_close.click();
    }


    autoScroll();


})();




