diff --git a/js/background.js b/js/background.js index b55a422..bf388fc 100644 --- a/js/background.js +++ b/js/background.js @@ -1,5 +1,4 @@ let proxies = []; // Initially empty, will be populated from local storage or fetched from the server - let currentProxyIndex = 0; // Start with the first proxy let proxyEnabled = false; @@ -191,6 +190,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { sendResponse({ status: "error", message: "No public proxy available at this moment. Please configure a custom proxy in the settings." }); } }); + return true; // For async response } else if (message.action === "disableProxy") { console.log("Disabling proxy..."); clearProxySettings(); @@ -238,6 +238,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { sendResponse({ status: "error", message: "No public proxy available at this moment. Please configure a custom proxy in the settings." }); } }); + return true; // For async response } else if (message.type === "disabled") { clearProxySettings(); chrome.storage.local.set({ proxyEnabled: false, proxyType: null }); @@ -252,6 +253,19 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { } }); } + } else if (message.action === "proxyFailed") { + console.log("Proxy setup failed:", message.error); + clearProxySettings(); + chrome.storage.local.set({ proxyEnabled: false, proxyType: null }); + chrome.tabs.query({}, function(tabs) { + for (let tab of tabs) { + chrome.tabs.sendMessage(tab.id, {action: "toggleOff"}, function(response) { + if (chrome.runtime.lastError) { + console.warn("Warning: Could not send message to tab " + tab.id + ". Tab might have been closed.", chrome.runtime.lastError.message); + } + }); + } + }); } else if (message.action === "updateProxies") { console.log("Attempting to update proxies..."); fetchProxies().then(() => { diff --git a/js/options.js b/js/options.js index 12e654c..d96afa6 100644 --- a/js/options.js +++ b/js/options.js @@ -177,8 +177,16 @@ saveSettings.addEventListener("click", () => { statusMessage.style.fontFamily = "Arial"; statusMessage.style.fontWeight = "bold"; applyProxySettings(proxyIP.value, proxyPort.value, filteredExceptions); - chrome.runtime.sendMessage({ action: "updateProxy", type: "custom", proxy: { host: proxyIP.value, port: parseInt(proxyPort.value) }, exceptions: filteredExceptions }); - clearStatusMessage(); + + // Send response to the message sender + chrome.runtime.sendMessage({ action: "updateProxy", type: "custom", proxy: { host: proxyIP.value, port: parseInt(proxyPort.value) }, exceptions: filteredExceptions }, (response) => { + if (chrome.runtime.lastError) { + console.error("Error sending message:", chrome.runtime.lastError); + } else { + console.log("Response received:", response); + } + clearStatusMessage(); + }); } }); }) @@ -189,6 +197,16 @@ saveSettings.addEventListener("click", () => { statusMessage.style.fontFamily = "Arial"; statusMessage.style.fontWeight = "bold"; chrome.proxy.settings.clear({}); + + // Send a message indicating proxy setup failed + chrome.runtime.sendMessage({ action: "proxyFailed", error: error }, (response) => { + if (chrome.runtime.lastError) { + console.error("Error sending message:", chrome.runtime.lastError); + } else { + console.log("Response received for proxy failure:", response); + } + clearStatusMessage(); + }); }) .finally(() => { // Re-enable the saveSettings button after the check is complete diff --git a/js/popup.js b/js/popup.js index ffb3083..f726abe 100644 --- a/js/popup.js +++ b/js/popup.js @@ -158,6 +158,15 @@ document.addEventListener("DOMContentLoaded", () => { statusMessage.style.color = "#f39c12"; // Orange for loading } else if (message.action === "updateStatus") { updateStatusFromBackground(message); + } else if (message.action === "toggleOff") { + // This is for when a proxy setup fails + proxyToggle.checked = false; + statusMessage.textContent = "Failed to set up proxy. Please check your settings."; + statusMessage.style.color = "#e74c3c"; // Red for error + // You might want to clear this message after some time or on interaction + setTimeout(() => { + statusMessage.textContent = ""; + }, 5000); // Clear after 5 seconds } });