-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathmain.js
More file actions
178 lines (160 loc) · 4.45 KB
/
main.js
File metadata and controls
178 lines (160 loc) · 4.45 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
const { app, BrowserWindow, screen, clipboard } = require("electron");
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const { windowManager } = require("node-window-manager");
// const { takeScreenshot } = require("electron-screencapture");
const screenshot = require("screenshot-desktop");
const sharp = require("sharp");
// There is a weird issue with Electron & Robotjs so let's just use osascript
// as temporary hack.
// const robot = require("robotjs");
const osascript = require("node-osascript");
const server = express();
server.use(bodyParser.urlencoded({ extended: true }));
const port = 3000;
let window;
let text;
let position = { x: 0, y: 0 };
let isPasting = false;
windowManager.requestAccessibility();
const script =
'tell application "System Events" to keystroke "v" using {command down}';
osascript.execute(script, (err, result, raw) => {
if (err) {
console.error(err);
}
});
// -- Electron App
function createWindow() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
// Create the browser window.
window = new BrowserWindow({
width: width,
height: height,
transparent: true,
frame: false,
hasShadow: false,
webPreferences: {
// nodeIntegration: true,
preload: path.join(__dirname, "preload.js"),
},
});
// and load the index.html of the app.
window.loadFile("index.html");
}
app.whenReady().then(() => {
createWindow();
app.on("activate", function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// Quit when all windows are closed.
app.on("window-all-closed", function () {
if (process.platform !== "darwin") app.quit();
});
// -- Express HTTP Server
server.get("/position", (req, res) => {
if (isPasting) {
return;
}
if (!window.isFocused()) {
window.show();
}
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
if (req.query.x && req.query.y) {
const x = (0.5 + parseFloat(req.query.x)) * width;
const y = (0.5 - parseFloat(req.query.y)) * height;
if (!isNaN(x) && !isNaN(y)) {
position.x = Math.round(x);
position.y = Math.round(y);
window.webContents.send("position", position);
}
}
res.send("k");
});
server.get("/screen", (req, res) => {
screenshot()
.then(async (img) => {
// img: Buffer filled with jpg goodness
// ...
const resized = await sharp(img)
.resize(1000)
.jpeg({
quality: 90,
})
.toBuffer();
res.set("Content-Type", "image/jpeg");
res.write(resized);
res.end();
// console.log(img);
})
.catch((err) => {
console.err(err);
});
});
server.post("/text", (req, res) => {
text = req.body.text;
window.webContents.send("text", req.body);
res.send("k");
});
server.get("/paste", async (req, res) => {
console.log("paste");
isPasting = true;
if (!text) {
console.log("ignore paste");
isPasting = false;
return;
}
// Write to clipboard.
clipboard.writeText(text);
// Blur electron window.
window.hide();
// Select window at current position.
const windowActivated = activateWindowAt(position.x, position.y);
// Send paste keystrokes.
if (windowActivated) {
const script =
'tell application "System Events" to keystroke "v" using {command down}';
osascript.execute(script, (err, result, raw) => {
if (err) {
console.error(err);
}
});
console.log("combination sent");
} else {
console.log("no window at position");
}
// Clear text.
window.webContents.send("text", { text: "" });
// Resume
isPasting = false;
});
server.listen(port, () => console.log(`Listening at http://localhost:${port}`));
// -- OS Windows
function activateWindowAt(x, y) {
console.log("activate window at:", x, y);
const windows = windowManager.getWindows();
for (const window of windows) {
const bounds = window.getBounds();
if (window.path.indexOf("electron") != -1) {
continue;
}
const isTaskBarMenu = bounds.y == 0 && bounds.height == 22;
if (isTaskBarMenu || !window.isVisible() || !window.isWindow()) {
continue;
}
console.log(window.path, bounds);
if (
x > bounds.x &&
y > bounds.y &&
x < bounds.x + bounds.width &&
y < bounds.y + bounds.height
) {
console.log("got window:", window.path, window.getTitle());
window.bringToTop();
return window;
}
}
return false;
}