panhaiyang / autoBuy

// ==UserScript==
// @name         autoBuy
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  自动购买
// @author       你们最爱的潘大大
// @match           https://www.xiaoniu88.com/product/bid/detail/*?*autobuy
// @grant        none
// @license		MIT
// ==/UserScript==
//自动购买有风险,投资需警慎
var obj={
	beginBuy:function(){
		var ye = Number($('span.g-fl').text().replace(',', ''));
		var syje = Number($('span.f-rest').text().replace(',', ''));
		if (ye > syje) {
			$('.subtract').click()
		} else {
			//可投资额
			var canTz = Math.floor(ye / 100) * 100;
			$('#buyMoney').val(canTz);
		}
		$('.to-buy').click();
		//obj.clearWenjuan();
		var iconClock=setInterval(function() {
			if($('.rate-list').length>0){//显示正常出借窗口
				if($('.icon-common-checkbox-selected').length>0){
					clearInterval(iconClock);
					$('.ensure').click();
					console.log('确认出借')
				}else{
					$('.icon-common-checkbox').click();
				}
			}else if($('.wenjuanCon').length>0 ){//显示的是问卷窗口
              //快速提交问卷
              $('._qList').each(function(){
                  $(this).find('.opt-list:last input').attr("checked","checked")
              });
               $("._Submit").click();
				$('.ZebraDialog_Close').click();
				$('.to-buy').click();
			}
		}, 100)
	},
	checkBuy:function(){
		var ye = Number($('span.g-fl').text().replace(',', ''));
		if(ye>0) return true;
		else return false;
	},
	checkContent:function(){
		if(document.getElementById('shoulderModel')) return true;
		else return false;
	},
	checkEmpty:function(){
		if(document.getElementsByClassName('data-empty').length>0) return true;
		else return false;
	},
	//检查问卷调查
	/* clearWenjuan:function(){
		if($('.wenjuanCon').length>0) {
			$('.ZebraDialog_Close').click();
			$('.to-buy').click();
		}
	}, */
	toBuy:function(){
		if(obj.checkBuy()){
			obj.beginBuy();
		}else{
          window.close();
		}
	}
}
var checkTime=0;
var clock=setInterval(function(){
    //判断是否404
    if(document.querySelector('h1')&&document.querySelector('h1').innerText=='404 Not Found'){
      location.reload();
    }
  //另外404
  if(document.getElementsByClassName('bg404').length>0){
     location.reload();
  }else{
    console.log("通过404检测")
  }
     
    if(document.getElementById('main-message')&&document.getElementById('main-message').innerText.indexOf('找不到')>-1)
    location.reload();
  //判断500
  if(document.getElementsByClassName('bg500').length>0){
     location.reload();
  }else{
    console.log("通过500检测")
  }
	//检查页面是否刷完
	if(obj.checkContent()){
		clearInterval(clock);
		console.log('开始购买')
		obj.toBuy();
	}else{
		if(checkTime>10||obj.checkEmpty()){
			location.reload();
		}
		checkTime++;
	}
},500);