天天基金数据抽取

用以快捷显示累计净值,任期回报,一年前累计净值

目前为 2021-06-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         天天基金数据抽取
// @namespace    http://tampermonkey.net/
// @version      1.1.0
// @description  用以快捷显示累计净值,任期回报,一年前累计净值
// @author       aotmd
// @match        http://fund.eastmoney.com/*
// @match        http://*.eastmoney.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// ==/UserScript==

(function () {
    addLoadEvent(() => {
        var url = window.location.href;
        //主页面
        var str = /fund\.eastmoney\.com\/.+/i;
        //净值页面
        var str2 = /.+eastmoney\.com.+/i;

        if (str.test(url)) {
            window.setTimeout(() => {
                //显示出来
                let a1 = document.createElement('div');
                a1.className = "gs1";
                document.body.appendChild(a1);
                var 任期回报 = (document.querySelector("tr.noBorder > td.td04.bold.ui-color-red").innerText.replace("%","")/100).toFixed(4);
                var 任职时间 = document.querySelector("tr.noBorder > td.td01").innerText.replace("~至今","");
                var 累计净值 = document.querySelector("dd.dataNums > span.ui-color-red").innerText;
                var 净值时间 = document.querySelector("dl.dataItem02 > dt > p").innerText.split('(')[1].replace(')', '');
                //取得指定股票代码
                var 股票代码=window.location.pathname.replace("/","").replace(".html","");
                var 净值网址=document.querySelector("#Div2 > div.item_title.hd.eye-protector-processed > div.item_more > a").href;
                //打开新窗口,获取净值
                window.open(净值网址+"?ref="+股票代码, '_blank',
                    "height=100,width=100,top="+window.screen.height+",left="+window.screen.width+",location=0,menubar=0,status=0,titlebar=0,toolbar=0");
                //持续监听变化
                var flag="";
                window.setInterval(()=>{
                    var 一年前的时间=GM_getValue(股票代码+"时间");
                    var 一年前累计净值=GM_getValue(股票代码+"净值");
                    if (flag===一年前累计净值)return;
                    flag=一年前累计净值;
                    a1.innerHTML = "任职时间:" + 任职时间 + "<br>净值时间:" + 净值时间 + "<br>任期回报:&nbsp;&nbsp;&nbsp;&nbsp;" +
                        任期回报 + "<br>累计净值:&nbsp;&nbsp;&nbsp;&nbsp;" + 累计净值 + "<br>一年前:<br>累计净值:&nbsp;&nbsp;&nbsp;&nbsp;"+
                        一年前累计净值+ "<br>取值时间:"+一年前的时间;
                });

            }, 0);
        }else if (str2.test(url)&&getQueryString("ref")!=null){
            var ref=getQueryString("ref");
            window.setTimeout(() => {
                document.querySelector("#pagebar > div.pagebtns > input.pnum").value=13;
                document.querySelector("#pagebar > div.pagebtns > input.pgo").click();
                window.setTimeout(() => {
                    var 时间=document.querySelector("#jztable > table > tbody > tr:nth-child(3) > td:nth-child(1)").innerText;
                    var 净值=document.querySelector("#jztable > table > tbody > tr:nth-child(3) > td:nth-child(3)").innerText;
                    GM_setValue(ref+"时间", 时间);
                    GM_setValue(ref+"净值", 净值);
                    window.close();
                },500);
            },0);
        }
    });

    addStyle(`
    .gs1 {
        padding: 5px 5px;
        font-size: 12px;
        color: snow;
        position: fixed;
        text-align: left;
        cursor: copy;
        border-radius: 10px;
        background-color: rgba(147, 146, 234, 0.87);
        right: 1%;
        top: 15%;
        z-index: 999999;
        box-shadow: 0 0 7px 0 rgba(18, 80, 18,0.4), 0 0 0 1px rgba(0,0,0,0.3);
    }`);

    /**
     * 添加浏览器执行事件
     * @param func 无参匿名函数
     */
    function addLoadEvent(func) {
        let oldOnload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function () {
                try {
                    oldOnload();
                } catch (e) {
                    console.log(e);
                } finally {
                    func();
                }
            }
        }
    }

    /**
     * 获取网页参数
     * @param name 参数名称
     * @returns {string|null}
     */
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = decodeURI(window.location.search).substr(1).match(reg);
        if(r != null) return (r[2]);
        return null;
    }

    //添加css样式
    function addStyle(rules) {
        let styleElement = document.createElement('style');
        styleElement["type"] = 'text/css';
        document.getElementsByTagName('head')[0].appendChild(styleElement);
        styleElement.appendChild(document.createTextNode(rules));
    }
})();