forked from jason53415/blockly-app
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpreload.js
398 lines (390 loc) · 11.4 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
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
const { contextBridge, ipcRenderer, clipboard } = require('electron');
const { PythonShell } = require('python-shell');
const path = require('path');
const fs = require('fs');
const os = require('os');
const https = require('https');
const Store = require('electron-store');
const dateformat = require('dateformat');
const download = require('download-git-repo');
const showdown = require('showdown');
const { v4: uuid4 } = require('uuid');
const { machineIdSync } = require('node-machine-id');
// const ElectronGoogleOAuth2 = require('@getstation/electron-google-oauth2').default;
const env = require('dotenv').config({ path: path.join(__dirname, '.env') });
if (env.error) {
throw result.error;
}
const schema = {
refresh_token: {
type: "string",
default: ""
},
project_path: {
type: "string",
default: path.join(os.homedir(), 'Desktop')
},
custom_python: {
type: "boolean",
default: false
},
custom_python_path: {
type: "string",
default: ""
}
};
const store = new Store({schema});
// const myApiOauth = new ElectronGoogleOAuth2(
// process.env.GOOGLE_OAUTH2_CLIENT_ID,
// process.env.GOOGLE_OAUTH2_PASSWORD,
// [], { successRedirectURL: 'https://www.paia-arena.com/' }
// );
const fileWatchers = {};
const session_id = uuid4();
let access_token = "";
let time = new Date().getTime();
let user_id = "";
contextBridge.exposeInMainWorld('deeplink', {
onLogin: (callback) => ipcRenderer.on('login', callback)
});
contextBridge.exposeInMainWorld('clipboard', clipboard);
contextBridge.exposeInMainWorld('dateformat', dateformat);
contextBridge.exposeInMainWorld('fs', fs);
contextBridge.exposeInMainWorld('https', {
get: (url, filepath, callback, end, error) => {
const file = fs.createWriteStream(filepath);
https.get(url, (res) => {
res.on('data', (d) => {
file.write(d);
callback(d);
});
res.on('end', () => {
file.close();
end();
});
}).on('error', error);
}
});
contextBridge.exposeInMainWorld('markdown', {
convert: (text) => {
const converter = new showdown.Converter();
return converter.makeHtml(text);
}
});
contextBridge.exposeInMainWorld('app', {
getVersion: () => {
return ipcRenderer.sendSync('getVersion');
}
});
contextBridge.exposeInMainWorld('popup', {
alert: (msg) => {
fixedAlert(msg);
},
confirm: (msg) => {
return fixedConfirm(msg);
}
});
contextBridge.exposeInMainWorld('python_env', {
run: (options, script, file, cwd) => {
const old_cwd = process.cwd();
process.chdir(cwd);
let python = new PythonShell(script, options);
python.on('message', function (message) {
const full_message = document.getElementById('content_console').textContent + message + '\n';
document.getElementById('content_console').textContent = full_message.slice(-50000);
const e = document.getElementById('console-body');
e.scrollTo(0, e.scrollHeight);
});
python.on('stderr', function (stderr) {
document.getElementById('content_console').textContent += stderr + '\n';
const e = document.getElementById('console-body');
e.scrollTo(0, e.scrollHeight);
});
python.on('close', function () {
document.getElementById('content_console').textContent += '> Python program finished\n';
const e = document.getElementById('console-body');
e.scrollTo(0, e.scrollHeight);
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
process.chdir(old_cwd);
});
python.on('error', function () {
window.alert('Error: process exited with code ' + python.exitCode);
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
process.chdir(old_cwd);
});
},
getCustom: () => {
return {
custom_python: store.get('custom_python'),
custom_python_path: store.get('custom_python_path'),
};
},
setCustom: (custom_python, custom_python_path) => {
store.set('custom_python', custom_python);
store.set('custom_python_path', custom_python_path);
}
});
contextBridge.exposeInMainWorld('file', {
write: (file, data) => {
fs.writeFileSync(file, data, (err) => {
if (err) window.alert(err);
console.log('The file has been saved at ' + file);
});
},
read: (file) => {
return fs.readFileSync(file, 'utf8', (err, data) => {
if (err) window.alert(err);
return data;
});
},
watch: (file, listener) => {
fileWatchers[file] = fs.watch(file, listener);
},
unwatch: (file) => {
if (file in fileWatchers) {
fileWatchers[file].close();
delete fileWatchers[file];
}
}
});
contextBridge.exposeInMainWorld('path', {
select: (options) => {
return ipcRenderer.sendSync('selectPath', options);
},
save: (options) => {
return ipcRenderer.sendSync('savePath', options);
},
open: (pathname) => {
ipcRenderer.send('openPath', pathname);
},
join: (...args) => {
return path.join(...args)
},
dirname: () => {
return __dirname;
},
basename: (pathname) => {
return path.basename(pathname);
},
homedir: () => {
return os.homedir();
}
});
contextBridge.exposeInMainWorld('project', {
getPath: () => {
return store.get('project_path');
},
setPath: (path) => {
store.set('project_path', path);
},
open: (pathname) => {
ipcRenderer.send('openPath', pathname);
}
});
contextBridge.exposeInMainWorld('dir', {
copy: (src, dest) => {
copyDir(src, dest);
}
});
contextBridge.exposeInMainWorld('repo', {
download: (repo, dest, callback) => {
download(repo, dest, callback);
}
});
contextBridge.exposeInMainWorld('paia', {
auth: async (url_token=null) => {
let refresh_token = store.get('refresh_token');
if (url_token === null && refresh_token.length == 0) {
return {ok: false, content: ''};
} else {
if (url_token !== null && refresh_token.length != 0) {
if (url_token === refresh_token) {
fixedAlert('您已登入此帳號');
return {ok: true, content: ''};
} else if (fixedConfirm('您正在嘗試從外部登入 PAIA,並將登出現有帳號,是否繼續執行?')) {
refresh_token = url_token;
store.set('refresh_token', url_token);
}
} else if (url_token !== null) {
refresh_token = url_token;
store.set('refresh_token', url_token);
}
const data = { refresh: refresh_token };
try {
const response = await paiaAPI("POST", "auth/token/refresh", data, null, ver="v1");
if (response.ok) {
const content = await response.json();
access_token = content.access;
return {ok: response.ok, content: content};
} else {
store.set('refresh_token', '');
return {ok: response.ok, content: `Error: ${response.status}`};
}
} catch (error) {
return {ok: false, content: error};;
}
}
},
login: async (email, password) => {
const data = {
"username": email,
"password": password
};
try {
const response = await paiaAPI("POST", "auth/login", data, null);
if (response.ok) {
const content = await response.json();
store.set('refresh_token', content.refresh);
access_token = content.access;
return {ok: response.ok, content: content};
} else {
return {ok: response.ok, content: `Error: ${response.status}`};;
}
} catch (error) {
console.error("Error:", error);
return {ok: false, content: error};
}
},
logout: () => {
store.set('refresh_token', "");
access_token = "";
},
user: async () => {
try {
const response = await paiaAPI("GET", "user/me", null, 'USER_TOKEN');
const content = await response.json();
user_id = content.id.toString();
return {ok: response.ok, content: content};
} catch(error) {
console.error("Error:", error);
return {ok: false, content: error};
}
},
ads: () => {
return `${env.parsed.PAIA_APP_HOST}/ads`
},
redirect: () => {
return `${env.parsed.PAIA_APP_HOST}/login?app=true`
},
ga: async(name, params) => {
await gaAPI(name, params);
}
});
contextBridge.exposeInMainWorld('api', {
paia: async (method, url, data, auth, ver=env.parsed.PAIA_API_VERSION, filename=null) => {
if (filename !== null) {
data = new FormData();
const name = path.basename(filename);
const file = new File([fs.readFileSync(filename, 'utf8')], name);
data.append("files", file, name);
}
try {
const response = await paiaAPI(method, url, data, auth, ver);
return {ok: response.ok, content: await response.json()};
} catch(error) {
console.error("Error:", error);
return {ok: false, content: error};
}
},
github: async (method, url, data) => {
try {
const response = await githubAPI(method, url, data);
return {ok: response.ok, content: await response.json()};
} catch(error) {
console.error("Error:", error);
return {ok: false, content: error};
}
}
});
const paiaAPI = function(method, url, data, auth, ver=env.parsed.PAIA_API_VERSION) {
const headers = {};
if (auth == 'USER_TOKEN') {
headers['Authorization'] = `Bearer ${access_token}`;
} else if (auth == 'DESKTOP_TOKEN') {
headers['Authorization'] = `Bearer ${env.parsed.PAIA_DESKTOP_TOKEN}`;
}
if (data instanceof FormData) {
return fetch(`${env.parsed.PAIA_API_HOST}/api/${ver}/${url}`, {
method: method,
headers: headers,
body: data
});
} else {
headers['Content-Type'] = 'application/json';
return fetch(`${env.parsed.PAIA_API_HOST}/api/${ver}/${url}`, {
method: method,
headers: headers,
body: (data !== null)? JSON.stringify(data) : null
});
}
};
const githubAPI = function(method, url, data) {
const headers = {};
headers['Content-Type'] = 'application/json';
headers['Accept'] = 'application/vnd.github.v3+json';
return fetch(`https://api.github.com/${url}`, {
method: method,
headers: headers,
body: (data !== null)? JSON.stringify(data) : null
});
};
const gaAPI = function(name, params) {
const headers = {};
headers['Content-Type'] = 'application/json';
const prev_time = time;
time = new Date().getTime();
return fetch(`https://google-analytics.com/mp/collect?measurement_id=${env.parsed.GA_MEASUREMENT_ID}&api_secret=${env.parsed.GA_API_SECRET}`, {
method: "POST",
headers: headers,
body: JSON.stringify({
client_id: machineIdSync(),
user_id: user_id,
events: [{
name: name,
params: {
engagement_time_msec: (time - prev_time).toString(),
session_id: session_id,
...params
}
}]
})
});
}
const copyDir = function(src, dest) {
try {
const destDir = path.join(dest, path.basename(src));
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir);
}
if (fs.lstatSync(src).isDirectory()) {
const files = fs.readdirSync(src);
files.forEach((file) => {
const curSrc = path.join(src, file);
if (fs.lstatSync(curSrc).isDirectory()) {
copyDir(curSrc, destDir);
} else {
fs.copyFileSync(curSrc, path.join(destDir, file));
}
});
}
} catch(err) {
fixedAlert(err);
}
}
const fixedAlert = function(msg) {
window.alert(msg);
if (process.platform === 'win32') {
ipcRenderer.send('fixFocus');
}
};
const fixedConfirm = function(msg) {
const res = window.confirm(msg);
if (process.platform === 'win32') {
ipcRenderer.send('fixFocus');
}
return res;
}