Skip to content

Instantly share code, notes, and snippets.

@fwindpeak
Last active December 11, 2020 12:55
Show Gist options
  • Select an option

  • Save fwindpeak/43dc0c4fbd5ddded7bd7fd171d24f23e to your computer and use it in GitHub Desktop.

Select an option

Save fwindpeak/43dc0c4fbd5ddded7bd7fd171d24f23e to your computer and use it in GitHub Desktop.
userscript for auto buy jd.com item
// ==UserScript==
// @name jd_autobuy
// @namespace http://fwindpeak.github.com
// @version 0.0.1
// @description autobuy jd item
// @author fwindpeak
// @match *://item.jd.com/*.html*
// @match *://marathon.jd.com/seckill/seckill.action*
// @match *://marathon.jd.com/koFail.html*
// @match *://cart.jd.com/addToCart.html*
// @match *://cart.jd.com/cart.action*
// @match *://trade.jd.com/shopping/order/getOrderInfo.action*
// @run-at document-idle
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Your code here...
var REFRESH_TIME = 1000; //自动刷新时间,0为不刷新
function qianggou(time = 0) {
console.log(time);
var handler = null;
var qgbtn = null; //抢购
var ljqgbtn = null; //立即抢购
var jrgucbtn = null; //加入购物车
var btnClass = null; //按钮className
var btnText = null; //按钮提示文字
handler = setInterval(function(){
qgbtn = document.getElementById('choose-btn-ko');
ljqgbtn = document.getElementById('btn-reservation');
jrgucbtn = document.getElementById('InitCartUrl');
if (null != qgbtn) {
btnClass = qgbtn.className;
btnText = qgbtn.text;
} else if (null != ljqgbtn) {
btnClass = ljqgbtn.className;
btnText = ljqgbtn.text;
} else if (null != jrgucbtn) {
btnClass = jrgucbtn.className;
}
if (null != qgbtn) {
//分之一
if (-1 == btnClass.indexOf('disable')) {
if (btnText.indexOf('未开始') > 0) {
if (0 != time) {
setTimeout(function(){
window.location.reload();
},time);
}
}
else {
clearInterval(handler);
qgbtn.click();
}
} else {
if (btnText.indexOf('未开始') > 0) {
if (0 != time) {
setTimeout(function(){
window.location.reload();
},time);
}
}
}
}
else if (null != ljqgbtn) {
//分支二 加购物车逻辑
if (-1 == btnClass.indexOf('disable')) {
if (btnText.indexOf('等待') > 0 || btnText.indexOf('预约') > 0) {
if (0 != time) {
setTimeout(function(){
window.location.reload();
},time);
}
console.log(btnText);
}
else {
clearInterval(handler);
ljqgbtn.click();
}
} else {
if (btnText.indexOf('等待') > 0 || btnText.indexOf('预约') > 0) {
if (0 != time) {
setTimeout(function(){
window.location.reload();
},time);
}
console.log(btnText);
}
}
}
else if (null != jrgucbtn) {
//分支二 加购物车逻辑
if (btnClass.indexOf('disable') > 0) {
clearInterval(handler);
}
clearInterval(handler);
jrgucbtn.click();
}
else {
return ;
}
}, 800);
}
function addtocart(){
var handler = null;
var GotoShoppingCart = null;
handler = setInterval(function(){
GotoShoppingCart = document.getElementById('GotoShoppingCart');
if (null != GotoShoppingCart) {
GotoShoppingCart.click();
clearInterval(handler);
}
}, 400);
}
function jiesuanfukuan(){
var handler = null;
var orderSubmit = null;
//提交订单
handler = setInterval(function(){
orderSubmit = document.getElementById('order-submit');
if (null != orderSubmit) {
orderSubmit.click();
clearInterval(handler);
}
}, 300);
}
function qianggoujiesuan(){
var handler = null;
var orderSubmit = null;
var clicked = 0;
//提交订单
handler = setInterval(function(){
orderSubmit = document.getElementsByClassName('submit-btn')[0];
console.log('qianggoujiesuan',clicked);
if (null != orderSubmit) {
orderSubmit.click();
if(clicked>1){
clearInterval(handler);
}else{
clicked++;
}
}
}, 500);
}
function qianggoushibai(){
var handler = null;
var tryBtn = null;
handler = setInterval(function(){
tryBtn = document.getElementById('tryBtn');
if (null != tryBtn) {
if (confirm('是否重新抢购')) {
tryBtn.click();
clearInterval(handler);
}
else {
clearInterval(handler);
}
}
}, 400);
}
function qianggouzhifu(){
_todo();
setInterval(function(){
_todo();
}, 1000);
function _todo(){
var handlersaveConsigneeTitleDivCount = 0;
var handlersaveConsigneeTitleDiv = null;
var saveConsigneeTitleDiv = null;
//保存收货人信息
handlersaveConsigneeTitleDiv = setInterval(function(){
saveConsigneeTitleDiv = document.getElementById('saveConsigneeTitleDiv');
console.log(saveConsigneeTitleDiv);
if (null != saveConsigneeTitleDiv) {
clearInterval(handlersaveConsigneeTitleDiv);
saveConsigneeTitleDiv.click();
handlersaveConsigneeTitleDivCount++;
if (handlersaveConsigneeTitleDivCount > 10) {
clearInterval(handlersaveConsigneeTitleDiv);
}
}
}, 400);
var handlerpayAndShipEditDivCount = 0;
var handlerpayAndShipEditDiv = null;
var payAndShipEditDiv = null;
//保存支付及配送方式
handlerpayAndShipEditDiv = setInterval(function(){
payAndShipEditDiv = document.getElementById('payAndShipEditDiv').getElementsByClassName('btn-submit')[0];
console.log(payAndShipEditDiv);
if (null != payAndShipEditDiv) {
clearInterval(handlerpayAndShipEditDiv);
payAndShipEditDiv.click();
handlerpayAndShipEditDivCount++;
if (handlerpayAndShipEditDivCount > 10) {
clearInterval(handlerpayAndShipEditDiv);
}
}
}, 400);
var handlerorderSubmitCount = 0;
var handlerorderSubmit = null;
var orderSubmit = null;
//提交订单
handlerorderSubmit = setInterval(function(){
orderSubmit = document.getElementById('order-submit');
console.log(orderSubmit);
if (null != orderSubmit) {
clearInterval(handlerorderSubmit);
orderSubmit.click();
handlerorderSubmitCount++;
if (handlerorderSubmitCount > 10) {
clearInterval(handlerorderSubmit);
}
}
}, 400);
}
}
var cUrl = window.location.href;
if(cUrl.indexOf('item.jd.com')>=0)qianggou(REFRESH_TIME);
else if(cUrl.indexOf('marathon.jd.com/seckill/seckill.action')>=0)qianggouzhifu();
else if(cUrl.indexOf('marathon.jd.com/koFail.html')>=0)qianggoushibai();
else if(cUrl.indexOf('cart.jd.com/addToCart.html')>=0)addtocart();
else if(cUrl.indexOf('cart.jd.com/cart.action')>=0)qianggoujiesuan();
else if(cUrl.indexOf('trade.jd.com/shopping/order/getOrderInfo.action')>=0)jiesuanfukuan();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment