landers.human-behavior
Version:
landers.human-behavior
216 lines (194 loc) • 6.76 kB
JavaScript
/**
* prototype
* @param {[type]} win [description]
* @return {[type]} [description]
*/
+function (win) {
win.Array.prototype.last = function(){
return this[this.length - 1];
};
}(window);
/**
* HumanBehavior
* @param {[type]} secret [description]
*/
function HumanBehavior(opts){
opts = opts || {};
this.secret = opts.secret;
this.apiurl = opts.apiurl;
this.timeout = opts.timeout || 5000;
this.timer = null;
this.isPost = true;
this.moves = [];
this.wheels = [];
this.keys = [];
this.uniqueTime = true;
this.hooks = opts.hooks || {};
}
HumanBehavior.prototype = {
getTime: function(){
return parseInt((new Date().getTime()) / 1000);
},
triggerHook: function(event){
var that = this;
var fun = that.hooks[event];
fun && fun();
delete that.hooks[event];
},
startCheck: function(){
var that = this;
that.triggerHook('onStart');
!function(){
document.onmousemove = function(e){
e = e || window.event;
var last = that.moves.last() || {} || {};
var t = that.getTime();
if (that.uniqueTime && last.t == t) return;
var pos = {
x: e.clientX || e.x,
y: e.clientY || e.y,
t: t
};
that.moves.push(pos);
that.send({trigger: 'onmousemove'});
that.print();
};
}();
!function(){
document.onkeydown = function(e){
e = e || window.event;
if (e.keyCode == 91 || e.keyCode == 18 || e.keyCode == 17) {
// 只按了 command, alt, ctrl, 不作处理
return;
}
if (
(e.keyCode == 116) ||
(e.ctrlKey && e.keyCode == 82) ||
(e.metaKey && e.keyCode == 82)
) {
that.send({
trigger: 'onkeydown',
success: function(response, xml) {
document.location.reload();
},
error: function(status){
document.location.reload();
}
});
if (e.preventDefault) e.preventDefault();
} else {
var last = that.keys.last() || {};
var t = that.getTime();
that.keys.push({
c: e.keyCode,
t: t
});
that.send({
trigger: 'onkeydown'
});
}
that.print();
};
}();
!function(){
function mouse_wheel(e){
e = e || window.event;
var last = that.wheels.last() || {};
var t = that.getTime();
if (last.t == t) return;
// http://help.dottoro.com/ljekedtv.php
that.wheels.push({
delta: e.wheelDelta || -40 * e.detail,
t: t
});
that.send({trigger: 'onmousewheel'});
that.print();
}
if(document.addEventListener){
document.addEventListener('DOMMouseScroll', mouse_wheel, false);
}//W3C
window.onmousewheel = document.onmousewheel = document.body.onmousewheel = mouse_wheel;//IE/Opera/Chrome
}();
!function(){
var onunload = window.onunload;
window.onunload = function(e){
that.send({trigger: 'onunload'});
onunload && onunload();
for (var i=1; i<=100000; i++) {
i++;
}
};
}();
!function(){
if (document.addEventListener){
document.body.addEventListener("touchstart",function(){
that.send({trigger: 'ontouchstart'});
});
}
}();
that.timer = setTimeout(function(){
that.send({trigger: 'onload'});
}, that.timeout);
},
sign: function(data){
var str = JSON.stringify(data) + this.secret;
return sha256(str);
},
collect: function(){
var data = Landers.browser.info();
data['5a44c66e'] = this.moves; //human_moves
data['f4fbf58a'] = this.keys; //human_keys
data['1db82b46'] = this.wheels; //human_wheels
return data;
},
send: function(opts){
var that = this;
opts = opts || {};
if (!opts.trigger || !that.apiurl) return;
that.triggerHook('onBeforeSend');
clearTimeout(that.timer);
var apiurl = that.apiurl;
setTimeout(function(){
var data = that.collect();
data['0c3bd52b'] = opts.trigger; //trigger
var success = function (rspn, type) {
clearTimeout(that.timer);
that.triggerHook('onAfterSend');
opts.success && opts.success(rspn);
that.msgbox('SUCCESS:' + opts.trigger);
};
var error = function (xhr, textStatus, errorThrown){
clearTimeout(that.timer);
that.triggerHook('onAfterSend');
that.msgbox('ERROR:' + opts.trigger);
};
apiurl += (apiurl.indexOf('?') > 0 ? '&' : '?') + 'sign=' + that.sign(data);
if (that.isPost) {
Landers.ajax.post(apiurl, data)
.success(success)
.error(error);
} else {
apiurl += '?d=' + encodeURIComponent(JSON.stringify(data));
Landers.ajax.jsonp(apiurl)
.success(success)
.error(error);
}
}, 500);
that.apiurl = '';
},
msgbox: function(msg){
if (window.isLocalDebug) {
alert(msg);
}
},
print: function(){
var that = this;
if (window.isLocalDebug) {
console.dp({
moves: that.moves[that.moves.length-1],
keys: that.keys[that.keys.length-1],
wheels: that.wheels[that.wheels.length-1]
});
}
}
};