Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -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');
});
46 changes: 25 additions & 21 deletions contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 7 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
},
Expand Down