@axeptio/behavior-detection 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Browser Build (IIFE) - Pure browser bundle without ES module exports
3
+ * Auto-initializing standalone bundle for CDN usage
4
+ */
5
+ export {};
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Browser Build (IIFE) - Pure browser bundle without ES module exports
3
+ * Auto-initializing standalone bundle for CDN usage
4
+ */
5
+ import { BehaviorDetector } from './behavior-detector';
6
+ import { MouseStrategy, ScrollStrategy, ClickStrategy, TapStrategy, KeyboardStrategy, EnvironmentStrategy, ResizeStrategy, } from './strategies/index.js';
7
+ /**
8
+ * Detect if the current device is mobile
9
+ */
10
+ function isMobileDevice() {
11
+ const hasTouchScreen = navigator.maxTouchPoints > 0 || 'ontouchstart' in window;
12
+ const mobileUA = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
13
+ const smallScreen = window.innerWidth < 768 && window.innerHeight < 1024;
14
+ return (hasTouchScreen && smallScreen) || mobileUA;
15
+ }
16
+ class BehaviorDetectionAPI {
17
+ constructor() {
18
+ this.detector = null;
19
+ this.checkInterval = null;
20
+ this.settings = null;
21
+ }
22
+ init(settings) {
23
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
24
+ if (this.detector) {
25
+ this.stop();
26
+ }
27
+ this.settings = settings;
28
+ this.detector = new BehaviorDetector({
29
+ pauseOnHidden: settings.pauseOnHidden !== undefined ? settings.pauseOnHidden : true
30
+ });
31
+ const isMobile = isMobileDevice();
32
+ const strategies = settings.strategies || {};
33
+ const defaultWeights = isMobile ? {
34
+ mouse: 0,
35
+ scroll: 0.35,
36
+ click: 0,
37
+ tap: 0.35,
38
+ keyboard: 0.15,
39
+ environment: 0.10,
40
+ resize: 0.05,
41
+ } : {
42
+ mouse: 0.30,
43
+ scroll: 0.15,
44
+ click: 0.30,
45
+ tap: 0,
46
+ keyboard: 0.10,
47
+ environment: 0.08,
48
+ resize: 0.02,
49
+ };
50
+ if (!isMobile && ((_a = strategies.mouse) === null || _a === void 0 ? void 0 : _a.enabled) !== false) {
51
+ const strategy = new MouseStrategy();
52
+ const weight = (_c = (_b = strategies.mouse) === null || _b === void 0 ? void 0 : _b.weight) !== null && _c !== void 0 ? _c : defaultWeights.mouse;
53
+ this.detector.addStrategy(strategy, weight);
54
+ }
55
+ if (((_d = strategies.scroll) === null || _d === void 0 ? void 0 : _d.enabled) !== false) {
56
+ const strategy = new ScrollStrategy();
57
+ const weight = (_f = (_e = strategies.scroll) === null || _e === void 0 ? void 0 : _e.weight) !== null && _f !== void 0 ? _f : defaultWeights.scroll;
58
+ this.detector.addStrategy(strategy, weight);
59
+ }
60
+ if (!isMobile && ((_g = strategies.click) === null || _g === void 0 ? void 0 : _g.enabled) !== false) {
61
+ const strategy = new ClickStrategy();
62
+ const weight = (_j = (_h = strategies.click) === null || _h === void 0 ? void 0 : _h.weight) !== null && _j !== void 0 ? _j : defaultWeights.click;
63
+ this.detector.addStrategy(strategy, weight);
64
+ }
65
+ if (isMobile && ((_k = strategies.tap) === null || _k === void 0 ? void 0 : _k.enabled) !== false) {
66
+ const strategy = new TapStrategy();
67
+ const weight = (_m = (_l = strategies.tap) === null || _l === void 0 ? void 0 : _l.weight) !== null && _m !== void 0 ? _m : defaultWeights.tap;
68
+ this.detector.addStrategy(strategy, weight);
69
+ }
70
+ if (((_o = strategies.keyboard) === null || _o === void 0 ? void 0 : _o.enabled) !== false) {
71
+ const strategy = new KeyboardStrategy();
72
+ const weight = (_q = (_p = strategies.keyboard) === null || _p === void 0 ? void 0 : _p.weight) !== null && _q !== void 0 ? _q : defaultWeights.keyboard;
73
+ this.detector.addStrategy(strategy, weight);
74
+ }
75
+ if (((_r = strategies.environment) === null || _r === void 0 ? void 0 : _r.enabled) !== false) {
76
+ const strategy = new EnvironmentStrategy();
77
+ const weight = (_t = (_s = strategies.environment) === null || _s === void 0 ? void 0 : _s.weight) !== null && _t !== void 0 ? _t : defaultWeights.environment;
78
+ this.detector.addStrategy(strategy, weight);
79
+ }
80
+ if (((_u = strategies.resize) === null || _u === void 0 ? void 0 : _u.enabled) !== false) {
81
+ const strategy = new ResizeStrategy();
82
+ const weight = (_w = (_v = strategies.resize) === null || _v === void 0 ? void 0 : _v.weight) !== null && _w !== void 0 ? _w : defaultWeights.resize;
83
+ this.detector.addStrategy(strategy, weight);
84
+ }
85
+ if (settings.autoStart !== false) {
86
+ this.start();
87
+ }
88
+ if (settings.checkMs && settings.checkMs > 0) {
89
+ this.startPeriodicCheck(settings.checkMs);
90
+ }
91
+ if (settings.onReady) {
92
+ settings.onReady(this);
93
+ }
94
+ }
95
+ start() {
96
+ if (!this.detector) {
97
+ return;
98
+ }
99
+ this.detector.start();
100
+ }
101
+ stop() {
102
+ if (this.detector) {
103
+ this.detector.stop();
104
+ }
105
+ if (this.checkInterval !== null) {
106
+ clearInterval(this.checkInterval);
107
+ this.checkInterval = null;
108
+ }
109
+ }
110
+ async score() {
111
+ if (!this.detector) {
112
+ return null;
113
+ }
114
+ return this.detector.score({ breakdown: true });
115
+ }
116
+ startPeriodicCheck(intervalMs) {
117
+ if (this.checkInterval !== null) {
118
+ clearInterval(this.checkInterval);
119
+ }
120
+ this.checkInterval = window.setInterval(async () => {
121
+ var _a, _b;
122
+ const result = await this.score();
123
+ if (!result || !this.settings)
124
+ return;
125
+ if (this.settings.onScore) {
126
+ this.settings.onScore(result);
127
+ }
128
+ const botThreshold = (_a = this.settings.botThreshold) !== null && _a !== void 0 ? _a : 0.3;
129
+ const humanThreshold = (_b = this.settings.humanThreshold) !== null && _b !== void 0 ? _b : 0.7;
130
+ if (result.score < botThreshold && this.settings.ifBot) {
131
+ this.settings.ifBot(result);
132
+ }
133
+ else if (result.score >= humanThreshold && this.settings.ifHuman) {
134
+ this.settings.ifHuman(result);
135
+ }
136
+ }, intervalMs);
137
+ }
138
+ getDetector() {
139
+ return this.detector;
140
+ }
141
+ }
142
+ // Create global API instance
143
+ const api = new BehaviorDetectionAPI();
144
+ // Auto-initialize if settings are already present
145
+ if (typeof window !== 'undefined') {
146
+ window.BehaviorDetector = api;
147
+ const checkAndInit = () => {
148
+ const settings = window.bdSettings;
149
+ if (settings) {
150
+ api.init(settings);
151
+ }
152
+ };
153
+ checkAndInit();
154
+ if (document.readyState === 'loading') {
155
+ document.addEventListener('DOMContentLoaded', checkAndInit);
156
+ }
157
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axeptio/behavior-detection",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Lightweight behavior detection library to assess human likelihood of user sessions",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -48,13 +48,13 @@
48
48
  "author": "Axeptio",
49
49
  "license": "MIT",
50
50
  "devDependencies": {
51
- "typescript": "latest",
51
+ "@aws-sdk/client-cloudfront": "^3.966.0",
52
+ "@aws-sdk/client-s3": "^3.966.0",
52
53
  "@playwright/test": "latest",
53
- "puppeteer": "latest",
54
- "selenium-webdriver": "latest",
55
54
  "chromedriver": "latest",
56
55
  "esbuild": "latest",
57
- "@aws-sdk/client-s3": "^3.515.0",
58
- "@aws-sdk/client-cloudfront": "^3.515.0"
56
+ "puppeteer": "latest",
57
+ "selenium-webdriver": "latest",
58
+ "typescript": "latest"
59
59
  }
60
60
  }