-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
175 lines (147 loc) · 6.11 KB
/
preload.js
File metadata and controls
175 lines (147 loc) · 6.11 KB
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
const { electron, contextBridge, ipcRenderer, powerMonitor } = require('electron')
let TemplateFolder = process.resourcesPath+'/templates/';
let ConfigFolder = process.resourcesPath+'/configs/';
const fs = require('fs');
// Detect if running in dev mode
const isDev = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'dev';
console.log('Running in development mode: ', isDev);
if (isDev) {
// If running in dev mode, set the test folder to the current directory
TemplateFolder = __dirname + '/templates/';
ConfigFolder = __dirname + '/configs/';
buildBranch = "dev-build";
} else {
buildBranch = "prod-build";
}
let templates = [];
// First get baked in templates
fs.readdir(TemplateFolder, (err, files) => {
files.forEach(file => {
// if txt file push into list
if (file.endsWith('.txt')) {
// remove .txt from filename
const templateName = file.replace('.txt', '');
const templateData = fs.readFileSync(TemplateFolder + file, 'utf8');
templates.push({
name: templateName,
data: templateData,
dataEncoded: btoa(templateData)
});
}
});
});
// Then get user templates
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])
}
const menuStatus = document.getElementById('menustatus');
menuStatus.innerHTML = buildBranch;
const templateSelector = document.getElementById('templates');
// add templates to select as options
templates.forEach(template => {
const option = document.createElement('option');
option.value = template.name;
option.text = template.name;
option.setAttribute('data-template', template.dataEncoded);
templateSelector.appendChild(option);
});
ipcRenderer.on('workerinfo', (event, workerinfo) => {
const workerText = document.getElementById('thread-graph-bar-text');
const workerPct = document.getElementById('thread-graph-bar-pct');
workerText.innerHTML = workerinfo.workers.count;
const pct = Math.round(workerinfo.workers.count / 200 * 100).toFixed(0);
workerPct.style.width = pct+'%';
});
ipcRenderer.on('sysinfo', (event, sysinfo) => {
const cpuText = document.getElementById('cpu-graph-bar-text');
const cpuPct = document.getElementById('cpu-graph-bar-pct');
const memText = document.getElementById('mem-graph-bar-text');
const memPct = document.getElementById('mem-graph-bar-pct');
const status = document.getElementById('status');
cpuText.innerHTML = Math.round(sysinfo.cpu.utilization_human)+'%';
cpuPct.style.width = Math.round(sysinfo.cpu.utilization_human)+'%';
memText.innerHTML = Math.round(sysinfo.memory.pctUsed)+'%';
memPct.style.width = Math.round(sysinfo.memory.pctUsed)+'%';
// Show load avg
status.innerHTML = 'Load Avg: '+sysinfo.cpu.loadavg[0].toFixed(2)+', '+sysinfo.cpu.loadavg[1].toFixed(2)+', '+sysinfo.cpu.loadavg[2].toFixed(2);
// Update the system information object if null
if (sysInfo === null) {
sysInfo = sysinfo;
}
});
})
contextBridge.exposeInMainWorld(
"api", {
invoke: (channel, data) => {
let validChannels = ["killAllWorkers","pingAllWorkers","startWorker","removeWorker","getTemplates","getUserAgent","getPublicIp","getUserTemplates","addUserTemplate","openUserTemplates"];
if (validChannels.includes(channel)) {
return ipcRenderer.invoke(channel, data);
}
},
},
);
window.addEventListener('contextmenu', (e) => {
e.preventDefault()
ipcRenderer.send('show-context-menu')
})
ipcRenderer.on('context-menu-command', (e, command) => {
if ( command == "recalculateContentLength") {
const payload = document.getElementById('payload');
const payloadText = payload.value;
var splitArray = payloadText.split("\n\n");
var headers = splitArray[0];
var body = splitArray[1];
if ( body.length > 0 ) {
const contentLength = body.length;
// Replace the content length in the payload
const newPayload = payloadText.replace(/Content-Length: \d+/g, 'Content-Length: '+contentLength);
payload.value = newPayload;
}
}
})
let sysInfo = null;
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
function clearLog() {
const logWindow = document.getElementById('log');
logWindow.innerHTML = '';
}
ipcRenderer.on('clearLog', (event) => {
clearLog();
});
ipcRenderer.on('logMessage', (event, logObj) => {
console.log(logObj)
const log = document.getElementById('log');
/* This is how each logline looks line
<div class="logline"><span class="timestamp">[00-00-0000 00:00:00]</span> <span class="worker">(worker-001)</span> <span class="severity">INFO</span> ❘ <span class="message">This is some log message ....</span></div>
*/
const worker = logObj.worker;
const msg = logObj.message;
const severity = logObj.severity;
const newLine = document.createElement('div');
newLine.className = 'logline';
// Get the current timestamp
const now = new Date();
const optionsTime = { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit' };
const optionsDate = { day: "numeric",month: "long", year: "numeric" };
const timeString = now.toLocaleTimeString([], optionsTime);
const dateString = now.toLocaleDateString([], optionsDate);
const logTimeStamp = dateString + " " + timeString;
newLine.innerHTML = '<span class="timestamp">['+logTimeStamp+']</span> <span class="worker">('+worker.padStart(10, ' ')+')</span> <span class="severity '+severity.toLowerCase()+'">'+severity.toUpperCase().padStart(6, ' ')+'</span> ❘ <span class="message">'+msg+'</span>';
log.appendChild(newLine);
if (log.childElementCount > 5) {
log.removeChild(log.firstChild);
}
});