Tampermonkey 油猴脚本例子2

// ==UserScript==
// @name         提取淘宝SKU
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       丛兴龙
// @match        https://item.taobao.com/item.htm?*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    console.info("脚本启动 提取淘宝SKU")
    function init(){
        //获取body
        let vbody = document.getElementsByTagName('body')[0]
        //插入按钮
        let vdiv = document.createElement('div')
        vdiv.setAttribute('id','mydiv')
        vdiv.setAttribute('class','col-sm-12')
        //vbody.appendChild(vdiv)
        document.querySelector("div").appendChild(vdiv)
        vdiv.innerHTML += '  '
        vdiv.innerHTML += '<button onclick="document.myscript_f1()" type="button" style=" 100%;height: 50px; z-index: 1000;">提取SKU</button>'
        console.info("插入按钮 完成")
        //插入消息框
        let vmsg = document.createElement('div')
        vmsg.setAttribute('id','mymsg')
        vmsg.setAttribute('class','col-sm-12')
        vmsg.setAttribute('style',' 100%;padding:5px;padding-left:50px')
        document.querySelector("div").appendChild(vmsg)
        vmsg.innerHTML = ''
        console.info("插入消息框 完成")
        //插入自定义方法
        document.myscript_f1 = function(){
            let vscripts = document.getElementsByTagName('script')
            //console.info('total',vscripts.length)
            for(let i = 0;i<vscripts.length;i++){
                let content = vscripts[i].innerHTML
                if (content.indexOf('skuMap')>0){
                    //console.info(i)
                    let skustr = content.match(/skuMap([sS]*)propertyMemoMap/)[1]
                    skustr = skustr.match(/({"[sS]*}})/)[1]
                    //console.info(skustr)
                    let memostr = content.match(/propertyMemoMap([sS]*?));/)[1]
                    memostr = memostr.match(/({"[sS]*"})/)[1]
                    //console.info(memostr)

                    let skumap = JSON.parse(skustr);
                    //console.info(skumap)
                    let memomap = JSON.parse(memostr);
                    //console.info(memomap)

                    //筛选结果

                    let result = []
                    for(let key in memomap){
                        if(memomap[key].indexOf('3060')>0||memomap[key].indexOf('3070')>0){
                            let tmpdict={}
                            tmpdict.sku=memomap[key]
                            tmpdict.price=skumap[';'+key+';'].price
                            result.push(tmpdict)
                        }
                    }
                    result = result.sort(function(a,b){
                        return a.price - b.price
                        })
                    console.info(result)
                    let msg = ''
                    for(let j =0;j<result.length;j++)
                    {
                        msg += '<p style="padding-left:50px">'
                        msg += '<span style="300px"> '
                        msg += result[j].price
                        msg += ' </span>'
                        msg += '<span> '
                        msg += result[j].sku
                        msg += ' </span>'
                        msg += '</p>'
                    }
                    console.info(msg)
                    document.myscript_showMsg(msg)
                }
            }
        }

        //以文件形式下载内容
        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)
        }
        document.myscript_showMsg=function(msg){
            let vmsg = document.getElementById('mymsg')
            vmsg.innerHTML = msg
        }
    };

    init();
})();
原文地址:https://www.cnblogs.com/congxinglong/p/14875034.html