|
7 | 7 | <title>Sploder</title> |
8 | 8 | </head> |
9 | 9 | <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); |
14 | 12 |
|
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'); |
28 | 18 |
|
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 | + } |
35 | 23 |
|
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); |
40 | 63 | } |
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); |
46 | 82 | } |
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); |
49 | 105 | } |
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); |
58 | 125 | // Custom built code to check if user is online. |
59 | 126 | // I have no idea how or why this works. |
60 | 127 | function online() { |
61 | 128 | if(document.getElementById("content-frame").contentWindow.location.href != "chrome-error://chromewebdata/"){ |
62 | 129 | page = document.getElementById("content-frame").contentWindow.location.href; |
63 | 130 | } else if(page == undefined) { |
64 | | - page = url; |
| 131 | + page = window.initialURL || ''; // Use the stored global URL |
65 | 132 | } |
66 | 133 | if(page.includes("offline.html")){ |
67 | 134 | page = "test"; |
68 | 135 | } else { |
69 | 136 |
|
70 | 137 |
|
71 | 138 | const frame = document.getElementById('content-frame'); |
72 | | - var url = "_[[URL]]_/php/ping.php"; |
| 139 | + var pingUrl = "_[[URL]]_/php/ping.php"; |
73 | 140 | var xhttp = new XMLHttpRequest(); |
74 | 141 | xhttp.onreadystatechange = function() { |
75 | 142 |
|
|
91 | 158 | } |
92 | 159 |
|
93 | 160 | }; |
94 | | - xhttp.open("GET", url, true); |
| 161 | + xhttp.open("GET", pingUrl, true); |
95 | 162 | xhttp.send(); |
96 | 163 |
|
97 | 164 | } |
98 | 165 | } |
| 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 | + |
99 | 175 | // Prevent scrolling using JavaScript |
100 | 176 | window.addEventListener('scroll', () => { |
101 | 177 | window.scrollTo(0, 0); // Keep the window locked at the top |
|
136 | 212 | width: 100%; |
137 | 213 | height: 100%; |
138 | 214 | display: flex; |
| 215 | + user-select: none; |
139 | 216 | -webkit-user-select: none; |
140 | 217 | -webkit-app-region: drag; |
141 | 218 | } |
142 | 219 | .title-bar-text { |
| 220 | + user-select: none; |
143 | 221 | -webkit-user-select: none; |
144 | 222 | -webkit-app-region: drag; |
145 | 223 | } |
|
154 | 232 | height: 16px; |
155 | 233 | margin-left: 3px; |
156 | 234 | margin-right: 3px; |
| 235 | + user-select: none; |
157 | 236 | -webkit-user-select: none; |
158 | 237 | -webkit-app-region: drag; |
159 | 238 | } |
160 | 239 | .candrag { |
| 240 | + user-select: none; |
161 | 241 | -webkit-user-select: none; |
162 | 242 | -webkit-app-region: drag; |
163 | 243 | } |
164 | 244 | </style> |
165 | 245 | <body> |
166 | 246 |
|
167 | 247 | <div class="window" style="width: 100%; height: calc(100% - 3px)"> |
168 | | - <div class="title-bar"> |
| 248 | + <div class="title-bar" id="titlebar"> |
169 | 249 | <img class="title-bar-icon" src="images/icon.png"> |
170 | 250 | <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> |
172 | 252 | <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> |
174 | 254 | <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> |
176 | 256 | </div> |
177 | 257 | </div> |
178 | 258 | <div class="window-body" |
179 | 259 | style="width: 100%; height: calc(100% - 50px); margin-left: 3px; margin-top:0px; padding-bottom: -50px;"> |
180 | 260 | <iframe id="content-frame" onload="online();" src=""> |
181 | 261 | </iframe> |
182 | | - <script> |
183 | | - document.getElementById("content-frame").setAttribute('src', url); |
184 | | - </script> |
| 262 | + <!-- The src will be set by initializeApp function --> |
185 | 263 |
|
186 | 264 | </div> |
187 | 265 | </div> |
|
0 commit comments