Skip to content

Commit d776962

Browse files
committed
Fix: Use content script for authenticated transaction sync
- Add steamledgerContentScript.js to handle API calls with session cookies - Content scripts on localhost:3000 can access Better Auth session - Background script sends message to content script when possible - Fallback to direct fetch if no SteamLedger tab is open - Add localhost:3000 to host_permissions and content_scripts - Fixes: 'Please login to sync transactions' auth error
1 parent 2bdc874 commit d776962

3 files changed

Lines changed: 111 additions & 14 deletions

File tree

background.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,31 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
10581058
return true; // Keep message channel open
10591059
}
10601060

1061+
/**
1062+
* Helper function to fetch transaction import directly (without content script)
1063+
* May fail if session cookies aren't accessible
1064+
*/
1065+
async function fetchTransactionImportDirect(marketData) {
1066+
const importResponse = await fetch(`${API_BASE_URL}/api/transactions/import`, {
1067+
method: 'POST',
1068+
credentials: 'include',
1069+
headers: {
1070+
'Content-Type': 'application/json'
1071+
},
1072+
body: JSON.stringify(marketData)
1073+
});
1074+
1075+
if (!importResponse.ok) {
1076+
const errorData = await importResponse.json().catch(() => ({}));
1077+
return {
1078+
success: false,
1079+
error: errorData.message || `Backend returned ${importResponse.status}`
1080+
};
1081+
}
1082+
1083+
return await importResponse.json();
1084+
}
1085+
10611086
// Handle manual transaction sync from popup
10621087
if (request.type === 'SYNC_TRANSACTIONS_MANUAL') {
10631088
(async () => {
@@ -1119,22 +1144,35 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
11191144
return;
11201145
}
11211146

1122-
// Send to SteamLedger backend
1123-
const importResponse = await fetch(`${API_BASE_URL}/api/transactions/import`, {
1124-
method: 'POST',
1125-
credentials: 'include',
1126-
headers: {
1127-
'Content-Type': 'application/json'
1128-
},
1129-
body: JSON.stringify(marketData)
1130-
});
1147+
// Send to SteamLedger backend via content script
1148+
// Content scripts on localhost:3000 can access session cookies
1149+
const tabs = await chrome.tabs.query({ url: `${API_BASE_URL}/*` });
1150+
1151+
let result;
1152+
if (tabs.length > 0) {
1153+
// Website tab is open - use content script for authenticated request
1154+
console.log('[Background] Found SteamLedger tab, using content script for auth');
1155+
1156+
try {
1157+
result = await chrome.tabs.sendMessage(tabs[0].id, {
1158+
type: 'IMPORT_TRANSACTIONS',
1159+
marketData: marketData
1160+
});
1161+
} catch (error) {
1162+
console.warn('[Background] Content script not responding, trying direct fetch');
1163+
// Fallback to direct fetch
1164+
result = await fetchTransactionImportDirect(marketData);
1165+
}
1166+
} else {
1167+
// No website tab open - try direct fetch (may fail if not authenticated)
1168+
console.log('[Background] No SteamLedger tab open, trying direct fetch');
1169+
result = await fetchTransactionImportDirect(marketData);
1170+
}
11311171

1132-
if (!importResponse.ok) {
1133-
const errorData = await importResponse.json().catch(() => ({}));
1134-
throw new Error(errorData.message || `Backend returned ${importResponse.status}`);
1172+
if (!result.success) {
1173+
throw new Error(result.error || result.message || 'Failed to import transactions');
11351174
}
11361175

1137-
const result = await importResponse.json();
11381176
console.log('[Background] Import result:', result);
11391177

11401178
// Store synced transactions count

manifest.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"https://cs2floatchecker.com/*",
2121
"https://api.cs2floatchecker.com/*",
2222
"https://steamledger.com/*",
23-
"https://api.steamledger.com/*"
23+
"https://api.steamledger.com/*",
24+
"http://localhost:3000/*"
2425
],
2526
"externally_connectable": {
2627
"matches": [
@@ -47,6 +48,14 @@
4748
"js": ["lib/marketIntelligence.js", "lib/inventoryEnhancer.js", "src/infiniteScroll.js", "src/buff163Integration.js", "src/item3DPreview.js", "src/tradeProtectionDisplay.js", "src/floatRarityDisplay.js", "src/multiMarketPricing.js", "src/watchlistButton.js", "src/transactionMonitor.js", "content.js"],
4849
"css": ["styles/main.css", "styles/inventory.css"],
4950
"run_at": "document_idle"
51+
},
52+
{
53+
"matches": [
54+
"http://localhost:3000/*",
55+
"https://steamledger.com/*"
56+
],
57+
"js": ["src/steamledgerContentScript.js"],
58+
"run_at": "document_idle"
5059
}
5160
],
5261
"action": {

src/steamledgerContentScript.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Content script for SteamLedger website (localhost:3000)
3+
* Handles transaction import with proper session cookie access
4+
*/
5+
6+
console.log('[SteamLedger Content Script] Loaded');
7+
8+
// Listen for messages from background script
9+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
10+
if (request.type === 'IMPORT_TRANSACTIONS') {
11+
console.log('[SteamLedger Content Script] Received IMPORT_TRANSACTIONS request');
12+
13+
// Make authenticated API call with session cookies
14+
(async () => {
15+
try {
16+
const response = await fetch('/api/transactions/import', {
17+
method: 'POST',
18+
credentials: 'include', // Include session cookies
19+
headers: {
20+
'Content-Type': 'application/json'
21+
},
22+
body: JSON.stringify(request.marketData)
23+
});
24+
25+
if (!response.ok) {
26+
const errorData = await response.json().catch(() => ({}));
27+
console.error('[SteamLedger Content Script] Import failed:', errorData);
28+
sendResponse({
29+
success: false,
30+
error: errorData.message || `API returned ${response.status}`
31+
});
32+
return;
33+
}
34+
35+
const result = await response.json();
36+
console.log('[SteamLedger Content Script] Import successful:', result);
37+
sendResponse(result);
38+
39+
} catch (error) {
40+
console.error('[SteamLedger Content Script] Error:', error);
41+
sendResponse({
42+
success: false,
43+
error: error.message
44+
});
45+
}
46+
})();
47+
48+
return true; // Keep message channel open for async response
49+
}
50+
});

0 commit comments

Comments
 (0)