Skip to content

Commit c497772

Browse files
Improve security
Oh my god what is this abomination.... I will one day have to rewrite this entirely... It is so, so, so, bad... I hate working with this codebase...
1 parent 0bb42ee commit c497772

File tree

3 files changed

+258
-65
lines changed

3 files changed

+258
-65
lines changed

src/local/start.html

Lines changed: 130 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,136 @@
77
<title>Sploder</title>
88
</head>
99
<script>
10-
// Check get parameter to get URL
11-
const queryString = window.location.search;
12-
const urlParams = new URLSearchParams(queryString);
13-
const url = urlParams.get('url');
10+
// Wait for DOM to be fully loaded before initializing
11+
document.addEventListener('DOMContentLoaded', initializeApp);
1412

15-
// If the URL is not provided, redirect to the update page
16-
if (url == null) {
17-
window.location.href = "_[[URL]]_/update";
18-
}
19-
20-
var page;
21-
var prevpage;
22-
var pagestat;
23-
// Windows XP theme
24-
const { remote } = require('electron');
25-
const win = remote.getCurrentWindow();
26-
function toggleMaximize() {
27-
const maxButton = document.getElementById('max-btn');
13+
function initializeApp() {
14+
// Check get parameter to get URL
15+
const queryString = window.location.search;
16+
const urlParams = new URLSearchParams(queryString);
17+
const url = urlParams.get('url');
2818

29-
// Check the current aria-label and toggle it
30-
if (!win.isMaximized()) {
31-
win.maximize();
32-
maxButton.setAttribute('aria-label', 'Restore');
33-
// Add logic to handle the "Restore" functionality if needed
34-
} else {
19+
// If the URL is not provided, redirect to the update page
20+
if (url == null) {
21+
window.location.href = "_[[URL]]_/update";
22+
}
3523

36-
win.unmaximize();
37-
maxButton.setAttribute('aria-label', 'Maximize');
38-
// Add logic to handle the "Maximize" functionality if needed
39-
}
24+
// Store URL in a global variable for other functions to access
25+
window.initialURL = url;
26+
27+
var page;
28+
var prevpage;
29+
var pagestat;
30+
31+
// Set up document content
32+
document.getElementById("content-frame").setAttribute('src', url);
33+
34+
// Initialize the button state
35+
checkmax();
36+
37+
// Set up additional window state listeners
38+
setTimeout(() => {
39+
window.addEventListener('resize', () => {
40+
setTimeout(checkmax, 50);
41+
});
42+
43+
// Additional event listener for the title bar
44+
const titleBar = document.querySelector('.title-bar');
45+
if (titleBar) {
46+
titleBar.addEventListener('mouseup', () => setTimeout(checkmax, 50));
47+
titleBar.addEventListener('dblclick', () => setTimeout(checkmax, 50));
48+
}
49+
50+
// Listen for window state changes directly from the main process
51+
if (window.electronAPI && window.electronAPI.onWindowStateChange) {
52+
window.electronAPI.onWindowStateChange((isMaximized) => {
53+
const maxButton = document.getElementById('max-btn');
54+
if (maxButton) {
55+
maxButton.setAttribute('aria-label', isMaximized ? 'Restore' : 'Maximize');
56+
}
57+
});
58+
}
59+
}, 100);
60+
61+
// Start the interval functions
62+
setInterval(discordrpc, 15000);
4063
}
41-
function checkmax() {
42-
const maxButton = document.getElementById('max-btn');
43-
44-
if (win.isMaximized()) {
45-
maxButton.setAttribute('aria-label', 'Restore');
64+
65+
// Window control functions using IPC
66+
async function toggleMaximize() {
67+
try {
68+
const maxButton = document.getElementById('max-btn');
69+
if (!maxButton) return; // Safety check
70+
71+
// Call the maximize function which returns true if window is now maximized
72+
const isMaximized = await window.electronAPI.maximizeWindow();
73+
74+
// Update button appearance immediately based on the returned state
75+
if (isMaximized) {
76+
maxButton.setAttribute('aria-label', 'Restore');
77+
} else {
78+
maxButton.setAttribute('aria-label', 'Maximize');
79+
}
80+
} catch (error) {
81+
console.error('Error in toggleMaximize:', error);
4682
}
47-
else {
48-
maxButton.setAttribute('aria-label', 'Maximize');
83+
}
84+
85+
async function checkmax() {
86+
try {
87+
const maxButton = document.getElementById('max-btn');
88+
if (!maxButton) return; // Safety check
89+
90+
// Get the current window state
91+
const isMaximized = await window.electronAPI.isWindowMaximized();
92+
93+
// Update the button's appearance based on the window state
94+
if (isMaximized) {
95+
if (maxButton.getAttribute('aria-label') !== 'Restore') {
96+
maxButton.setAttribute('aria-label', 'Restore');
97+
}
98+
} else {
99+
if (maxButton.getAttribute('aria-label') !== 'Maximize') {
100+
maxButton.setAttribute('aria-label', 'Maximize');
101+
}
102+
}
103+
} catch (error) {
104+
console.error('Error in checkmax:', error);
49105
}
50-
}
51-
var checkmax = setInterval(checkmax, 100);
52-
// Keep updating the Discord RPC message
53-
var rpcinfo;
54-
function discordrpc() {
55-
rpcinfo = document.getElementById("content-frame").contentWindow.window.rpcinfo;
56-
}
57-
setInterval(discordrpc, 15000);
106+
}
107+
108+
// Run checkmax at a regular interval to keep the button state in sync
109+
setInterval(checkmax, 100);
110+
111+
// Add event listeners for window state changes
112+
window.addEventListener('resize', checkmax);
113+
document.addEventListener('mouseup', checkmax);
114+
115+
// Keep updating the Discord RPC message
116+
var rpcinfo;
117+
function discordrpc() {
118+
try {
119+
rpcinfo = document.getElementById("content-frame").contentWindow.window.rpcinfo;
120+
} catch (error) {
121+
console.error('Error in discordrpc:', error);
122+
}
123+
}
124+
setInterval(discordrpc, 15000);
58125
// Custom built code to check if user is online.
59126
// I have no idea how or why this works.
60127
function online() {
61128
if(document.getElementById("content-frame").contentWindow.location.href != "chrome-error://chromewebdata/"){
62129
page = document.getElementById("content-frame").contentWindow.location.href;
63130
} else if(page == undefined) {
64-
page = url;
131+
page = window.initialURL || ''; // Use the stored global URL
65132
}
66133
if(page.includes("offline.html")){
67134
page = "test";
68135
} else {
69136

70137

71138
const frame = document.getElementById('content-frame');
72-
var url = "_[[URL]]_/php/ping.php";
139+
var pingUrl = "_[[URL]]_/php/ping.php";
73140
var xhttp = new XMLHttpRequest();
74141
xhttp.onreadystatechange = function() {
75142

@@ -91,11 +158,20 @@
91158
}
92159

93160
};
94-
xhttp.open("GET", url, true);
161+
xhttp.open("GET", pingUrl, true);
95162
xhttp.send();
96163

97164
}
98165
}
166+
// Add window focus and blur listeners to check window state
167+
window.addEventListener('focus', function() {
168+
setTimeout(checkmax, 50);
169+
});
170+
171+
window.addEventListener('blur', function() {
172+
setTimeout(checkmax, 50);
173+
});
174+
99175
// Prevent scrolling using JavaScript
100176
window.addEventListener('scroll', () => {
101177
window.scrollTo(0, 0); // Keep the window locked at the top
@@ -136,10 +212,12 @@
136212
width: 100%;
137213
height: 100%;
138214
display: flex;
215+
user-select: none;
139216
-webkit-user-select: none;
140217
-webkit-app-region: drag;
141218
}
142219
.title-bar-text {
220+
user-select: none;
143221
-webkit-user-select: none;
144222
-webkit-app-region: drag;
145223
}
@@ -154,34 +232,34 @@
154232
height: 16px;
155233
margin-left: 3px;
156234
margin-right: 3px;
235+
user-select: none;
157236
-webkit-user-select: none;
158237
-webkit-app-region: drag;
159238
}
160239
.candrag {
240+
user-select: none;
161241
-webkit-user-select: none;
162242
-webkit-app-region: drag;
163243
}
164244
</style>
165245
<body>
166246

167247
<div class="window" style="width: 100%; height: calc(100% - 3px)">
168-
<div class="title-bar">
248+
<div class="title-bar" id="titlebar">
169249
<img class="title-bar-icon" src="images/icon.png">
170250
<div class="title-bar-text">Sploder</div>
171-
<div class="titlebar-movable"></div>
251+
<div class="titlebar-movable" onmouseup="setTimeout(checkmax, 50)" onmousedown="setTimeout(checkmax, 500)"></div>
172252
<div class="title-bar-controls">
173-
<button id="min-btn" onclick="win.minimize()" aria-label="Minimize"></button>
253+
<button id="min-btn" onclick="window.electronAPI.minimizeWindow()" aria-label="Minimize"></button>
174254
<button id="max-btn" onclick="toggleMaximize()" aria-label="Maximize"></button>
175-
<button id="close-btn" onclick="win.close()" aria-label="Close"></button>
255+
<button id="close-btn" onclick="window.electronAPI.closeWindow()" aria-label="Close"></button>
176256
</div>
177257
</div>
178258
<div class="window-body"
179259
style="width: 100%; height: calc(100% - 50px); margin-left: 3px; margin-top:0px; padding-bottom: -50px;">
180260
<iframe id="content-frame" onload="online();" src="">
181261
</iframe>
182-
<script>
183-
document.getElementById("content-frame").setAttribute('src', url);
184-
</script>
262+
<!-- The src will be set by initializeApp function -->
185263

186264
</div>
187265
</div>

0 commit comments

Comments
 (0)