-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (72 loc) · 2.72 KB
/
index.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
const { default: axios } = require('axios');
const { app, BrowserWindow } = require('electron')
const constants = require('./constants')
const open = require('open');
const isElectron = require('is-electron');
if (!isElectron()) {
console.log('use npm start');
process.exit()
}
async function Authenticate() {
if (!app.isReady()) {
await app.whenReady();
}
const win = new BrowserWindow({
width: 600,
height: 900
})
const info = constants.launcherAppClient2;
if (!info) {
throw reject(new Error(`Client ${client} is not valid`))
}
const loginQuery = new URLSearchParams();
loginQuery.append('redirectUrl', info.redirectUrl);
loginQuery.append('client_id', info.clientId);
loginQuery.append('responseType', 'code');
var url = constants.loginURL + '?' + loginQuery.toString()
win.loadURL(url);
win.webContents.setWindowOpenHandler((details) => {
console.log(`url: ` + details.url)
open(details.url);
return {
action: 'deny'
}
})
try {
win.webContents.debugger.attach('1.3');
} catch (err) {
console.log('Debugger attach failed: ', err);
}
win.webContents.debugger.on('detach', (event, reason) => {
console.log('Debugger detached due to: ', reason);
});
win.webContents.debugger.on('message', (event, method, params) => {
if (method === 'Network.responseReceived') {
const url = new URL(params.response.url);
if (url.pathname.endsWith('/id/api/exchange/generate')) {
win.webContents.debugger.sendCommand('Network.getResponseBody', { requestId: params.requestId }).then(function (response) {
if (response.base64Encoded) {
var body = Buffer.from(response.body, 'base64').toString()
} else {
var body = response.body;
}
const parsed = JSON.parse(body);
win.loadFile('result.html').then(() => {
win.webContents.executeJavaScript(`var code='${parsed.code}';document.getElementById('code').innerHTML=code`)
})
});
}
}
})
win.webContents.debugger.sendCommand('Network.enable');
win.webContents.session.webRequest.onBeforeRequest(function (details, cb) {
var url = new URL(details.url);
if (url.pathname.endsWith('/id/api/analytics')) {
return cb({ cancel: true })
}
return cb({ cancel: false })
});
};
app.whenReady().then(() => {
Authenticate();
})