(tabId, url, source = "background_update")
| 216 | |
| 217 | // Function to update the server with the current URL |
| 218 | async function updateServerWithUrl(tabId, url, source = "background_update") { |
| 219 | if (!url) { |
| 220 | console.error("Cannot update server with empty URL"); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | console.log(`Updating server with URL for tab ${tabId}: ${url}`); |
| 225 | |
| 226 | // Get the saved settings |
| 227 | chrome.storage.local.get(["browserConnectorSettings"], async (result) => { |
| 228 | const settings = result.browserConnectorSettings || { |
| 229 | serverHost: "localhost", |
| 230 | serverPort: 3025, |
| 231 | }; |
| 232 | |
| 233 | // Maximum number of retry attempts |
| 234 | const maxRetries = 3; |
| 235 | let retryCount = 0; |
| 236 | let success = false; |
| 237 | |
| 238 | while (retryCount < maxRetries && !success) { |
| 239 | try { |
| 240 | // Send the URL to the server |
| 241 | const serverUrl = `http://${settings.serverHost}:${settings.serverPort}/current-url`; |
| 242 | console.log( |
| 243 | `Attempt ${ |
| 244 | retryCount + 1 |
| 245 | }/${maxRetries} to update server with URL: ${url}` |
| 246 | ); |
| 247 | |
| 248 | const response = await fetch(serverUrl, { |
| 249 | method: "POST", |
| 250 | headers: { |
| 251 | "Content-Type": "application/json", |
| 252 | }, |
| 253 | body: JSON.stringify({ |
| 254 | url: url, |
| 255 | tabId: tabId, |
| 256 | timestamp: Date.now(), |
| 257 | source: source, |
| 258 | }), |
| 259 | // Add a timeout to prevent hanging requests |
| 260 | signal: AbortSignal.timeout(5000), |
| 261 | }); |
| 262 | |
| 263 | if (response.ok) { |
| 264 | const responseData = await response.json(); |
| 265 | console.log( |
| 266 | `Successfully updated server with URL: ${url}`, |
| 267 | responseData |
| 268 | ); |
| 269 | success = true; |
| 270 | } else { |
| 271 | console.error( |
| 272 | `Server returned error: ${response.status} ${response.statusText}` |
| 273 | ); |
| 274 | retryCount++; |
| 275 | // Wait before retrying |
no outgoing calls
no test coverage detected