-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.js
More file actions
663 lines (579 loc) · 18 KB
/
index.js
File metadata and controls
663 lines (579 loc) · 18 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/**
* Node Selection Hook
*
* This module provides a Node.js interface for monitoring text selections
* across applications on Windows, macOS, and Linux using UI Automation and Accessibility APIs.
*
* Copyright (c) 2025 0xfullex (https://github.com/0xfullex/selection-hook)
* Licensed under the MIT License
*/
const EventEmitter = require("events");
const gypBuild = require("node-gyp-build");
const path = require("path");
const isWindows = process.platform === "win32";
const isMac = process.platform === "darwin";
const isLinux = process.platform === "linux";
let nativeModule = null;
// Make debugFlag a private module variable to avoid global state issues
let _debugFlag = false;
try {
if (!isWindows && !isMac && !isLinux) {
throw new Error("[selection-hook] Only supports Windows, macOS, and Linux platforms");
}
nativeModule = gypBuild(path.resolve(__dirname));
} catch (err) {
console.error("[selection-hook] Failed to load native module:", err.message);
}
class SelectionHook extends EventEmitter {
#instance = null;
#running = false;
static SelectionMethod = {
NONE: 0,
UIA: 1,
/** @deprecated This method has been removed */
FOCUSCTL: 2,
ACCESSIBLE: 3,
AXAPI: 11,
/** @reserved AT-SPI method is reserved for future use */
ATSPI: 21,
PRIMARY: 22,
CLIPBOARD: 99,
};
static PositionLevel = {
NONE: 0,
MOUSE_SINGLE: 1,
MOUSE_DUAL: 2,
SEL_FULL: 3,
SEL_DETAILED: 4,
};
static FilterMode = {
DEFAULT: 0,
INCLUDE_LIST: 1,
EXCLUDE_LIST: 2,
};
static FineTunedListType = {
EXCLUDE_CLIPBOARD_CURSOR_DETECT: 0,
INCLUDE_CLIPBOARD_DELAY_READ: 1,
};
/** Sentinel value indicating coordinates are unavailable (-99999) */
static INVALID_COORDINATE = -99999;
// Linux only
static DisplayProtocol = {
UNKNOWN: 0,
X11: 1,
WAYLAND: 2,
};
/**
* Compositor type constants (Linux only).
*
* Values represent the compositor, not the desktop environment (DE).
* DE-bundled compositors are detected via XDG_CURRENT_DESKTOP:
* KDE Plasma → KWin, GNOME → mutter, COSMIC → cosmic-comp.
* Standalone compositors are detected via their own environment variables:
* Hyprland → HYPRLAND_INSTANCE_SIGNATURE, sway → SWAYSOCK.
* WLROOTS is a catch-all for generic wlroots-based compositors (labwc, river, etc.).
*/
static CompositorType = {
UNKNOWN: 0,
KWIN: 1,
MUTTER: 2,
HYPRLAND: 3,
SWAY: 4,
WLROOTS: 5,
COSMIC_COMP: 6,
};
constructor() {
if (!nativeModule) {
throw new Error(
"[selection-hook] Native module failed to load - only works on Windows, macOS, and Linux"
);
}
super();
try {
this.#instance = new nativeModule.TextSelectionHook();
} catch (err) {
throw new Error(`[selection-hook] Failed to create native instance: ${err.message}`);
}
}
/**
* Start monitoring text selections
* @param {SelectionConfig} [config] Optional configuration options
* @returns {boolean} Success status
*/
start(config = null) {
const defaultConfig = this.#getDefaultConfig();
_debugFlag = config?.debug ?? defaultConfig.debug;
if (this.#running) {
this.#logDebug("Text selection hook already running");
return true;
}
if (!this.#instance) {
try {
this.#instance = new nativeModule.TextSelectionHook();
} catch (err) {
this.#handleError("Failed to create hook instance", err);
return false;
}
}
if (config) {
this.#initByConfig(defaultConfig, config);
}
try {
const callback = (data) => {
try {
if (!data || !data.type || !this.#running) return;
switch (data.type) {
case "text-selection":
const formattedData = this.#formatSelectionData(data);
if (formattedData) {
this.emit("text-selection", formattedData);
}
break;
case "mouse-event":
if (data.action === "mouse-wheel") {
const { x, y, button, flag } = data;
this.emit(data.action, { x, y, button, flag });
} else {
const { x, y, button } = data;
this.emit(data.action, { x, y, button });
}
break;
case "keyboard-event":
{
const { uniKey, vkCode, sys, scanCode, flags } = data;
const keyData = { uniKey, vkCode, sys, flags };
if (scanCode !== undefined) keyData.scanCode = scanCode;
this.emit(data.action, keyData);
}
break;
case "status":
this.emit("status", data.status);
break;
case "error":
this.emit("error", new Error(data.error));
break;
}
} catch (err) {
this.#handleError("Failed to process event data", err);
}
};
this.#instance.start(callback);
this.#running = true;
this.emit("status", "started");
return true;
} catch (err) {
this.#handleError("Failed to start hook", err, "fatal");
return false;
}
}
/**
* Stop monitoring text selections, can start again using start()
* @returns {boolean} Success status
*/
stop() {
if (!this.#instance || !this.#running) {
this.#logDebug("Text selection hook not running");
return true;
}
try {
this.#instance.stop();
this.#running = false;
this.emit("status", "stopped");
return true;
} catch (err) {
this.#handleError("Failed to stop hook", err, "fatal");
this.#running = false;
return false;
}
}
/**
* Get current text selection
* @returns {object|null} Selection data or null
*/
getCurrentSelection() {
if (!this.#instance || !this.#running) {
this.#logDebug("Text selection hook not running");
return null;
}
try {
const data = this.#instance.getCurrentSelection();
return this.#formatSelectionData(data);
} catch (err) {
this.#handleError("Failed to get current selection", err);
return null;
}
}
/**
* Enable mousemove events (high CPU usage)
* @returns {boolean} Success status
*/
enableMouseMoveEvent() {
if (!this.#checkInstance()) return false;
try {
this.#instance.enableMouseMoveEvent();
return true;
} catch (err) {
this.#handleError("Failed to enable mouse move events", err);
return false;
}
}
/**
* Disable mousemove events
* @returns {boolean} Success status
*/
disableMouseMoveEvent() {
if (!this.#checkInstance()) return false;
try {
this.#instance.disableMouseMoveEvent();
return true;
} catch (err) {
this.#handleError("Failed to disable mouse move events", err);
return false;
}
}
/**
* Enable clipboard fallback for text selection
* Uses Ctrl+C as a last resort to get selected text
* @returns {boolean} Success status
*/
enableClipboard() {
if (!this.#checkInstance()) return false;
try {
this.#instance.enableClipboard();
return true;
} catch (err) {
this.#handleError("Failed to enable clipboard fallback", err);
return false;
}
}
/**
* Disable clipboard fallback for text selection
* Will not use Ctrl+C to get selected text
* @returns {boolean} Success status
*/
disableClipboard() {
if (!this.#checkInstance()) return false;
try {
this.#instance.disableClipboard();
return true;
} catch (err) {
this.#handleError("Failed to disable clipboard fallback", err);
return false;
}
}
/**
* Set clipboard mode and program list for text selection
*
* Configures how the clipboard fallback mechanism works for different programs.
* Mode can be:
* - DEFAULT: Use clipboard for all programs
* - INCLUDE_LIST: Only use clipboard for programs in the list
* - EXCLUDE_LIST: Use clipboard for all programs except those in the list
*
* @param {number} mode - Clipboard mode (SelectionHook.ClipboardMode)
* @param {string[]} programList - Array of program names to include/exclude.
* @returns {boolean} Success status
*/
setClipboardMode(mode, programList = []) {
if (!this.#checkInstance()) return false;
const validModes = Object.values(SelectionHook.FilterMode);
if (!validModes.includes(mode)) {
this.#handleError("Invalid clipboard mode", new Error("Invalid argument"));
return false;
}
if (!Array.isArray(programList)) {
this.#handleError("Program list must be an array", new Error("Invalid argument"));
return false;
}
try {
this.#instance.setClipboardMode(mode, programList);
return true;
} catch (err) {
this.#handleError("Failed to set clipboard mode and list", err);
return false;
}
}
/**
* Set global filter mode for text selection
*
* Configures how the global filter mechanism works for different programs.
* Mode can be:
* - DEFAULT: disable global filter
* - INCLUDE_LIST: Only use global filter for programs in the list
* - EXCLUDE_LIST: Use global filter for all programs except those in the list
*
* @param {number} mode - Filter mode (SelectionHook.FilterMode)
* @param {string[]} programList - Array of program names to include/exclude
* @returns {boolean} Success status
*/
setGlobalFilterMode(mode, programList = []) {
if (!this.#checkInstance()) return false;
const validModes = Object.values(SelectionHook.FilterMode);
if (!validModes.includes(mode)) {
this.#handleError("Invalid filter mode", new Error("Invalid argument"));
return false;
}
if (!Array.isArray(programList)) {
this.#handleError("Program list must be an array", new Error("Invalid argument"));
return false;
}
try {
this.#instance.setGlobalFilterMode(mode, programList);
return true;
} catch (err) {
this.#handleError("Failed to set global filter mode and list", err);
return false;
}
}
/**
* Set fine-tuned list for specific behaviors
*
* Configures fine-tuned lists for specific application behaviors.
* List types:
* - EXCLUDE_CLIPBOARD_CURSOR_DETECT: Exclude cursor detection for clipboard operations
* - INCLUDE_CLIPBOARD_DELAY_READ: Include delay when reading clipboard content
*
* @param {number} listType - Fine-tuned list type (SelectionHook.FineTunedListType)
* @param {string[]} programList - Array of program names for the fine-tuned list
* @returns {boolean} Success status
*/
setFineTunedList(listType, programList = []) {
if (!this.#checkInstance()) return false;
const validTypes = Object.values(SelectionHook.FineTunedListType);
if (!validTypes.includes(listType)) {
this.#handleError("Invalid fine-tuned list type", new Error("Invalid argument"));
return false;
}
if (!Array.isArray(programList)) {
this.#handleError("Program list must be an array", new Error("Invalid argument"));
return false;
}
try {
this.#instance.setFineTunedList(listType, programList);
return true;
} catch (err) {
this.#handleError("Failed to set fine-tuned list", err);
return false;
}
}
/**
* Set selection passive mode
* @param {boolean} passive - Passive mode
* @returns {boolean} Success status
*/
setSelectionPassiveMode(passive) {
if (!this.#checkInstance()) return false;
try {
this.#instance.setSelectionPassiveMode(passive);
return true;
} catch (err) {
this.#handleError("Failed to set selection passive mode", err);
return false;
}
}
/**
* Write text to clipboard
* @param {string} text - Text to write to clipboard
* @returns {boolean} Success status
*/
writeToClipboard(text) {
if (isLinux) {
// Linux clipboard write/read is not implemented in native code.
// Host applications should use their own clipboard API (e.g., Electron clipboard).
this.#logDebug("writeToClipboard is not supported on Linux");
return false;
}
if (!this.#checkInstance()) return false;
if (typeof text !== "string") {
this.#handleError("Text must be a string", new Error("Invalid argument"));
return false;
}
try {
return this.#instance.writeToClipboard(text);
} catch (err) {
this.#handleError("Failed to write text to clipboard", err);
return false;
}
}
/**
* Read text from clipboard
* @returns {string|null} Text from clipboard or null if empty or error
*/
readFromClipboard() {
if (isLinux) {
// Linux clipboard write/read is not implemented in native code.
// Host applications should use their own clipboard API (e.g., Electron clipboard).
this.#logDebug("readFromClipboard is not supported on Linux");
return null;
}
if (!this.#checkInstance()) return null;
try {
return this.#instance.readFromClipboard();
} catch (err) {
this.#handleError("Failed to read text from clipboard", err);
return null;
}
}
/**
* Check if the process is trusted for accessibility (macOS only)
* @returns {boolean} True if the process is trusted for accessibility, false otherwise
*/
macIsProcessTrusted() {
if (!isMac) {
this.#logDebug("Not supported on this platform");
return false;
}
if (!this.#checkInstance()) return false;
try {
return this.#instance.macIsProcessTrusted();
} catch (err) {
this.#handleError("Failed to check macOS process trust status", err);
return false;
}
}
/**
* Try to request accessibility permissions (macOS only)
* This MAY show a dialog to the user if permissions are not granted
* @returns {boolean} The current permission status, not the request result
*/
macRequestProcessTrust() {
if (!isMac) {
this.#logDebug("Not supported on this platform");
return false;
}
if (!this.#checkInstance()) return false;
try {
return this.#instance.macRequestProcessTrust();
} catch (err) {
this.#handleError("Failed to request macOS process trust", err);
return false;
}
}
/**
* Get Linux environment information (Linux only)
* @returns {object|null} Linux environment info object or null on non-Linux
*/
linuxGetEnvInfo() {
if (!isLinux) {
this.#logDebug("linuxGetEnvInfo is only supported on Linux");
return null;
}
if (!this.#checkInstance()) return null;
try {
return this.#instance.linuxGetEnvInfo();
} catch (err) {
this.#handleError("Failed to get Linux environment info", err);
return null;
}
}
/**
* Check if hook is running
* @returns {boolean} Running status
*/
isRunning() {
return this.#running;
}
/**
* Release resources
*/
cleanup() {
this.stop();
this.removeAllListeners();
this.#instance = null;
}
#getDefaultConfig() {
return {
debug: false,
enableMouseMoveEvent: false,
enableClipboard: true,
selectionPassiveMode: false,
clipboardMode: SelectionHook.FilterMode.DEFAULT,
globalFilterMode: SelectionHook.FilterMode.DEFAULT,
clipboardFilterList: [],
globalFilterList: [],
};
}
#initByConfig(defaultConfig, userConfig) {
const config = {};
// Only keep values that exist in userConfig and differ from defaultConfig
for (const key in userConfig) {
if (userConfig[key] !== defaultConfig[key]) {
config[key] = userConfig[key];
}
}
// Apply the filtered config
if (config.enableMouseMoveEvent !== undefined) {
if (config.enableMouseMoveEvent) {
this.#instance.enableMouseMoveEvent();
} else {
this.#instance.disableMouseMoveEvent();
}
}
if (config.enableClipboard !== undefined) {
if (config.enableClipboard) {
this.#instance.enableClipboard();
} else {
this.#instance.disableClipboard();
}
}
if (config.selectionPassiveMode !== undefined) {
this.#instance.setSelectionPassiveMode(config.selectionPassiveMode);
}
if (config.clipboardMode !== undefined || config.clipboardFilterList !== undefined) {
this.#instance.setClipboardMode(
config.clipboardMode ?? defaultConfig.clipboardMode,
config.clipboardFilterList ?? defaultConfig.clipboardFilterList
);
}
if (config.globalFilterMode !== undefined || config.globalFilterList !== undefined) {
this.#instance.setGlobalFilterMode(
config.globalFilterMode ?? defaultConfig.globalFilterMode,
config.globalFilterList ?? defaultConfig.globalFilterList
);
}
}
#formatSelectionData(data) {
if (!data) return null;
const selectionInfo = {
text: data.text,
programName: data.programName,
startTop: { x: data.startTopX, y: data.startTopY },
startBottom: { x: data.startBottomX, y: data.startBottomY },
endTop: { x: data.endTopX, y: data.endTopY },
endBottom: { x: data.endBottomX, y: data.endBottomY },
mousePosStart: { x: data.mouseStartX, y: data.mouseStartY },
mousePosEnd: { x: data.mouseEndX, y: data.mouseEndY },
method: data.method || 0,
posLevel: data.posLevel || 0,
};
if (isMac) {
selectionInfo.isFullscreen = data.isFullscreen;
}
return selectionInfo;
}
// Private helper methods
#checkInstance() {
if (!this.#instance) {
this.#logDebug("Text selection hook instance not created");
return false;
}
return true;
}
// level: "error" or "fatal"
// fatal will always show the error message
#handleError(message, err, level = "error") {
if (!_debugFlag && level === "error") return;
const errorMsg = `${message}: ${err.message}`;
console.error("[selection-hook] ", errorMsg);
if (err.stack) {
console.error(err.stack);
}
this.emit("error", new Error(errorMsg));
}
#logDebug(message) {
if (_debugFlag) {
console.warn("[selection-hook] ", message);
}
}
}
module.exports = SelectionHook;