-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
410 lines (398 loc) · 14.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*** @type {Record<string, Function>} */
const native = require("./build/Release/jsautogui");
const os = require("os");
const platform = os.platform();
const eType = (a, t, n) => {
if (typeof a !== t) {
const e = new Error("Expected " + n + " to be a " + t + ", instead got: " + typeof a);
throw e;
}
};
const eInt32 = (a, n) => {
eType(a, "number", n);
if (a < INT32_MIN || a > INT32_MAX) {
const e = new Error("Expected " + n + " to be a valid int32.");
throw e;
}
if (a !== Math.floor(a)) {
const e = new Error("Expected " + n + " to be an int32, instead got a float/double.");
throw e;
}
};
const eSize = (a, min, max, n) => {
eType(a, "number", n);
if (a < min || a > max) {
const e = new Error("Expected " + n + " to be between (" + min + ", " + max + ").");
throw e;
}
};
const perm = s => {
const e = new Error("Couldn't " + s + ", is the program lacking some permissions?");
throw e;
};
const INT32_MIN = -2147483648;
const INT32_MAX = 2147483647;
let callPause = 0;
let failSafe = false;
let failSafeInterval;
const MESSAGE_BOX = {
ICON: {
hand: 0x00000010,
question: 0x00000020,
exclamation: 0x00000030,
asterisk: 0x00000040,
user: 0x00000080,
warning: 0x00000030,
error: 0x00000010,
information: 0x00000040,
stop: 0x00000010
},
BUTTONS: {
ok: 0x00000000,
ok_cancel: 0x00000001,
abort_retry_ignore: 0x00000002,
yes_no_cancel: 0x00000003,
yes_no: 0x00000004,
retry_cancel: 0x00000005,
cancel_try_continue: 0x00000006
},
BUTTON_STYLE: {
def1: 0x00000000,
def2: 0x00000100,
def3: 0x00000200,
def4: 0x00000300
},
TYPE: {
appl_modal: 0x00000000,
system_modal: 0x00001000,
task_modal: 0x00002000,
help: 0x00003000
},
ACTIONS: {
no_focus: 0x00008000,
set_foreground: 0x00010000,
default_desktop_only: 0x00020000,
top_most: 0x00040000,
right: 0x00080000,
rtl_reading: 0x00100000,
},
RESPONSES: {
ok: 1,
cancel: 2,
abort: 3,
retry: 4,
ignore: 5,
yes: 6,
no: 7,
try: 10,
continue: 11,
1: "ok",
2: "cancel",
3: "abort",
4: "retry",
5: "ignore",
6: "yes",
7: "no",
10: "try",
11: "continue"
}
};
let KEYS = {};
if (platform === "win32") {
const WIN_KEYS = require("./data/win-keys");
KEYS = {
...WIN_KEYS,
"!": WIN_KEYS.shift,
"@": WIN_KEYS.altright,
"#": WIN_KEYS.altright,
"$": WIN_KEYS.altright,
"%": WIN_KEYS.shift,
"^": WIN_KEYS.shift,
"&": WIN_KEYS.shift,
"(": WIN_KEYS.shift,
")": WIN_KEYS.shift,
"_": WIN_KEYS.shift,
"=": WIN_KEYS.shift,
"{": WIN_KEYS.altright,
"}": WIN_KEYS.altright,
"[": WIN_KEYS.altright,
"]": WIN_KEYS.altright,
"|": WIN_KEYS.altright,
"\\": WIN_KEYS.altright,
":": WIN_KEYS.shift,
";": WIN_KEYS.shift,
"\"": WIN_KEYS.shift,
"<": WIN_KEYS.shift,
">": WIN_KEYS.shift,
"?": WIN_KEYS.shift,
"/": WIN_KEYS.altright,
"~": WIN_KEYS.altright,
"`": WIN_KEYS.altright
};
} else if (platform === "linux") {
KEYS = require("./data/x11-keys");
} else {
// KEYS = {...require("./data/osx-keys"), ...require("./data/osx-modifiers")}
}
const KEY_NAMES = [
"\t", "\n", "\r", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "_", "`", "a", "b", "c",
"d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"{", "|", "}", "~", "accept", "add", "alt", "altleft", "altright", "apps", "backspace", "browserback",
"browserfavorites", "browserforward", "browserhome", "browserrefresh", "browsersearch", "browserstop", "capslock",
"clear", "convert", "ctrl", "ctrlleft", "ctrlright", "decimal", "del", "delete", "divide", "down", "end", "enter",
"esc", "escape", "execute", "f1", "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f2", "f20",
"f21", "f22", "f23", "f24", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "final", "fn", "hanguel", "hangul", "hanja",
"help", "home", "insert", "junja", "kana", "kanji", "launchapp1", "launchapp2", "launchmail", "launchmediaselect",
"left", "modechange", "multiply", "nexttrack", "nonconvert", "num0", "num1", "num2", "num3", "num4", "num5", "num6",
"num7", "num8", "num9", "numlock", "pagedown", "pageup", "pause", "pgdn", "pgup", "playpause", "prevtrack", "print",
"printscreen", "prntscrn", "prtsc", "prtscr", "return", "right", "scrolllock", "select", "separator", "shift",
"shiftleft", "shiftright", "sleep", "space", "stop", "subtract", "tab", "up", "volumedown", "volumemute",
"volumeup", "win", "winleft", "winright", "yen", "command", "option", "optionleft", "optionright"
];
function linear(dx, dy, steps) {
const stepVector = {x: dx / steps, y: dy / steps};
return function (_step) {
API.mouse.moveRel(stepVector.x, stepVector.y);
};
}
class MessageBox {
title = "";
text = "";
buttons = "ok";
icon = "";
show() {
let flags = 0;
const btn = MESSAGE_BOX.BUTTONS[this.buttons];
const icon = MESSAGE_BOX.ICON[this.icon];
if (btn) flags |= btn;
if (icon) flags |= icon;
return MESSAGE_BOX.RESPONSES[native.show_message_box(this.title, this.text, flags)];
};
}
class PromptBox {
show() {
return native.show_prompt();
};
}
const API = {
__native: native,
MESSAGE_BOX,
KEY_NAMES,
get FAIL_SAFE() {
return failSafe;
},
set FAIL_SAFE(v) {
eType(v, "boolean", "FAIL_SAFE");
if (v === failSafe) return;
clearInterval(failSafeInterval);
if (v === true) failSafeInterval = setInterval(() => {
const pos = native.get_mouse_pos();
if (!pos) return;
if (pos.x < 3 && pos.y < 3) throw new Error("Failed safely. This has been enabled by setting the .FAIL_SAFE value to true.");
}, 100);
},
get PAUSE() {
return callPause;
},
set PAUSE(v) {
eInt32(v, "PAUSE");
eSize(v, 0, INT32_MAX, "PAUSE");
callPause = v;
native.set_pause(v);
},
mouse: {
LinearTween: linear,
get position() {
return this.getPosition();
},
set position(v) {
this.setPosition(v.x, v.y);
},
get x() {
return this.position.x;
},
set x(v) {
eInt32(v, "mouse.position.x");
eSize(v, 0, INT32_MAX, "mouse.position.x");
if (!native.set_mouse_x(v)) return perm("set the x position of the mouse");
},
get y() {
return this.position.y;
},
set y(v) {
eInt32(v, "mouse.position.y");
eSize(v, 0, INT32_MAX, "mouse.position.y");
if (!native.set_mouse_y(v)) return perm("set the y position of the mouse");
},
getPosition(point = true) {
const pos = native.get_mouse_pos();
if (!pos) return perm("find the position of the mouse");
if (!point) return pos;
const obj = {};
Object.defineProperties(obj, {
x: {
get: () => pos.x,
set: v => this.x = v
},
y: {
get: () => pos.y,
set: v => this.y = v
}
});
return obj;
},
setPosition(x, y) {
eInt32(x, "mouse.position.x");
eInt32(y, "mouse.position.y");
eSize(x, 0, INT32_MAX, "mouse.position.x");
eSize(y, 0, INT32_MAX, "mouse.position.y");
if (!native.set_mouse_pos(x, y)) return perm("set the position of the mouse");
},
moveTo(x, y, duration = 0, tween = linear, deltaTime = 50) {
eInt32(x, "x");
eInt32(y, "y");
eSize(x, 0, INT32_MAX, "x");
eSize(y, 0, INT32_MAX, "y");
eInt32(duration, "duration");
eInt32(deltaTime, "deltaTime");
if (duration === 0) return native.set_mouse_pos(x, y);
eType(tween, "function");
const start = native.get_mouse_pos();
return this.moveRel(x - start.x, y - start.y, duration, tween, deltaTime);
},
moveRel(dx, dy, duration = 0, tween = linear, deltaTime = 50) {
eInt32(dx, "x");
eInt32(dy, "y");
eInt32(duration, "duration");
eInt32(deltaTime, "deltaTime");
if (duration === 0) return native.set_mouse_pos_rel(dx, dy) || perm("set the position of the mouse");
if (deltaTime > duration) deltaTime = duration;
const start = native.get_mouse_pos();
const steps = Math.ceil(duration / deltaTime);
const tweenInit = tween(dx, dy, steps);
let step = 0;
return new Promise(r => {
const intervalId = setInterval(() => {
duration -= deltaTime;
if (duration <= 0) {
if (duration !== 0) native.set_mouse_pos(start.x, start.y);
r();
return clearInterval(intervalId);
}
tweenInit(step);
step++;
}, deltaTime);
});
},
dragTo(x, y, button = "primary", duration = 0, tween = linear, deltaTime = 50) {
this.down(button);
const r = this.moveTo(x, y, duration, tween, deltaTime);
if (r instanceof Promise) return r.then(() => this.up(button));
this.up(button);
},
dragRel(x, y, button = "primary", duration = 0, tween = linear, deltaTime = 50) {
this.down(button);
const r = this.moveRel(x, y, duration, tween, deltaTime);
if (r instanceof Promise) return r.then(() => this.up(button));
this.up(button);
},
get isSwapped() {
return native.is_mouse_swapped();
},
down(button = "primary") {
if (button === "primary") button = this.isSwapped ? "right" : "left";
if (button === "secondary") button = this.isSwapped ? "left" : "right";
if (button === "right") return native.mouse_right_down() || perm("press a button in mouse");
if (button === "left") return native.mouse_left_down() || perm("press a button in mouse");
if (button === "middle") return native.mouse_middle_down() || perm("press a button in mouse");
throw new Error("Expected the button argument to be 'right', 'left', 'middle', 'primary', 'secondary'. Got: " + button);
},
up(button = "primary") {
if (button === "primary") button = this.isSwapped ? "right" : "left";
if (button === "secondary") button = this.isSwapped ? "left" : "right";
if (button === "right") return native.mouse_right_up() || perm("release a button in mouse");
if (button === "left") return native.mouse_left_up() || perm("release a button in mouse");
if (button === "middle") return native.mouse_middle_up() || perm("release a button in mouse");
throw new Error("Expected the button argument to be 'right', 'left', 'middle', 'primary', 'secondary'. Got: " + button);
},
click(button = "primary") {
if (button === "primary") button = this.isSwapped ? "right" : "left";
if (button === "secondary") button = this.isSwapped ? "left" : "right";
if (button === "right") return native.click_right() || perm("press a button in mouse");
if (button === "left") return native.click_left() || perm("press a button in mouse");
if (button === "middle") return native.click_middle() || perm("press a button in mouse");
throw new Error("Expected the button argument to be 'right', 'left', 'middle', 'primary', 'secondary'. Got: " + button);
},
scroll(x = 0, y = 0) {
eInt32(x, "x");
eInt32(y, "y");
if (!native.mouse_scroll(x, y)) perm("scroll the wheel of mouse");
}
},
keyboard: {
__native(key, fn) {
if (KEYS[key]) native[fn](false, KEYS[key]);
else {
/*const modifier = WIN_MODIFIERS[key];
if (modifier) native.key_down(false, modifier) || perm("press a key of keyboard");
native[fn](true, key.charCodeAt(0));
if (modifier) native.key_up(false, modifier) || perm("press a key of keyboard");*/
native[fn](true, key.charCodeAt(0));
// todo: fix modifiers
}
},
press(...keys) {
for (const key of keys) this.__native(key, "press_key");
},
down(...keys) {
for (const key of keys) this.__native(key, "key_down");
},
up(...keys) {
for (const key of keys) this.__native(key, "key_up");
},
write(...texts) {
for (const text of texts) {
for (let i = 0; i < text.length; i++) this.press(text[i]);
}
},
hotkey(...keys) {
this.down(...keys);
this.up(...keys);
}
},
screen: {
get size() {
return native.get_screen_size();
},
get width() {
return this.size.width;
},
get height() {
return this.size.height;
},
get center() {
const s = this.size;
return {x: Math.floor(s.width / 2), y: Math.floor(s.height / 2)};
},
includes(x, y) {
eInt32(x, "x");
eInt32(y, "y");
const size = native.get_screen_size();
return x >= 0 && y >= 0 && x < size.width && y < size.height;
},
contains(x, y) {
return this.includes(x, y);
}
},
MessageBox,
PromptBox,
alert(text, title = "Alert") {
return native.show_message_box(text, title, MESSAGE_BOX.ICON.warning | MESSAGE_BOX.BUTTONS.ok);
},
confirm(text, title = "Confirm") {
return native.show_message_box(text, title, MESSAGE_BOX.ICON.warning | MESSAGE_BOX.BUTTONS.yes_no) === MESSAGE_BOX.RESPONSES.yes;
}
};
module.exports = API;
API.default = API;