-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
29 lines (26 loc) · 1.03 KB
/
calculator.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
function calculateAndSendResponse() {
const urlParams = new URLSearchParams(window.location.search);
const calculation = urlParams.get('calculation');
if (calculation) {
try {
// Perform calculation
const result = eval(calculation);
// Send result back in JSON format
const jsonResponse = { result: result };
return JSON.stringify(jsonResponse);
} catch (error) {
console.error('Error occurred during calculation:', error);
// Send error back in JSON format
const errorResponse = { error: error.message };
return JSON.stringify(errorResponse);
}
} else {
console.error('No calculation specified in the URL!');
// Send error back in JSON format
const errorResponse = { error: 'No calculation specified' };
return JSON.stringify(errorResponse);
}
}
// Initiate the response sending
const response = calculateAndSendResponse();
window.parent.postMessage(response, '*');