Fix overlay web adapter: Add standalone mode API fallback

- Added isStandaloneMode detection (window.parent === window)
- getFixtureData() now fetches directly from /api/overlay/fixtures in standalone mode
- getCompletedMatches() now fetches directly from /api/overlay/completed-matches in standalone mode
- getWinningOutcomes() now fetches directly from /api/overlay/winning-outcomes in standalone mode
- Prevents timeout waiting for parent window response when no parent exists
parent 975881f2
......@@ -109,6 +109,12 @@
// Web environment - set up postMessage bridge
originalConsoleLog('[WebAdapter] Setting up postMessage bridge for web environment');
// Check if we're in standalone mode (no parent window to respond)
const isStandaloneMode = window.parent === window;
if (isStandaloneMode) {
originalConsoleLog('[WebAdapter] Standalone mode detected - no parent window, will use direct API calls');
}
// Data storage
let fixtureData = null;
let timerState = { running: false, remaining_seconds: 0 };
......@@ -157,12 +163,39 @@
// Get fixture data - returns Promise (async like Qt)
getFixtureData: function() {
return new Promise((resolve) => {
return new Promise(async (resolve) => {
originalConsoleLog('[WebAdapter] getFixtureData called, current data:', fixtureData ? fixtureData.length + ' fixtures' : 'null');
if (fixtureData && fixtureData.length > 0) {
originalConsoleLog('[WebAdapter] Returning cached fixture data');
resolve(JSON.stringify(fixtureData));
} else if (isStandaloneMode) {
// Standalone mode - fetch directly from API
originalConsoleLog('[WebAdapter] Standalone mode - fetching fixture data directly from API');
try {
const apiBaseUrl = window.location.origin;
const response = await fetch(`${apiBaseUrl}/api/overlay/fixtures`, {
method: 'GET',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
originalConsoleLog('[WebAdapter] API returned fixture data:', data ? data.length : 0, 'fixtures');
fixtureData = data || [];
resolve(JSON.stringify(fixtureData));
} else {
originalConsoleLog('[WebAdapter] API request failed:', response.status, response.statusText);
resolve(JSON.stringify([]));
}
} catch (error) {
originalConsoleLog('[WebAdapter] API fetch error:', error.message);
resolve(JSON.stringify([]));
}
} else {
// Request data from parent
originalConsoleLog('[WebAdapter] Requesting fixture data from parent');
......@@ -218,12 +251,39 @@
// Get completed matches - returns Promise
getCompletedMatches: function() {
return new Promise((resolve) => {
return new Promise(async (resolve) => {
originalConsoleLog('[WebAdapter] getCompletedMatches called, current data:', completedMatches ? completedMatches.length + ' matches' : 'null');
if (completedMatches && completedMatches.length > 0) {
originalConsoleLog('[WebAdapter] Returning cached completed matches');
resolve(JSON.stringify(completedMatches));
} else if (isStandaloneMode) {
// Standalone mode - fetch directly from API
originalConsoleLog('[WebAdapter] Standalone mode - fetching completed matches directly from API');
try {
const apiBaseUrl = window.location.origin;
const response = await fetch(`${apiBaseUrl}/api/overlay/completed-matches`, {
method: 'GET',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
originalConsoleLog('[WebAdapter] API returned completed matches:', data ? data.length : 0, 'matches');
completedMatches = data || [];
resolve(JSON.stringify(completedMatches));
} else {
originalConsoleLog('[WebAdapter] API request failed:', response.status, response.statusText);
resolve(JSON.stringify([]));
}
} catch (error) {
originalConsoleLog('[WebAdapter] API fetch error:', error.message);
resolve(JSON.stringify([]));
}
} else {
// Request data from parent
originalConsoleLog('[WebAdapter] Requesting completed matches from parent');
......@@ -248,20 +308,48 @@
// Get winning outcomes - returns Promise
getWinningOutcomes: function(matchId) {
return new Promise((resolve) => {
// Request data from parent
requestWinningOutcomes(matchId);
let attempts = 0;
const checkData = setInterval(() => {
attempts++;
if (winningOutcomes && winningOutcomes.length > 0) {
clearInterval(checkData);
resolve(JSON.stringify(winningOutcomes));
} else if (attempts > 30) {
clearInterval(checkData);
return new Promise(async (resolve) => {
if (isStandaloneMode) {
// Standalone mode - fetch directly from API
originalConsoleLog('[WebAdapter] Standalone mode - fetching winning outcomes from API for match:', matchId);
try {
const apiBaseUrl = window.location.origin;
const response = await fetch(`${apiBaseUrl}/api/overlay/winning-outcomes/${matchId}`, {
method: 'GET',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
originalConsoleLog('[WebAdapter] API returned winning outcomes:', data ? data.length : 0, 'outcomes');
resolve(JSON.stringify(data || []));
} else {
originalConsoleLog('[WebAdapter] API request failed:', response.status, response.statusText);
resolve(JSON.stringify([]));
}
} catch (error) {
originalConsoleLog('[WebAdapter] API fetch error:', error.message);
resolve(JSON.stringify([]));
}
}, 100);
} else {
// Request data from parent
requestWinningOutcomes(matchId);
let attempts = 0;
const checkData = setInterval(() => {
attempts++;
if (winningOutcomes && winningOutcomes.length > 0) {
clearInterval(checkData);
resolve(JSON.stringify(winningOutcomes));
} else if (attempts > 30) {
clearInterval(checkData);
resolve(JSON.stringify([]));
}
}, 100);
}
});
}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment