Tampermonkey 油猴指令碼例子2
阿新 • • 發佈:2021-06-11
// ==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="width: 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','width: 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([\s\S]*)propertyMemoMap/)[1] skustr = skustr.match(/({"[\s\S]*}})/)[1] //console.info(skustr) let memostr = content.match(/propertyMemoMap([\s\S]*?)\);/)[1] memostr = memostr.match(/({"[\s\S]*"})/)[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="width: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(); })();