其他分享
首页 > 其他分享> > Tampermonkey 油猴脚本例子

Tampermonkey 油猴脚本例子

作者:互联网

// ==UserScript==
// @name         数据召测
// @namespace    http://tampermonkey.net/
// @version      0.1.0303
// @description  try to take over the world!
// @author       You
// @match        http://0.0.0.0/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    console.info("油猴脚本启动");
    function init(){
        //console.info("init");
        let vbody = document.getElementsByTagName('body')[0]
        let vdiv = document.createElement('div')
        vdiv.setAttribute('id','cxldiv')
        vdiv.setAttribute('class','col-sm-12')
        //vbody.appendChild(vdiv)
        document.querySelector("div").appendChild(vdiv)
        vdiv.innerHTML += '  '
        //vdiv.innerHTML += '<input id="cxl_input_cons_no" type="text">'
        vdiv.innerHTML += '<button type="button" onclick="document.myscript_selected()">选记录</button>'
        vdiv.innerHTML += '<button type="button" onclick="document.myscript_changeDataItem()">选数据项</button>'
        vdiv.innerHTML += '<button type="button" onclick="document.myscript_getData()">召测</button>'
        vdiv.innerHTML += '<button id="cxlbtn_save" type="button" onclick="document.myscript_saveResult()">保存结果</button>'
        //选电表
        document.myscript_selected = function(){
            //console.info("myscript_myzc")
            //选记录
            document.getElementsByName('btSelectItem')[0].click()
            //选直抄
            document.getElementsByName('taskKind')[1].click()
            //存库
            document.getElementById('saveDb').click()
        }
        //选数据项
        document.myscript_changeDataItem = function(){
            //console.info("myscript_changeDataItem")
            //取消正有示值
            document.getElementById('30901006_anchor').click()
            //选数据项
            document.getElementById('30201001_anchor').click()
            document.getElementById('30301001_anchor').click()
            document.getElementById('30601001_anchor').click()
        }
        //召测
        document.myscript_getData = function(){
            document.getElementById('dataCall').click()
            document.querySelector("#cxlbtn_save").removeAttribute('disabled')
            if (document.myjob_save){
                clearInterval(document.myjob_save)
            }
            document.myjob_save = setInterval("document.myscript_saveResult()",2000)

        }
        //保存召测结果
        document.myscript_saveResult = function(){
            console.info("myscript_saveResult")
            const result = document.getElementById('resultDetail').innerHTML.replace(/\t/g,'').replace(/\s\s/g,'').replace(/<tr.+?>/g,'\n').replace(/<.+?>/g,' ')
            if(result)
            {
                console.info(result)
                clearInterval(document.myjob_save)

                const cons_no = result.match(/用户编号\s+\d+/)[0].replace(/用户编号/g,'cn')
                const now = new Date().getTime()
                document.myscript_download(result, cons_no+' '+now+'.txt')
                document.querySelector("#cxlbtn_save").setAttribute('disabled','disabled')
            }
        }
        document.myscript_download=function(content, fileName){
            let downLink = document.createElement('a')
            downLink.download = fileName
            //字符内容转换为blod地址
            let blob = new Blob([content])
            downLink.href = URL.createObjectURL(blob)
            // 链接插入到页面
            document.body.appendChild(downLink)
            downLink.click()
            // 移除下载链接
            document.body.removeChild(downLink)
        }
    }

    init()
})();

标签:vdiv,innerHTML,click,getElementById,myscript,例子,document,Tampermonkey,油猴
来源: https://www.cnblogs.com/congxinglong/p/14780894.html