// ==UserScript==
// @name         DexScreen-GetTran-HasStartDate
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://solscan.io/account/.*#defiactivities
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none

// ==/UserScript==


function funcDownload(content, filename) {
    console.log('begin download');
    // 创建隐藏的可下载链接
    var eleLink = document.createElement('a');
    eleLink.download = filename;
    eleLink.style.display = 'none';
    // 字符内容转变成blob地址
    var 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
};




function get_thead_arr(_thead) {

    let titlearr = _thead.innerText.trim().split(/\r?\n/);
    titlearr = ['DATE', 'TYPE', 'USD', 'Token', 'SOL', 'PRICE', 'MAKER', 'TXN'];
    return titlearr;
}





var res = [];
// var simres = [];
// window.addEventListener('load', () => {

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

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

    var button_tran = document.createElement("button");//创建 获取toptran一个按钮

    mydivbox.appendChild(button_tran);

    button_tran.className = "tam-mybutton-gettran"
    button_tran.textContent = "getswap"; //按钮内容
    button_tran.style.width = "90px"; //按钮宽度
    //button.style.height = "28px"; //按钮高度
    button_tran.style.align = "center"; //文本居中
    button_tran.style.color = "blue"; //按钮文字颜色
    button_tran.style.background = "#e33e33"; //按钮底色
    button_tran.style.border = "1px solid #e33e33"; //边框属性
    button_tran.addEventListener("click", clickButton); //监听按钮点击事件

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

    async function get_page_data() {


        let table_ = document.querySelectorAll("table.w-full")[0];
        let thead_ = table_.getElementsByTagName('thead')[0];
        let arr_th_ = Array.from(thead_.getElementsByTagName('th'));
        // arr_th_长度为8 第一个是空
        let time_click_ = arr_th_[2].querySelectorAll("div.items-center.cursor-pointer")[0];
        // await handle_thead(arr_th_);
        if (arr_th_[2].innerText.trim() == "Time") {
            time_click_.click();
            await sleep(1000);
        }


        let data_res = [];
        let tbody_ = table_.getElementsByTagName('tbody')[0];
        function get_tobody_data(tbody_) {
            let arr_trs_datares = [];
            let arr_trs = Array.from(tbody_.querySelectorAll("tr"));
            for (let idx_tr_ = 0; idx_tr_ < arr_trs.length; idx_tr_++) {
                let arr_tds_datares = new Array(13);
                arr_trs_datares.push(arr_tds_datares);
                let tr_ = arr_trs[idx_tr_];
                let arr_tds = Array.from(tr_.querySelectorAll("td"));
                for (let idx_td_ = 0; idx_td_ < arr_tds.length; idx_td_++) {
                    let td_ = arr_tds[idx_td_];
                    if (idx_td_ == 0) {
                        continue
                    }
                    else if (idx_td_ == 1) {
                        let txhash = td_.innerText.trim();
                        arr_tds_datares[0] = txhash;
                    } else if (idx_td_ == 2) {
                        let date = td_.innerText.trim();
                        arr_tds_datares[1] = date;
                        arr_tds_datares[2] = new Date(date).valueOf();

                    } else if (idx_td_ == 3) {
                        let action_type = td_.innerText.trim();
                        if (action_type.indexOf('SWAP') != -1) {
                            action_type = "SWAP";
                        }
                        arr_tds_datares[3] = action_type;
                    } else if (idx_td_ == 4) {
                        let from = td_.innerText.trim();
                        arr_tds_datares[4] = from;
                    } else if (idx_td_ == 5) {
                        let arr_tokenname_amount = td_.innerText.trim().replace(/,/, '').split('\n');
                        let arr_tokenlink = Array.from(td_.querySelectorAll("a"));
                        let swapin_tokenaddress = arr_tokenlink[0].href.trim().split('/').slice(-1)[0];
                        let swapin_tokenname = arr_tokenname_amount[1];
                        let swapin_tokenamount = arr_tokenname_amount[0];
                        let swapout_tokenaddress = arr_tokenlink[1].href.trim().split('/').slice(-1)[0];
                        let swapout_tokenname = arr_tokenname_amount[3];
                        let swapout_tokenamount = arr_tokenname_amount[2];

                        arr_tds_datares[5] = swapin_tokenaddress;
                        arr_tds_datares[6] = swapout_tokenaddress;
                        arr_tds_datares[7] = swapin_tokenname;
                        arr_tds_datares[8] = swapout_tokenname;
                        arr_tds_datares[9] = swapin_tokenamount;
                        arr_tds_datares[10] = swapout_tokenamount;

                    } else if (idx_td_ == 6) {
                        let router_link = td_.querySelectorAll("a")[0].href.trim().split('/').slice(-1)[0];
                        arr_tds_datares[11] = router_link;
                    }
                    else if (idx_td_ == 7) {
                        // link 是pool的地址 每一给pool都不一样 无法确实是raydium pool 还是 juypool的 所以用图片来确定
                        let arr_pool_img = Array.from(td_.querySelectorAll("img"));
                        for (let idx_pool_img = 0; idx_pool_img < arr_pool_img.length; idx_pool_img++) {
                            arr_pool_img[idx_pool_img] = arr_pool_img[idx_pool_img].src.trim().split('=').slice(-1)[0];
                        }
                        if (arr_pool_img.includes("68747470733a2f2f737461746963732e736f6c7363616e2e696f2f65782d696d672f3637356b5058394d48546a53327a7431716672314e5948757a654c5866514d394832347746535574314d70382e706e67")) {
                            // 含有 raydium 的图片
                            arr_pool_img = 1;
                        } else {
                            arr_pool_img = 0;
                        }
                        arr_tds_datares[12] = arr_pool_img;

                    }
                }

            }

            return arr_trs_datares;

        }
        let arr_title = ['txhash', 'date', 'timestamp', 'action', 'from', 'swapin_tokenaddress',
            'swapout_tokenaddress',
            'swapin_tokenname',
            'swapout_tokenname',
            'swapin_tokenamount',
            'swapout_tokenamount',
            'router',
            'israydium'

        ]

        let arr_trs_datares = get_tobody_data(tbody_);
        data_res = [...arr_title, ...arr_trs_datares];
        return data_res;


    }

})();

function get_page_element() {


    let div_select_page = document.querySelectorAll("div.items-center.justify-end")[0];
    let button_num_onepage = div_select_page.querySelectorAll('button[type="button"].border-input')[0];
    let arr_button_left_right = div_select_page.querySelectorAll("button.inline-flex.items-center.justify-center.whitespace-nowrap");
    let button_pre_onepage = arr_button_left_right[1];
    let button_aft_onepage = arr_button_left_right[2];

}

async function get_page_data() {


    let table_ = document.querySelectorAll("table.w-full")[0];
    let thead_ = table_.getElementsByTagName('thead')[0];
    let arr_th_ = Array.from(thead_.getElementsByTagName('th'));
    // arr_th_长度为8 第一个是空
    let time_click_ = arr_th_[2].querySelectorAll("div.items-center.cursor-pointer")[0];
    // await handle_thead(arr_th_);
    if (arr_th_[2].innerText.trim() == "Time") {
        time_click_.click();
        await sleep(1000);
    }


    let data_res = [];
    let tbody_ = table_.getElementsByTagName('tbody')[0];
    function get_tobody_data(tbody_) {
        let arr_trs_datares = [];
        let arr_trs = Array.from(tbody_.querySelectorAll("tr"));
        for (let idx_tr_ = 0; idx_tr_ < arr_trs.length; idx_tr_++) {
            let arr_tds_datares = new Array(13);
            arr_trs_datares.push(arr_tds_datares);
            let tr_ = arr_trs[idx_tr_];
            let arr_tds = Array.from(tr_.querySelectorAll("td"));
            for (let idx_td_ = 0; idx_td_ < arr_tds.length; idx_td_++) {
                let td_ = arr_tds[idx_td_];
                if (idx_td_ == 0) {
                    continue
                }
                else if (idx_td_ == 1) {
                    let txhash = td_.innerText.trim();
                    arr_tds_datares[0] = txhash;
                } else if (idx_td_ == 2) {
                    let date = td_.innerText.trim();
                    arr_tds_datares[1] = date;
                    arr_tds_datares[2] = new Date(date).valueOf();

                } else if (idx_td_ == 3) {
                    let action_type = td_.innerText.trim();
                    if (action_type.indexOf('SWAP') != -1) {
                        action_type = "SWAP";
                    }
                    arr_tds_datares[3] = action_type;
                } else if (idx_td_ == 4) {
                    let from = td_.innerText.trim();
                    arr_tds_datares[4] = from;
                } else if (idx_td_ == 5) {
                    let arr_tokenname_amount = td_.innerText.trim().replace(/,/, '').split('\n');
                    let arr_tokenlink = Array.from(td_.querySelectorAll("a"));
                    let swapin_tokenaddress = arr_tokenlink[0].href.trim().split('/').slice(-1)[0];
                    let swapin_tokenname = arr_tokenname_amount[1];
                    let swapin_tokenamount = arr_tokenname_amount[0];
                    let swapout_tokenaddress = arr_tokenlink[1].href.trim().split('/').slice(-1)[0];
                    let swapout_tokenname = arr_tokenname_amount[3];
                    let swapout_tokenamount = arr_tokenname_amount[2];

                    arr_tds_datares[5] = swapin_tokenaddress;
                    arr_tds_datares[6] = swapout_tokenaddress;
                    arr_tds_datares[7] = swapin_tokenname;
                    arr_tds_datares[8] = swapout_tokenname;
                    arr_tds_datares[9] = swapin_tokenamount;
                    arr_tds_datares[10] = swapout_tokenamount;

                } else if (idx_td_ == 6) {
                    let router_link = td_.querySelectorAll("a")[0].href.trim().split('/').slice(-1)[0];
                    arr_tds_datares[11] = router_link;
                }
                else if (idx_td_ == 7) {
                    // link 是pool的地址 每一给pool都不一样 无法确实是raydium pool 还是 juypool的 所以用图片来确定
                    let arr_pool_img = Array.from(td_.querySelectorAll("img"));
                    for (let idx_pool_img = 0; idx_pool_img < arr_pool_img.length; idx_pool_img++) {
                        arr_pool_img[idx_pool_img] = arr_pool_img[idx_pool_img].src.trim().split('=').slice(-1)[0];
                    }
                    if (arr_pool_img.includes("68747470733a2f2f737461746963732e736f6c7363616e2e696f2f65782d696d672f3637356b5058394d48546a53327a7431716672314e5948757a654c5866514d394832347746535574314d70382e706e67")) {
                        // 含有 raydium 的图片
                        arr_pool_img = 1;
                    } else {
                        arr_pool_img = 0;
                    }
                    arr_tds_datares[12] = arr_pool_img;

                }
            }

        }

        return arr_trs_datares;

    }
    let arr_title = ['txhash', 'date', 'timestamp', 'action', 'from', 'swapin_tokenaddress',
        'swapout_tokenaddress',
        'swapin_tokenname',
        'swapout_tokenname',
        'swapin_tokenamount',
        'swapout_tokenamount',
        'router',
        'israydium'

    ]

    let arr_trs_datares = get_tobody_data(tbody_);
    data_res = [...arr_title, ...arr_trs_datares];
    return data_res;


}

