diff --git a/background.js b/background.js new file mode 100644 index 0000000..214f9f5 --- /dev/null +++ b/background.js @@ -0,0 +1,39 @@ +// background.js - Service worker to handle API calls from content script +// This bypasses CORS restrictions by making requests from the extension context + +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + if (request.action === 'placeOrder') { + // Make API call from background script (bypasses CORS) + const { url, orderData } = request; + + fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(orderData) + }) + .then(response => { + if (!response.ok) { + return response.json().then(errorData => { + throw new Error(errorData.message || `HTTP error! status: ${response.status}`); + }); + } + return response.json(); + }) + .then(data => { + sendResponse({ success: true, data: data }); + }) + .catch(error => { + console.error('API Error:', error); + sendResponse({ success: false, error: error.message }); + }); + + return true; // Keep message channel open for async response + } +}); + +// Optional: Listen for extension installation +chrome.runtime.onInstalled.addListener(() => { + console.log('OpenAlgo Extension installed'); +}); diff --git a/contentScript.js b/contentScript.js index fd66c20..378e46a 100644 --- a/contentScript.js +++ b/contentScript.js @@ -304,40 +304,44 @@ function placeSmartOrder(action, settings) { } // Make API call to OpenAlgo +// Make API call to OpenAlgo via background script (bypasses CORS) function makeApiCall(url, data, actionText) { // Show loading indicator const loadingNotification = showNotification(`Processing ${actionText}...`, 'info', true); - - fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(data) - }) - .then(response => response.json()) - .then(data => { + + // Send message to background script to make the API call + chrome.runtime.sendMessage({ + action: 'placeOrder', + url: url, + orderData: data + }, (response) => { // Remove loading notification if (loadingNotification) { loadingNotification.remove(); } - if (data.status === 'success') { - showNotification(`${actionText} successful!`, 'success'); - } else { - showNotification(`Error: ${data.message || 'Unknown error'}`, 'error'); - } - }) - .catch(error => { - // Remove loading notification - if (loadingNotification) { - loadingNotification.remove(); + // Check if there was an error in the message passing + if (chrome.runtime.lastError) { + showNotification(`Connection Error: ${chrome.runtime.lastError.message}`, 'error'); + return; } - showNotification(`API Error: ${error.message}`, 'error'); + // Handle the response from background script + if (response && response.success) { + const responseData = response.data; + if (responseData.status === 'success') { + showNotification(`${actionText} successful!`, 'success'); + } else { + showNotification(`Error: ${responseData.message || 'Unknown error'}`, 'error'); + } + } else { + const errorMsg = response && response.error ? response.error : 'Unknown error occurred'; + showNotification(`API Error: ${errorMsg}`, 'error'); + } }); } + // Show notification on the page function showNotification(message, type, isPersistent = false) { const notification = document.createElement('div'); diff --git a/manifest.json b/manifest.json index 5cdc689..c39eeef 100644 --- a/manifest.json +++ b/manifest.json @@ -12,10 +12,17 @@ "128": "images/icon128.png" } }, + "background": { + "service_worker": "background.js" + }, "permissions": [ "storage", "activeTab" ], + "host_permissions": [ + "http://*/*", + "https://*/*" + ], "content_security_policy": { "extension_pages": "script-src 'self'; object-src 'self'" },