bot-guardian-js
Version:
A powerful bot detection and prevention library for Node.js applications
74 lines (73 loc) • 2.48 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
class GuardianTracker {
constructor() {
this.events = [];
this.initializeTracking();
}
initializeTracking() {
// Mouse movement tracking
document.addEventListener('mousemove', (e) => {
this.trackMouseMovement(e);
});
// Scroll tracking
document.addEventListener('scroll', () => {
this.trackScroll();
});
// Click tracking
document.addEventListener('click', (e) => {
this.trackClick(e);
});
}
trackMouseMovement(e) {
const acceleration = Math.sqrt(Math.pow(e.movementX || 0, 2) +
Math.pow(e.movementY || 0, 2));
this.sendData({
type: 'mouse',
acceleration,
timestamp: Date.now()
});
}
trackScroll() {
const scrollSpeed = window.scrollY / (performance.now() / 1000);
this.sendData({
type: 'scroll',
speed: scrollSpeed,
timestamp: Date.now()
});
}
trackClick(e) {
this.sendData({
type: 'click',
x: e.clientX,
y: e.clientY,
timestamp: Date.now()
});
}
sendData(data) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield fetch('/track', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
}
catch (error) {
console.error('Failed to send tracking data:', error);
}
});
}
}
// Initialize tracker
window.guardianTracker = new GuardianTracker();