// ==UserScript==
// @name 提取天猫SKU
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author 丛兴龙
// @match https://detail.tmall.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('style','background-color: #fff; 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('skuList')>0){
//console.info(i)
let skuListStr = content.match(/skuList[\s\S]*?(\[[\s\S]*?\])/)[1]
//console.info("skuListStr",skuListStr)
let skuList = JSON.parse(skuListStr)
console.info("skuList",skuList)
let skuMapStr = content.match(/skuMap[\s\S]*?(\{[\s\S]*?\}\})/)[1]
//console.info("skuMapStr",skuMapStr)
let skuMap = JSON.parse(skuMapStr)
console.info("skuMap",skuMap)
let result = []
let skus = document.getElementsByClassName('tm-sale-prop')
console.info('skus',skus)
for (let i=0;i<skus.length;i++){
let sku = skus[i]
console.info('sku',sku)
let items = sku.getElementsByTagName('li')
for (let j=0;j<items.length;j++){
let item = items[j]
let c = item.getAttribute('class')
if ( c && c.indexOf("tb-out-of-stock")>=0){
continue
}
if ( c && c.indexOf("tb-selected")>=0){
}
else{
item.getElementsByTagName('a')[0].click()
}
console.info('item',item)
let prices = document.getElementsByClassName('tm-price')
let price = 0
for (let i = 0 ;i < prices.length;i++){
price = prices[i].innerText
}
let title = item.getAttribute('title')
if (title){
result.push({'title':title,'price':price})
}
}
}
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="background-color: rgba(255,255,255,0.7); width:50%; height:20px; padding-left:50px">'
msg += '<span>'
msg += j+1
msg += '</span>'
msg += '<span> '
msg += result[j].price
msg += ' </span>'
msg += '<span> '
msg += result[j].title
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();
})();