-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
165 lines (145 loc) · 3.69 KB
/
script.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
const version = 16;
navigator.serviceWorker && navigator.serviceWorker.register('/sw.js?ver=' + version).then(function (registration) {
console.log('Service worker registered with scope:', registration.scope);
});
const display = document.getElementById("display");
const buttons = document.querySelectorAll(".append");
const buttonClear = document.getElementById("button-clear");
const buttonEqual = document.getElementById("button-equal");
let errSetting = 0;
let counter = 0;
buttons.forEach(item => {
let val = item.dataset.purpose;
console.log("Loading: " + val);
item.addEventListener('click', function(){
appendDisplay(val)
})
})
buttonClear.addEventListener('click', function(){
clearDisplay()
})
buttonEqual.addEventListener('click', function(){
evaluateDisplay();
errorSetting();
})
document.addEventListener("keydown", function(event) {
if (event.defaultPrevented) {
return; // Do nothing if the event was already processed
}
let val = null;
switch (event.key) {
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
val = event.key;
break;
case "/":
case "*":
case "+":
case "-":
case "(":
case ")":
case ".":
val = event.key;
break;
case "Enter":
val = "=";
break;
case "=":
val = "=";
break;
case "Backspace":
val = "Del";
break;
case "Escape":
val = "C";
break;
case "C":
val = "C";
break;
case "c":
val = "C";
break;
default:
return; // Do not act on other keys
}
console.log("Keypress detected: " + val);
if (val === "C") {
clearDisplay();
} else if (val === "=") {
evaluateDisplay();
} else {
appendDisplay(val);
}
event.preventDefault();
});
function errorSetting() {
counter++;
if (counter == 5) {
errSetting = 1;
}
}
setInterval(counter = 0, 5000);
Window.onload = function size() {
window.resizeTo(700, window.screen.availHeight / 2);
}
function appendDisplay(char) {
if (char == "Del") {
display.textContent = display.textContent.slice(0, -1);
} else {
if (display.textContent === "0") {
display.textContent = char;
} else {
display.textContent += char;
}
}
}
function clearDisplay() {
display.textContent = "0";
}
function evaluateDisplay() {
try {
//let result = eval(display.textContent);
//let result = new Function("return " + display.textContent);
display.textContent = math.evaluate(display.textContent);
} catch (error) {
if (errSetting == 0) {
display.textContent = "Error";
} else {
display.textContent = error;
}
}
}
function checkForUpdates() {
console.log("Checking for updates 1/2");
if ('serviceWorker' in navigator) {
console.log("Checking for updates 2/2");
navigator.serviceWorker.register('/sw.js?ver=' + version).then((registration) => {
const storedVersion = localStorage.getItem('sw-version');
if (storedVersion !== version) {
console.log("Installing updates.");
// Log the current and new version for debugging
console.log('Current version:', storedVersion);
console.log('New version:', version);
registration.update().then(() => {
localStorage.setItem('sw-version', version);
console.log("Updates installed.");
}).catch((updateError) => {
console.error('Error installing update:', updateError);
});
} else {
console.log("No updates found.");
}
}).catch((error) => {
console.error('Error registering service worker:', error);
});
}
}
setTimeout(checkForUpdates, 2000);