forked from nut-tree/libnut-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermissionCheck.js
82 lines (74 loc) · 3.21 KB
/
permissionCheck.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
let libnut = require("bindings")("libnut");
let hasScreenRecordingPermission = false;
let hasAccessibilityPermission = false;
try {
const permissions = require("@nut-tree/node-mac-permissions");
const wrapWithWarning = (message, nativeFunction) => (...args) => {
console.warn(message);
return nativeFunction(...args);
};
const askForAccessibility = (nativeFunction, functionName) => {
if (process.platform !== 'darwin' || hasAccessibilityPermission) {
return nativeFunction;
}
const accessibilityStatus = permissions.getAuthStatus("accessibility");
if (accessibilityStatus === 'authorized') {
hasAccessibilityPermission = true;
return nativeFunction;
} else if (accessibilityStatus === 'not determined' || accessibilityStatus === 'denied') {
permissions.askForAccessibilityAccess();
return wrapWithWarning(`##### WARNING! The application running this script tries to access accessibility features to execute ${functionName}! Please grant requested access and visit https://github.com/nut-tree/nut.js#macos for further information. #####`, nativeFunction);
}
}
const askForScreenRecording = (nativeFunction, functionName) => {
if (process.platform !== 'darwin' || hasScreenRecordingPermission) {
return nativeFunction;
}
const screenCaptureStatus = permissions.getAuthStatus("screen");
if (screenCaptureStatus === 'authorized') {
hasScreenRecordingPermission = true;
return nativeFunction;
} else if (screenCaptureStatus === 'not determined' || screenCaptureStatus === 'denied') {
permissions.askForScreenCaptureAccess();
return wrapWithWarning(`##### WARNING! The application running this script tries to screen recording features to execute ${functionName}! Please grant the requested access and visit https://github.com/nut-tree/nut.js#macos for further information. #####`, nativeFunction);
}
}
const accessibilityAccess = [
"dragMouse",
"moveMouse",
"getMousePos",
"mouseClick",
"mouseToggle",
"scrollMouse",
"keyTap",
"keyToggle",
"typeString",
"getScreenSize",
"highlight",
"captureScreen",
"getWindows",
"getActiveWindow",
"getWindowRect",
"getWindowTitle",
"focusWindow",
"resizeWindow"
];
const screenCaptureAccess = [
"getWindowTitle",
"captureScreen",
];
for (const functionName of accessibilityAccess) {
const originalFunction = libnut[functionName];
libnut[functionName] = (...args) => askForAccessibility(originalFunction, functionName)(...args);
}
for (const functionName of screenCaptureAccess) {
const originalFunction = libnut[functionName];
libnut[functionName] = (...args) => askForScreenRecording(originalFunction, functionName)(...args);
}
} catch (e) {
console.warn(`Encountered error establishing macOS permission checks:`, e.message);
console.warn(`Returning original module.`);
libnut = require("bindings")("libnut");
} finally {
module.exports = libnut;
}