This repository has been archived by the owner on May 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
selector.js
279 lines (242 loc) · 8.58 KB
/
selector.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
// This code is a part of MagicCap which is a MPL-2.0 licensed project.
// Copyright (C) Jake Gealer <[email protected]> 2019.
// Requires the various things which are needed.
const { ipcRenderer } = require("electron");
// Gets the display number.
const screenNumber = Number(window.location.hash.substr(1));
// Where display information will be defined.
let displayInfo;
// Defines the selection type.
let selectionType = "__cap__";
// Defines all of the selections made across all of the windows.
const selections = {};
// Handles when keys are pushed.
document.addEventListener("keydown", async event => {
switch (event.key) {
case "Escape": {
await ipcRenderer.send("screen-close");
break;
}
}
});
// Defines the position of the first click.
let firstClick = null;
// This is the element for the selection.
const element = document.getElementById("selection");
// Handles when the mouse is down.
document.body.onmousedown = async e => {
if (e.target.matches(".clickable-property")) {
return;
}
firstClick = require("electron").screen.getCursorScreenPoint();
firstClick.pageX = e.pageX;
firstClick.pageY = e.pageY;
}
// Checks if a number is between other numbers. NUMBERRRRRRS!
function between(x, min, max) {
return x >= min && x <= max;
}
// Gets the inbetween windows.
const getInbetweenWindows = electronMouse => {
const inThese = [];
for (const x of displayInfo.activeWindows) {
if (between(electronMouse.x, x.x, x.x + x.width) && between(electronMouse.y, x.y, x.y + x.height)) {
inThese.push(x);
}
}
inThese.sort((a, b) => {
return a.width - b.width;
});
return inThese;
}
// Called when the mouse moves.
document.body.onmousemove = e => {
const thisClick = require("electron").screen.getCursorScreenPoint();
ipcRenderer.send(`${displayInfo.uuid}-event-send`, {
type: "invalidate-selections",
});
if (firstClick) {
element.style.width = `${Math.abs(e.pageX - firstClick.pageX)}px`;
element.style.height = `${Math.abs(e.pageY - firstClick.pageY)}px`;
element.style.left = (e.pageX - firstClick.pageX < 0) ? `${e.pageX}px` : `${firstClick.pageX}px`;
element.style.top = (e.pageY - firstClick.pageY < 0) ? `${e.pageY}px` : `${firstClick.pageY}px`;
} else {
const inThese = getInbetweenWindows(thisClick);
if (inThese.length === 0) {
return;
}
const screenPart = inThese[0];
element.style.width = `${screenPart.width}px`;
element.style.height = `${screenPart.height}px`;
element.style.left = `${screenPart.x - displayInfo.bounds.x}px`;
element.style.top = `${screenPart.y - displayInfo.bounds.y}px`;
}
}
// Protects against XSS.
const xssProtect = data => {
const lt = /</g;
const gt = />/g;
const ap = /'/g;
const ic = /"/g;
return data.replace(lt, "<").replace(gt, ">").replace(ap, "'").replace(ic, """);
}
// Called when the mouse button goes up.
document.body.onmouseup = async e => {
const thisClick = require("electron").screen.getCursorScreenPoint();
if (e.target.matches(".clickable-property")) {
return;
}
let inThese, start, end;
if (e.clientX === firstClick.pageX) {
inThese = getInbetweenWindows(thisClick);
if (inThese.length === 0) {
return;
}
}
const posInfo = element.getBoundingClientRect();
const width = posInfo.width;
const height = posInfo.height;
start = {
pageX: posInfo.x,
pageY: posInfo.y,
};
start.x = start.pageX + displayInfo.bounds.x;
start.y = start.pageY + displayInfo.bounds.y;
end = {
x: start.x + width,
y: start.y + height,
pageY: start.pageY + height,
pageX: start.pageX + width,
};
if (selectionType === "__cap__") {
ipcRenderer.send("screen-close", {
startX: start.x,
startY: start.y,
startPageX: start.pageX,
startPageY: start.pageY,
endX: end.x,
endY: end.y,
endPageX: end.pageX,
endPageY: end.pageY,
display: screenNumber,
selections: selections,
width: width,
height: height,
});
} else {
const selection = {
display: screenNumber,
selectionType: selectionType,
startX: start.x,
startY: start.y,
startPageX: start.pageX,
startPageY: start.pageY,
endX: end.x,
endY: end.y,
endPageX: end.pageX,
endPageY: end.pageY,
};
ipcRenderer.send(`${displayInfo.uuid}-event-send`, {
type: "selection-made",
args: selection,
});
if (selections[selectionType]) {
selections[selectionType].push(selection);
} else {
selections[selectionType] = [selection];
}
firstClick = null;
const selectionBlackness = document.createElement("div");
selectionBlackness.classList.add("selection-container");
selectionBlackness.style.width = element.style.width;
selectionBlackness.style.height = element.style.height;
selectionBlackness.style.left = element.style.left;
selectionBlackness.style.top = element.style.top;
selectionBlackness.style.bottom = element.style.bottom;
selectionBlackness.style.right = element.style.right;
selectionBlackness.innerHTML = `
<h1 class="selection-text">
${xssProtect(selectionType)}
</h1>
`;
document.body.appendChild(selectionBlackness);
}
}
// Defines the uploader properties HTML element.
const uploaderProperties = document.getElementById("UploaderProperties");
// This is called when a button is invoked.
function invokeButton(buttonId) {
const newNodes = [];
for (const el of uploaderProperties.childNodes) {
if (el.nodeName === "A") {
newNodes.push(el);
}
}
const htmlElement = newNodes[buttonId];
const button = displayInfo.buttons[buttonId];
switch (button.type) {
case "selection": {
htmlElement.childNodes[1].classList.add("selected");
for (const thisButtonId in displayInfo.buttons) {
if (buttonId != thisButtonId) {
const thisButton = displayInfo.buttons[thisButtonId];
if (thisButton.type === "selection") {
newNodes[thisButtonId].childNodes[1].classList.remove("selected");
}
}
}
selectionType = button.name;
ipcRenderer.send(`${displayInfo.uuid}-event-send`, {
type: "selection-type-change",
args: {
selectionType: selectionType,
},
});
break;
}
}
}
// Sets the display information.
ipcRenderer.send(`screen-${screenNumber}-load`);
ipcRenderer.once("load-reply", (_, info) => {
displayInfo = info;
// Sets the background image.
document.body.style.backgroundImage = `url("file://${__dirname}/dimmer.png"), url("http://127.0.0.1:${displayInfo.port}/?key=${displayInfo.key}&display=${screenNumber}")`;
// Handles displaying the buttons.
if (displayInfo.buttons && displayInfo.mainDisplay) {
let propertyStr = "";
for (const buttonId in displayInfo.buttons) {
const button = displayInfo.buttons[buttonId];
propertyStr += `
<a href="javascript:invokeButton(${buttonId})" style="cursor: default;">
<img class="clickable-property${button.active ? " selected" : ""}" src="file://${button.imageLocation}">
</a>
`;
}
uploaderProperties.innerHTML += propertyStr;
}
});
// Called when a event is recieved from another screen.
ipcRenderer.on("event-recv", (_, res) => {
if (res.display == screenNumber) {
return;
}
switch (res.type) {
case "invalidate-selections": {
element.style.width = "0px";
element.style.height = "0px";
break;
}
case "selection-type-change": {
selectionType = res.args.selectionType;
break;
}
case "selection-made": {
if (selections[res.args.selectionType]) {
selections[res.args.selectionType].push(res.args);
} else {
selections[res.args.selectionType] = [res.args];
}
}
}
});