1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
<!doctype html> <html lang="de"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>Countdown bis nächster F1 GP (Debug)</title> <style> body { font-family: system-ui, Arial, sans-serif; display:flex; align-items:center; justify-content:center; height:100vh; margin:0; background:#0b1220; color:#e6eef8; } .card { background:#0f1724; padding:24px; border-radius:12px; box-shadow:0 6px 20px rgba(2,6,23,0.6); text-align:center; width:360px; } h1 { margin:0 0 12px; font-size:18px; color:#fff; } .race { font-weight:600; margin-bottom:8px; } .time { font-size:28px; letter-spacing:0.6px; margin-bottom:10px; } .note { font-size:13px; color:#9fb0d6; } </style> </head> <body> <div class="card"> <h1>Countdown bis nächster F1 GP</h1> <div class="race" id="raceInfo">Lade Renninfo…</div> <div class="time" id="countdown">--:--:--:--</div> <div class="note" id="local">Lokale Zeit</div> </div>
<script> const API = 'https://api.openf1.org/v1';
function parseIso(s) { if (!s) return null; const d = new Date(s); return isNaN(d) ? null : d; }
// Robuster Fetch mit Logging des Response-Bodys bei Fehler async function tryFetchJson(url) { try { const res = await fetch(url); const text = await res.text(); let json = null; try { json = text ? JSON.parse(text) : null; } catch(e){ /* not json */ } if (!res.ok) { console.warn('Fetch non-ok', url, res.status, text); return null; } return json ?? text; } catch (e) { console.warn('Fetch exception', url, e); return null; } }
async function fetchAllUpcomingItems() { const tryUrls = [ `${API}/sessions`, `${API}/meetings?future=true`, `${API}/meetings` ]; for (const url of tryUrls) { const data = await tryFetchJson(url); console.log('fetched from', url, data); if (!data) continue; if (Array.isArray(data) && data.length) return data; if (Array.isArray(data?.sessions) && data.sessions.length) return data.sessions; if (Array.isArray(data?.meetings) && data.meetings.length) return data.meetings; } return null; }
function getDateCandidates(item) { const list = []; if (!item) return list; if (item.date_start) list.push(parseIso(item.date_start)); if (item.date_end) list.push(parseIso(item.date_end)); if (item.date) list.push(parseIso(item.date)); if (item.sessions && Array.isArray(item.sessions)) { for (const s of item.sessions) { if (s.date_start) list.push(parseIso(s.date_start)); if (s.date) list.push(parseIso(s.date)); } } return list.filter(d => d instanceof Date && !isNaN(d)); }
function normalizeSessionName(s) { if (!s) return null; const lower = s.toLowerCase(); if (/\brace\b/i.test(s)) return 'Race'; if (/\bqualif/i.test(lower) || /\bq\b(?![a-z])/i.test(lower)) return 'Qualifying'; if (/\bsprint\b/i.test(lower)) return 'Sprint'; if (/\b(fp|practice|free practice|p)\b/i.test(lower)) return 'Practice'; // fallback: first meaningful word capitalized const word = s.split(/\s+/).find(Boolean) || s; return word.charAt(0).toUpperCase() + word.slice(1); }
function extractSessionType(obj) { if (!obj) return null; const fields = ['session_name','sessionType','session_type','type','name','title','phase']; for (const f of fields) { if (!obj[f]) continue; const norm = normalizeSessionName(String(obj[f])); if (norm) return norm; } return null; }
function pickNext(items) { if (!items || !Array.isArray(items)) return null; const now = new Date(); const candidates = [];
for (const it of items) { if (!it) continue; if (it.session_name || it.sessionType || it.type || it.name) { const d = parseIso(it.date_start || it.date || it.date_time); candidates.push({ item: it, date: d, type: 'session' }); } else if (it.meeting_key || it.meeting_name || it.sessions) { const dates = getDateCandidates(it); for (const d of dates) candidates.push({ item: it, date: d, type: 'meeting' }); if (Array.isArray(it.sessions)) { for (const s of it.sessions) { const sd = parseIso(s.date_start || s.date); if (sd) candidates.push({ item: s, date: sd, parent: it, type: 'session' }); } } } else { const d = parseIso(it.date_start || it.date || it.date_time); if (d) candidates.push({ item: it, date: d, type: 'unknown' }); } }
const future = candidates.filter(c => c.date && c.date > now); if (future.length === 0) return null; future.sort((a,b) => a.date - b.date); return future[0]; }
function startCountdown(targetDate, onTick) { let interval = null; function update() { const now = new Date(); let diff = targetDate - now; if (diff < 0) diff = 0; const days = Math.floor(diff / (1000*60*60*24)); const hours = Math.floor((diff%(1000*60*60*24)) / (1000*60*60)); const minutes = Math.floor((diff%(1000*60*60)) / (1000*60)); const seconds = Math.floor((diff%(1000*60)) / 1000); onTick({ days, hours, minutes, seconds, remainingMs: diff }); if (diff === 0 && interval !== null) { clearInterval(interval); interval = null; } } update(); interval = setInterval(update, 1000); return () => { if (interval !== null) { clearInterval(interval); interval = null; } }; }
(async function init() { const raceInfoEl = document.getElementById('raceInfo'); const countdownEl = document.getElementById('countdown'); const localEl = document.getElementById('local');
try { const items = await fetchAllUpcomingItems(); console.log('items', items); if (!items) throw new Error('Keine Items empfangen (items null)'); const picked = pickNext(items); console.log('picked', picked); if (!picked) throw new Error('Kein zukünftiges Rennen/Session gefunden (picked null)');
console.log('picked.item', picked.item);
const obj = picked.item; const parent = picked.parent || null; const date = picked.date; if (!date) throw new Error('Kein Datum im ausgewählten Element');
// GP-Name: meeting/race_name bevorzugen const meetingName = (parent && (parent.meeting_name || parent.meeting_key)) || obj.meeting_name || obj.meeting_key || null; const gpName = meetingName || obj.race_name || obj.name || obj.meeting_key || 'Nächstes Rennen';
// Session-Typ (normalisiert, ohne Nummern) let sessionType = extractSessionType(obj); if (!sessionType && parent && parent.sessions && Array.isArray(parent.sessions)) { const now = new Date(); const sessCandidates = parent.sessions .map(s => ({ s, d: parseIso(s.date_start || s.date) })) .filter(x => x.d && x.d > now) .sort((a,b) => a.d - b.d); if (sessCandidates.length) sessionType = extractSessionType(sessCandidates[0].s); }
const circuit = (parent && (parent.circuit_name || parent.location)) || obj.circuit_name || obj.location || ''; const country = (parent && (parent.country || parent.location_country)) || obj.country || obj.location_country || '';
// Format: "Grand Prix Name — SessionType" const displayName = sessionType ? `${gpName} — ${sessionType}` : gpName;
raceInfoEl.textContent = `${displayName}${circuit ? ' — ' + circuit : ''}${country ? ' ('+country+')' : ''}`; localEl.textContent = `Start (lokal): ${date.toLocaleString(undefined, { dateStyle:'medium', timeStyle:'short' })}`;
startCountdown(date, ({days, hours, minutes, seconds, remainingMs}) => { const dstr = days > 0 ? days + 'd ' : ''; countdownEl.textContent = `${dstr}${String(hours).padStart(2,'0')}:${String(minutes).padStart(2,'0')}:${String(seconds).padStart(2,'0')}`; if (remainingMs === 0) countdownEl.textContent = 'Rennen begonnen!'; });
} catch (err) { raceInfoEl.textContent = 'Fehler beim Laden'; countdownEl.textContent = '--:--:--:--'; console.error(err); } })(); </script> </body> </html>
|