您的位置:首页 > Web前端 > JavaScript

双11,自动领取优惠券 js 代码

2015-10-16 01:55 531 查看
http://www.zhihu.com/question/36426051/answer/67690322

本来我不熟悉 js,正好研究一下

(function(window, document) {
var interval = 800;
var closeDelay = 200;
var index = 0;
var couponLinks;
var getCoupon = function() {
if (index >= couponLinks.length) {
console.log("领取完毕");
return;
}
var coponLink = couponLinks[index];
coponLink.click();
index++;
console.log("领取 第" + index + " 张");
setTimeout(getCoupon, interval);
setTimeout(function() {
var close = document.querySelector('.mui-dialog-close');
if (close != null) close.click();
}, closeDelay);
}

var _scrollTop = 0;
var _scrollStep = document.documentElement.clientHeight;
var _maxScrollTop = document.body.clientHeight - _scrollStep;

var autoScrollDown = setInterval(function() {
_scrollTop += _scrollStep;
if (_scrollTop <= _maxScrollTop) {
document.body.scrollTop = _scrollTop;
return;
}
clearInterval(autoScrollDown);

couponLinks = document.querySelectorAll('.mui-act-item-yhqbtn');
console.log("总共:" + couponLinks.length + "条张优惠券待领取…");
getCoupon();
}, 500);
}) (window, document);


搜一下关键的几个函数和变量,就很容易理解了。

document.querySelector
document.querySelectorAll
document.documentElement.clientHeight
document.body.clientHeight
document.body.scrollTop
setInterval
setTimeout

搞明白了这几含义,写个自动领券的代码还不是分分钟~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: