Skip to content

Commit 767dc4a

Browse files
rainerstudiosclaude
andcommitted
Refactor: Use backend API for inventory sync (matches web app)
PROBLEM: - Content script trying to fetch from Steam directly - Getting HTML instead of JSON (privacy/auth issues) - Complex error-prone approach SOLUTION: - Extension calls backend API (same as web app) - Backend fetches from Steam (server-side Steam API) - Backend enriches with prices and float values - Backend stores in database - Returns stats to extension FLOW: 1. User clicks "Sync Full Inventory" button 2. Popup → Background → POST /api/steam/inventory/sync 3. Backend (Investment API): - Fetches Steam inventory - Converts to JSON - Enriches with market prices - Stores in portfolio table 4. Returns: {success, added, updated, skipped, total} BENEFITS: - No Steam cookie/auth issues - Automatic price enrichment - Automatic float value lookup - Consistent with web app - More reliable Based on API docs: D:\AppADay\cs2float-checker\csfloat-api-docs\pages\steam-integration.mdx 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 30d469a commit 767dc4a

1 file changed

Lines changed: 22 additions & 62 deletions

File tree

popup.js

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -510,90 +510,50 @@ async function syncTransactionsFromPopup() {
510510
*/
511511
/**
512512
* Sync inventory from popup
513-
* Opens Steam inventory page and triggers content script sync
514-
* MUST use content script to bypass Steam's 10-day restriction
513+
* Calls backend API (same as web app "Import from Steam")
514+
* Backend fetches from Steam, enriches with prices, stores in DB
515515
*/
516516
async function syncInventoryFromPopup() {
517517
const button = document.getElementById('syncInventoryBtn');
518518
const originalText = button.innerHTML;
519519

520520
try {
521-
button.innerHTML = '⏳ Opening Inventory...';
521+
button.innerHTML = '📦 Syncing...';
522522
button.disabled = true;
523523

524-
console.log('[Popup] Opening Steam inventory page...');
524+
console.log('[Popup] Calling backend inventory sync API...');
525525

526-
// Step 1: Open or focus Steam inventory tab
527-
let tabs = await chrome.tabs.query({ url: 'https://steamcommunity.com/*/inventory*' });
528-
529-
let tab;
530-
if (tabs.length > 0) {
531-
tab = tabs[0];
532-
await chrome.tabs.update(tab.id, { active: true });
533-
console.log('[Popup] Found existing inventory tab:', tab.id);
534-
} else {
535-
tab = await chrome.tabs.create({
536-
url: 'https://steamcommunity.com/my/inventory',
537-
active: true
538-
});
539-
console.log('[Popup] Created new inventory tab:', tab.id);
540-
541-
// Wait for page to load (longer wait for new tab)
542-
await new Promise(resolve => setTimeout(resolve, 4000));
543-
}
544-
545-
button.innerHTML = '📦 Syncing Inventory...';
546-
547-
// Step 2: Try to send message with retry logic
548-
const maxRetries = 5;
549-
let lastError = null;
550-
551-
for (let attempt = 1; attempt <= maxRetries; attempt++) {
552-
try {
553-
console.log(`[Popup] Attempt ${attempt}/${maxRetries} to contact content script...`);
554-
555-
const response = await chrome.tabs.sendMessage(tab.id, {
556-
type: 'CS2_SYNC_INVENTORY'
557-
});
558-
559-
if (response && response.success) {
560-
console.log('[Popup] ✅ Sync successful:', response);
526+
// Call backend API via background script
527+
const response = await chrome.runtime.sendMessage({
528+
type: 'SYNC_INVENTORY_API'
529+
});
561530

562-
button.innerHTML = `✅ Synced ${response.itemCount || 0} Items!`;
563-
button.style.background = 'linear-gradient(135deg, #22c55e, #16a34a)';
531+
if (response && response.success) {
532+
console.log('[Popup] ✅ Sync successful:', response);
564533

565-
setTimeout(() => {
566-
button.innerHTML = originalText;
567-
button.style.background = '';
568-
button.disabled = false;
569-
}, 3000);
534+
const stats = response.stats || {};
535+
const total = stats.added || 0 + stats.updated || 0;
570536

571-
return; // Success!
572-
} else {
573-
throw new Error(response?.error || 'Failed to sync inventory');
574-
}
537+
button.innerHTML = `✅ Synced ${total} Items!`;
538+
button.style.background = 'linear-gradient(135deg, #22c55e, #16a34a)';
575539

576-
} catch (error) {
577-
lastError = error;
578-
console.warn(`[Popup] Attempt ${attempt} failed:`, error.message);
540+
setTimeout(() => {
541+
button.innerHTML = originalText;
542+
button.style.background = '';
543+
button.disabled = false;
544+
}, 3000);
579545

580-
if (attempt < maxRetries) {
581-
// Wait before retry (exponential backoff)
582-
await new Promise(resolve => setTimeout(resolve, 1000 * attempt));
583-
}
584-
}
546+
} else {
547+
throw new Error(response?.error || 'Failed to sync inventory');
585548
}
586549

587-
// All retries failed
588-
throw lastError || new Error('Failed to contact content script');
589-
590550
} catch (error) {
591551
console.error('[Popup] ❌ Inventory sync failed:', error);
592552

593553
button.innerHTML = '❌ Sync Failed';
594554
button.style.background = 'linear-gradient(135deg, #ef4444, #dc2626)';
595555

596-
alert(`Failed to sync inventory:\n${error.message}\n\nTroubleshooting:\n1. Make sure you're logged into Steam\n2. Reload the extension (chrome://extensions)\n3. Try again from your inventory page`);
556+
alert(`Failed to sync inventory:\n${error.message}\n\nMake sure:\n1. You're logged into SteamLedger\n2. Your Steam account is linked\n3. Your Steam inventory is set to Public`);
597557

598558
setTimeout(() => {
599559
button.innerHTML = originalText;

0 commit comments

Comments
 (0)