Skip to content

Commit 42db2fc

Browse files
rainerstudiosclaude
andcommitted
Add: Complete inventory sync following SteamAuth pattern
Changes: 1. Added inventorySync.js to manifest.json content scripts 2. Updated inventorySync to use Chrome message passing (not window.postMessage) 3. Added background handler for 'syncInventory' action 4. Background forwards inventory to /api/inventory/sync endpoint Architecture (following SteamAuth): - Content script (inventorySync.js) fetches inventory from Steam API - Sends to background via chrome.runtime.sendMessage - Background forwards to backend API with credentials - Returns result to content script Benefits: - Bypasses 10-day restriction (user authenticated to own account) - No external dependencies (no SteamAuth needed) - Follows Chrome extension best practices - Clean separation: content → background → backend 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6d9f2a8 commit 42db2fc

3 files changed

Lines changed: 66 additions & 22 deletions

File tree

background.js

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

1061+
// Handle inventory sync (SteamAuth pattern)
1062+
if (request.action === 'syncInventory') {
1063+
(async () => {
1064+
try {
1065+
console.log('[Background] 📦 Inventory sync request received');
1066+
console.log('[Background] 📦 Inventory contains:', request.payload.inventory.length, 'items');
1067+
1068+
const response = await fetch(
1069+
`${API_BASE_URL}/api/inventory/sync`,
1070+
{
1071+
method: 'POST',
1072+
headers: {
1073+
'Content-Type': 'application/json',
1074+
'X-Extension-Token': EXTENSION_SECRET,
1075+
},
1076+
credentials: 'include',
1077+
body: JSON.stringify(request.payload),
1078+
}
1079+
);
1080+
1081+
if (!response.ok) {
1082+
const errorText = await response.text();
1083+
let errorData;
1084+
try {
1085+
errorData = JSON.parse(errorText);
1086+
} catch {
1087+
errorData = { message: errorText };
1088+
}
1089+
1090+
console.error('[Background] ❌ Inventory sync failed:', errorData);
1091+
sendResponse({
1092+
success: false,
1093+
error: errorData.message || `Backend returned ${response.status}`
1094+
});
1095+
return;
1096+
}
1097+
1098+
const data = await response.json();
1099+
console.log('[Background] ✅ Inventory sync successful:', data);
1100+
sendResponse(data);
1101+
} catch (error) {
1102+
console.error('[Background] ❌ Inventory sync error:', error);
1103+
sendResponse({ success: false, error: error.message });
1104+
}
1105+
})();
1106+
return true; // Keep message channel open
1107+
}
1108+
10611109
/**
10621110
* Helper function to fetch transaction import directly (without content script)
10631111
* May fail if session cookies aren't accessible

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"https://steamcommunity.com/id/*/tradehistory",
4646
"https://steamcommunity.com/profiles/*/tradehistory"
4747
],
48-
"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"],
48+
"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", "src/inventorySync.js", "content.js"],
4949
"css": ["styles/main.css", "styles/inventory.css"],
5050
"run_at": "document_idle"
5151
},

src/inventorySync.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@
177177
},
178178

179179
async sendToBackend(inventory, steamId) {
180-
this.log('📤 Sending inventory to backend...');
181-
182-
// Get backend URL from storage or use default
183-
const backendUrl = await this.getBackendUrl();
180+
this.log('📤 Sending inventory to backend via background script...');
184181

185182
const payload = {
186183
steamId: steamId,
@@ -190,24 +187,23 @@
190187
contextId: 2
191188
};
192189

193-
this.log(`📤 Sending ${inventory.length} items to ${backendUrl}`);
194-
195-
const response = await fetch(`${backendUrl}/api/inventory/sync`, {
196-
method: 'POST',
197-
headers: {
198-
'Content-Type': 'application/json'
199-
},
200-
body: JSON.stringify(payload),
201-
credentials: 'include' // Include session cookies
190+
this.log(`📤 Sending ${inventory.length} items to background script`);
191+
192+
// Send to background script using Chrome message passing (SteamAuth pattern)
193+
return new Promise((resolve, reject) => {
194+
chrome.runtime.sendMessage({
195+
action: 'syncInventory',
196+
payload: payload
197+
}, (response) => {
198+
if (chrome.runtime.lastError) {
199+
reject(new Error(chrome.runtime.lastError.message));
200+
} else if (response && response.error) {
201+
reject(new Error(response.error));
202+
} else {
203+
resolve(response);
204+
}
205+
});
202206
});
203-
204-
if (!response.ok) {
205-
const errorText = await response.text();
206-
throw new Error(`Backend returned ${response.status}: ${errorText}`);
207-
}
208-
209-
const result = await response.json();
210-
return result;
211207
},
212208

213209
async getBackendUrl() {

0 commit comments

Comments
 (0)