-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
preload.js
344 lines (310 loc) · 9.72 KB
/
preload.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
var { ipcRenderer, contextBridge } = require('electron');
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
try{
if (session && session.version){
ipcRenderer.send('vdonVersion', {ver:session.version});
}
} catch(e){}
})
window.addEventListener('message', ({ data }) => {
console.log("preload.js-Message-Incoming: "+data);
ipcRenderer.send('postMessage', data)
});
var doSomethingInWebApp = null;
try {
if ((typeof session !== 'undefined') && session.version){
ipcRenderer.send('vdonVersion', {ver:session.version}); // clear the current Version; let it load if needed.
} else {
ipcRenderer.send('vdonVersion', {ver:false}); // clear the current Version; let it load if needed.
}
} catch(e){
console.log(e);
}
try {
contextBridge.exposeInMainWorld("electronApi", { //
'exposeDoSomethingInWebApp' : function (callback) {
doSomethingInWebApp = callback;
},
'updateVersion' : function (version) { // window.electronApi.updateVersion(session.version);
console.log("33:"+version);
ipcRenderer.send('vdonVersion', {ver:version});
},
'updatePPT' : function (PPTHotkey) { // window.electronApi.updateVersion(session.version);
console.log("updatePPT recieved !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
ipcRenderer.send('PPTHotkey', PPTHotkey);
}
});
} catch(e){
}
var storedEle = null;
var PPTTimeout = null;
ipcRenderer.on('postMessage', (event, ...args) => {
console.log(args);
try {
if (!doSomethingInWebApp){
if (session && session.remoteInterfaceAPI){
doSomethingInWebApp = session.remoteInterfaceAPI;
}
}
}catch(e){}
try {
if ("record" in args[0]) {
if (doSomethingInWebApp) {
console.log("doSomethingInWebApp");
var x = args[0].params.x;
var y = args[0].params.y;
var ele = document.elementFromPoint(x,y);
if (ele.id){
var fauxEvent = {};
fauxEvent.data = {};
fauxEvent.data.record = ele.id;;
doSomethingInWebApp(fauxEvent);
}
} else {
console.log(doSomethingInWebApp);
console.log(session.remoteInterfaceAPI);
console.log("no doSomethingInWebApp");
}
}
if ("hangup" in args[0]) {
if (args[0].hangup == "estop"){
if (doSomethingInWebApp) {
console.log("HANG UP ESTOP1 ");
var fauxEvent = {};
fauxEvent.data = {};
fauxEvent.data.hangup = "estop";
doSomethingInWebApp(fauxEvent);
} else {
console.log("HANG UP ESTOP1 2");
session.hangup(false, true); // no reload, estop=true
console.log(session.remoteInterfaceAPI);
console.log("no doSomethingInWebApp");
}
} else {
if (doSomethingInWebApp) {
console.log("HANG UP 1");
var fauxEvent = {};
fauxEvent.data = {};
fauxEvent.data.close = true; // close and hangup are the same; close is compatible with older vdon versions tho. no estop tho.
doSomethingInWebApp(fauxEvent);
} else {
console.log("HANG UP 2");
session.hangup();
console.log(session.remoteInterfaceAPI);
console.log("no doSomethingInWebApp");
}
}
}
if (doSomethingInWebApp && ("mic" in args[0])){ // this is the new version.
var fauxEvent = {};
fauxEvent.data = {};
fauxEvent.data.mic = args[0].mic
doSomethingInWebApp(fauxEvent);
return;
} else if ("micOld" in args[0]) { // this is for old pre v22 version
if (session && (args[0].micOld === true)){ // unmute
session.muted = false; // set
toggleMute(true); // apply
} else if (session && (args[0].micOld === false)){ // mute
session.muted = true; // set
toggleMute(true); // apply
} else if (args[0].micOld === "toggle") { // toggle
toggleMute();
}
return;
}
if ("PPT" in args[0]){
console.log(args[0].PPT);
if (PPTTimeout){
clearTimeout(PPTTimeout);
PPTTimeout = setTimeout(function(node){
PPTTimeout=null;
if (node){
session.muted = true;
toggleMute(true);
getById("mutebutton").classList.remove("PPTActive");
} else if (doSomethingInWebApp){
var fauxEvent = {};
fauxEvent.data = {};
fauxEvent.data.PPT = false;
doSomethingInWebApp(fauxEvent);
}
},200, args[0].node);
} else {
if (args[0].node){
session.muted = false;
toggleMute(true);
getById("mutebutton").classList.add("PPTActive");
} else if (doSomethingInWebApp){
var fauxEvent = {};
fauxEvent.data = {};
fauxEvent.data.PPT = true;
doSomethingInWebApp(fauxEvent);
}
PPTTimeout = setTimeout(function(node){
PPTTimeout=null;
if (node){
session.muted = true;
toggleMute(true);
getById("mutebutton").classList.remove("PPTActive");
} else if (doSomethingInWebApp){
var fauxEvent = {};
fauxEvent.data = {};
fauxEvent.data.PPT = false;
doSomethingInWebApp(fauxEvent);
}
},600, args[0].node);
}
return;
}
if ("getDeviceList" in args[0]) {
var x = args[0].params.x;
var y = args[0].params.y;
var ele = document.elementFromPoint(x,y);
storedEle = ele;
var menu = ele.dataset.menu;
var response = {};
response.menu = menu || false;
response.eleId = ele.id || false;
response.UUID = ele.dataset.UUID || false;
response.params = args[0].params;
if (typeof enumerateDevices === "function"){
enumerateDevices().then(function(deviceInfos) {
response.deviceInfos = deviceInfos;
response = JSON.parse(JSON.stringify(response));
ipcRenderer.send('deviceList', response);
})
} else {
console.log("calling requestOutputAudioStream");
requestOutputAudioStream().then(function(deviceInfos) {
response.deviceInfos = deviceInfos;
response = JSON.parse(JSON.stringify(response));
ipcRenderer.send('deviceList', response);
//deviceInfos = JSON.parse(JSON.stringify(deviceInfos));
/* var output = [];
for (var i=0;i<deviceInfos.length;i++){
if (deviceInfos[i].kind === "audiooutput"){
output.push(deviceInfos[i]);
}
} */
console.log("Should only be audio output");
//console.log(output);
//ipcRenderer.send('deviceList', deviceInfos);
})
}
}
if ("changeVideoDevice" in args[0]) {
changeVideoDeviceById(args[0].changeVideoDevice);
}
if ("nativeList" in args[0]){
//console.log("nativeList got");
event.returnValue = true;
}
if ("changeAudioDevice" in args[0]) {
changeAudioDeviceById(args[0].changeAudioDevice);
}
if ("changeAudioOutputDevice" in args[0]) {
//args[0].data.menu = menu || false;
//args[0].data.eleId = ele.id || false;
//args[0].data.UUID = ele.dataset.UUID || false;
//args[0].deviceInfos;
//args[0].data.params = params;
if ("data" in args[0]){
setSink(storedEle, args[0].changeAudioOutputDevice);
storedEle.manualSink = args[0].changeAudioOutputDevice;
//storedEle.manualSink = args[0].changeAudioOutputDevice;
} else if (typeof changeAudioOutputDeviceById === "function"){
changeAudioOutputDeviceById(args[0].changeAudioOutputDevice);
} else {
changeAudioOutputDeviceByIdThirdParty(args[0].changeAudioOutputDevice);
}
storedEle = null;
}
} catch(e){
console.error(e);
}
})
function setSink(ele, id){
ele.setSinkId(id).then(() => {
console.log("New Output Device:" + id);
}).catch(error => {
console.error(error);
});
}
function changeAudioOutputDeviceByIdThirdParty(deviceID){
console.log("Output deviceID: "+deviceID);
document.querySelectorAll("audio, video").forEach(ele=>{
try {
if (ele.manualSink){
setSink(ele,ele.manualSink);
} else {
setSink(ele,deviceID);
}
} catch(e){}
});
document.querySelectorAll('iframe').forEach( item =>{
try{
item.contentWindow.document.body.querySelectorAll("audio, video").forEach(ele=>{
try {
if (ele.manualSink){
setSink(ele,ele.manualSink);
} else {
setSink(ele,deviceID);
}
} catch(e){}
});
} catch(e){}
});
}
function enumerateDevicesThirdParty() {
if (typeof navigator.enumerateDevices === "function") {
return navigator.enumerateDevices();
} else if (typeof navigator.mediaDevices === "object" && typeof navigator.mediaDevices.enumerateDevices === "function") {
return navigator.mediaDevices.enumerateDevices();
} else {
return new Promise((resolve, reject) => {
try {
if (window.MediaStreamTrack == null || window.MediaStreamTrack.getSources == null) {
throw new Error();
}
window.MediaStreamTrack.getSources((devices) => {
resolve(devices
.filter(device => {
return device.kind.toLowerCase() === "video" || device.kind.toLowerCase() === "videoinput";
})
.map(device => {
return {
deviceId: device.deviceId != null ? device.deviceId : ""
, groupId: device.groupId
, kind: "videoinput"
, label: device.label
, toJSON: /* */ function() {
return this;
}
};
}));
});
} catch (e) {}
});
}
}
function requestOutputAudioStream() {
console.log("requestOutputAudioStream");
return navigator.mediaDevices.getUserMedia({audio: true, video: false}).then(function(stream) { // Apple needs thi to happen before I can access EnumerateDevices.
return enumerateDevicesThirdParty().then(function(deviceInfos) {
console.log("enumerateDevicesThirdParty");
stream.getTracks().forEach(function(track) { // We don't want to keep it without audio; so we are going to try to add audio now.
track.stop(); // I need to do this after the enumeration step, else it breaks firefox's labels
});
console.log(deviceInfos);
return deviceInfos;
});
});
}