-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromesetup.js
87 lines (79 loc) · 2.64 KB
/
chromesetup.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
const { exec } = require('child_process');
const axios = require('axios');
const prompt = require("prompt-sync")({ sigint: true });
const { prompts } = require('./config');
function getWsEndpoint() {
if (prompts) {
return new Promise((resolve, reject) => {
const res = prompt("Chrome must be restarted before continuing. Do you wish to proceed? (Y/N): ");
if (res.trim().toLowerCase() !== 'y') {
console.log("Exiting");
process.exit();
}
// Close all open instances of Chrome
const closeChromeCommand = process.platform === 'win32' ? 'taskkill /im chrome.exe /f' : 'pkill chrome';
exec(closeChromeCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error closing Chrome: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.error(`Error closing Chrome: ${stderr}`);
reject(stderr);
return;
}
console.log("All Chrome instances closed successfully.");
// Open a new Chrome instance using Windows' Run prompt
const openChromeCommand = 'start chrome --remote-debugging-port=9222';
exec(openChromeCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error opening Chrome: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.error(`Error opening Chrome: ${stderr}`);
reject(stderr);
return;
}
console.log("New Chrome instance opened successfully.");
prompt("Log into Genote, then press any key to continue.");
// Make a GET request to fetch the webSocketDebuggerUrl
setTimeout(() => {
axios.get('http://127.0.0.1:9222/json/version')
.then(response => {
const wsDbUrl = response.data.webSocketDebuggerUrl;
console.log("WebSocket Debugger URL:", wsDbUrl);
resolve(wsDbUrl);
})
.catch(error => {
console.error('Error fetching version information:', error);
reject(error);
});
}, 1000);
});
});
});
}
else {
return new Promise((resolve, reject) => {
// Make a GET request to fetch the webSocketDebuggerUrl
setTimeout(() => {
axios.get('http://127.0.0.1:9222/json/version')
.then(response => {
const wsDbUrl = response.data.webSocketDebuggerUrl;
console.log("WebSocket Debugger URL:", wsDbUrl);
resolve(wsDbUrl);
})
.catch(error => {
console.error('Error fetching version information. Please restart Chrome with remote-debugging on port 9222.', error);
reject(error);
});
}, 1000);
});
}
}
module.exports = {
getWsEndpoint
};