if (typeof $ === 'function'&& typeof(isSpiderPage)==='undefined') {
    $.ajaxPrefilter(function (options, orginOption, jqXHR) {
        var beforeCallBack = options.success;
        var beforeSettring = orginOption;
        options.success = function (r) {
            if (r.antiVerify) {
                if (r.antiVerify === 1) {
                    antiReload(); //重新请求数据
                    //移除验证码
                    var antiVerify = document.querySelector("#antiVerify");
                    antiVerify.parentNode.removeChild(antiVerify);
                } else {
                    window.nextSetting = beforeSettring;//记录请求
                    shwoCode(r.textVerify, r.imgData);//验证码展示
                }
            } else if (r.antiJump) {
                window.location.href = r.href;
            } else {
                if (r.antiData && typeof (antiDecode) !== "function") {//等待初始化完毕
                    var localInterval = setInterval(function () {
                        if (typeof (antiDecode) === "function") {
                            r = JSON.parse(antiDecode(r.antiData))
                            if (beforeCallBack) beforeCallBack(r)
                            clearInterval(localInterval)
                        }
                    }, 200)
                } else {
                    if (r.antiData) {
                        r = JSON.parse(antiDecode(r.antiData))
                    }
                    if (beforeCallBack) beforeCallBack(r)
                }
            }
        }
    })
}


function antiReload(flag) {
    try {
        if (flag === 1) {
            window.nextSetting.data.antiVerifyCheck = document.querySelector("#antivalue").value.substr(1);
            window.nextSetting.data.imgw = document.querySelector("#antiimg").width;
        } else {
            if (window.nextSetting.data) {
                delete window.nextSetting.data.antiVerifyCheck;
            }
        }
    } catch (e) {
        console.log(e)
    }
    $.ajax(window.nextSetting)
}


function shwoCode(text, imgdata) {
    var htmlcode = '<div style="width: 100%;height: 100%;position:absolute;top:0;background-color: #382f3d;opacity: .1;z-index: 99999999999990;"></div>'
        + '<div style="position: fixed;top: 50%;left: 50%;transform: translateX(-50%) translateY(-50%);z-index: 999999999999990;max-width: 360px;">'
        + '<div>'
        + '<img style="width: 90vw;max-width: 360px;display: block;" src="/antiRes/images/verify_logo.png">'
        + '</div>'
        + '<div style="border: #F5F5F5 solid 1px;margin: auto;width: 90vw;max-width: 360px;background-color: #FFFFFF;box-shadow: 1px 1px 1px 1px grey;padding: 10pt;display: flex;flex-direction: column;">'
        + '<div style="margin-bottom:8pt"><div>请在下图依次点击：<span>' + text + '</span></div></div>'
        + '<div style="position:relative;width:100%">'
        + '<img id="antiimg" onclick="antiAdd(event,this);" src="data:image/png;base64,' + imgdata + '" style="width:100%">'
        + '<input type="hidden" id="antivalue" value="">'
        + '</div>'
        + '<div style="margin-top: 1vh;display: flex;flex-direction: row;justify-content: space-between;">'
        + '<img style="argin-left: 8pt;width: 25pt;height: 25pt;float: left;cursor: hand;" onclick="antiReload()" src="/antiRes/images/flush.png" \>'
        + '<div><button style="background-color: #24C0D7;text-align: center;vertical-align: middle;touch-action: manipulation;cursor: pointer;background-image: none;border: 1px solid transparent;white-space: nowrap;padding: 8px 12px;font-size: 14px;line-height: 1.42857143;border-radius: 4px;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;color: #FFFFFF;position: relative;outline-width: 0px;box-shadow: none !important;" onclick="antiReload(1);">确定</button></div>'
        + '</div>'
        + '</div>'
        + '</div>'
    if (document.querySelector("#antiVerify") == null) {
        var wrap = document.createElement("div");
        wrap.id = "antiVerify";
        wrap.innerHTML = htmlcode;
        var insertParent = document.body
        if (document.querySelector("#supersearchPage") && document.querySelector("#supersearchPage").className !== "hidden") {//搜索首页验证码要在搜索页内
            insertParent = document.querySelector("#supersearchPage")
        } else if (document.querySelector("#entsearchPage") && document.querySelector("#entsearchPage").className !== "hidden") {
            insertParent = document.querySelector("#entsearchPage")
        }
        insertParent.appendChild(wrap);
    } else {
        document.querySelector("#antiVerify").innerHTML = htmlcode;
    }
    document.querySelector("#antiVerify div").addEventListener('touchmove', function (event) {
        event.preventDefault();
    })
}

function antiAdd(event, obj) {
    if (obj.parentNode.querySelectorAll(".imgs").length < 3) {
        var offsetX = event.clientX - obj.getBoundingClientRect().left;
        var offsetY = event.clientY - obj.getBoundingClientRect().top;
        var offx = parseInt(offsetX);
        var offy = parseInt(offsetY);
        var icon = "<img onclick='antiRemove(this)' class='imgs' src='/antiRes/images/hoverclick.png' "
            + "style='position:absolute;top:" + (offsetY - 8) + "px;left:" + (offsetX - 8) + "px;'   offx=" + offx + " offy=" + offy + " />";
        obj.parentNode.innerHTML += icon;
        document.querySelector("#antivalue").value += (";" + offx + "," + offy)
    }
}

function antiRemove(obj) {
    var offx = obj.getAttribute("offx");
    var offy = obj.getAttribute("offy");
    document.querySelector("#antivalue").value = document.querySelector("#antivalue").value.replace((";" + offx + "," + offy), "");
    obj.parentNode.removeChild(obj);
}
