bezier-mouse-js
Version:
Lightweight library to generate human-like mouse movements with Bézier curves.
129 lines (125 loc) • 4.83 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
BezierMouse: () => BezierMouse,
default: () => BezierMouse,
utils: () => utils_default
});
module.exports = __toCommonJS(index_exports);
// src/BezierMouse.ts
var import_bezier_js = require("bezier-js");
// src/utils.ts
var { floor, random } = Math;
function choice(items) {
return items[floor(random() * items.length)];
}
function randint(min, max) {
return floor(random() * (max - min + 1)) + min;
}
var utils = { choice, randint };
var utils_default = utils;
// src/BezierMouse.ts
var { abs, ceil } = Math;
var BezierMouse = class {
constructor(mouseSpeed = 100) {
this.nutJsModule = null;
this.mouseSpeed = mouseSpeed;
}
async getNutJs() {
if (!this.nutJsModule) {
try {
this.nutJsModule = await import("@nut-tree/nut-js");
this.nutJsModule.mouse.config.mouseSpeed = this.mouseSpeed;
} catch {
throw new Error(
"bezier-mouse-js: @nut-tree/nut-js is required for mouse control methods. Install it with: npm install @nut-tree/nut-js"
);
}
}
return this.nutJsModule;
}
async moveAndClick(initPos, finPos, clickType = "LEFT", opts = {}) {
const nut = await this.getNutJs();
await this.move(initPos, finPos, opts);
await nut.mouse.click(nut.Button[clickType]);
}
async moveAndDoubleClick(initPos, finPos, clickType = "LEFT", opts = {}) {
const nut = await this.getNutJs();
await this.move(initPos, finPos, opts);
await nut.mouse.doubleClick(nut.Button[clickType]);
}
async move(initPos, finPos, opts = {}) {
const nut = await this.getNutJs();
let finishPosition = finPos;
if (!opts.preciseClick) {
const deviation = 5;
const randDeviation = () => utils_default.randint(deviation / 2, deviation);
const region = new nut.Region(
finPos.x,
finPos.y,
randDeviation(),
randDeviation()
);
finishPosition = await nut.randomPointIn(region);
}
const path = this.bezierCurveTo(initPos, finishPosition, opts);
const nutPoints = path.map((p) => new nut.Point(p.x, p.y));
await nut.mouse.move(nutPoints);
}
bezierCurveTo(initPos, finPos, opts = {}) {
const curve = this.cubicBezierCurve(initPos, finPos, opts);
const LUT = curve.getLUT(opts.steps || 100);
return LUT.map((point) => ({ x: point.x, y: point.y }));
}
cubicBezierCurve(initPos, finPos, opts = {}) {
const cntlPt1 = this.getBezierControlPoint(initPos, finPos, opts);
const cntlPt2 = this.getBezierControlPoint(initPos, finPos, opts);
return new import_bezier_js.Bezier([initPos, cntlPt1, cntlPt2, finPos]);
}
getBezierControlPoint(initPos, finPos, opts = {}) {
const { deviation = 20, flip = false } = opts;
const deltaX = abs(ceil(finPos.x) - ceil(initPos.x));
const deltaY = abs(ceil(finPos.y) - ceil(initPos.y));
const randDeviation = () => utils_default.randint(deviation / 2, deviation);
const refPointX = flip ? initPos.x : finPos.x;
const refPointY = flip ? initPos.y : finPos.y;
return {
x: refPointX + utils_default.choice([-1, 1]) * deltaX * 0.01 * randDeviation(),
y: refPointY + utils_default.choice([-1, 1]) * deltaY * 0.01 * randDeviation()
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BezierMouse,
utils
});
//# sourceMappingURL=index.cjs.map