-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeed.html
More file actions
525 lines (504 loc) · 22.2 KB
/
feed.html
File metadata and controls
525 lines (504 loc) · 22.2 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#1a1208" media="(prefers-color-scheme: light)" />
<meta name="theme-color" content="#0f0a04" media="(prefers-color-scheme: dark)" />
<title>NYT Feed</title>
<link rel="manifest" href="data:application/manifest+json;base64,eyJuYW1lIjoiTmV3cyIsInNob3J0X25hbWUiOiJOZXdzIiwic3RhcnRfdXJsIjoiZmVlZC5odG1sIiwiZGlzcGxheSI6InN0YW5kYWxvbmUiLCJiYWNrZ3JvdW5kX2NvbG9yIjoiI2ZhZjZmMCIsInRoZW1lX2NvbG9yIjoiIzFhMTIwOCIsImljb25zIjpbeyJzcmMiOiJkYXRhOmltYWdlL3N2Zyt4bWw7YmFzZTY0LFBITjJaeUI0Yld4dWN6MGlhSFIwY0RvdkwzZDNkeTUzTXk1dmNtY3ZNakF3TUM5emRtY2lJSFpwWlhkQ2IzZzlJakFnTUNBMU1USWdOVEV5SWo0S0lDQThjbVZqZENCM2FXUjBhRDBpTlRFeUlpQm9aV2xuYUhROUlqVXhNaUlnY25nOUlqazJJaUJtYVd4c1BTSWpNV0V4TWpBNElpOCtDaUFnUEhSbGVIUWdlRDBpTWpVMklpQjVQU0l5T1RnaUlIUmxlSFF0WVc1amFHOXlQU0p0YVdSa2JHVWlDaUFnSUNCbWIyNTBMV1poYldsc2VUMGlSMlZ2Y21kcFlTd2dKMVJwYldWeklFNWxkeUJTYjIxaGJpY3NJSE5sY21sbUlnb2dJQ0FnWm05dWRDMXphWHBsUFNJeE16SWlDaUFnSUNCbWIyNTBMWGRsYVdkb2REMGlZbTlzWkNJS0lDQWdJR1pwYkd3OUlpTm1ZV1kyWmpBaUNpQWdJQ0JzWlhSMFpYSXRjM0JoWTJsdVp6MGlMVFFpUGs1WlZEd3ZkR1Y0ZEQ0S1BDOXpkbWMrQ2c9PSIsInNpemVzIjoiNTEyeDUxMiIsInR5cGUiOiJpbWFnZS9zdmcreG1sIiwicHVycG9zZSI6ImFueSBtYXNrYWJsZSJ9XX0=">
<link rel="apple-touch-icon" href="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MTIgNTEyIj4KICA8cmVjdCB3aWR0aD0iNTEyIiBoZWlnaHQ9IjUxMiIgcng9Ijk2IiBmaWxsPSIjMWExMjA4Ii8+CiAgPHRleHQgeD0iMjU2IiB5PSIyOTgiIHRleHQtYW5jaG9yPSJtaWRkbGUiCiAgICBmb250LWZhbWlseT0iR2VvcmdpYSwgJ1RpbWVzIE5ldyBSb21hbicsIHNlcmlmIgogICAgZm9udC1zaXplPSIxMzIiCiAgICBmb250LXdlaWdodD0iYm9sZCIKICAgIGZpbGw9IiNmYWY2ZjAiCiAgICBsZXR0ZXItc3BhY2luZz0iLTQiPk5ZVDwvdGV4dD4KPC9zdmc+Cg==">
<style>
:root {
--bg: #faf6f0;
--bg-alt: #f2ebe0;
--bg-raise: #ffffff;
--border: #e0d8cc;
--border-art: #e8e0d5;
--text: #1a1208;
--text-muted: #7a6a5a;
--text-faint: #b0a898;
--text-dim: #4a3f33;
--accent: #8b6f4e;
--accent-border: #c4a882;
--topbar-bg: #1a1208;
--topbar-text: #faf6f0;
--topbar-sub: #8b6f4e;
--topbar-icon: #b0a898;
--menu-bg: #2a2018;
--menu-border: #3a3028;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #16110a;
--bg-alt: #201810;
--bg-raise: #201810;
--border: #3a2e22;
--border-art: #2a2018;
--text: #e8ddd0;
--text-muted: #8a7a6a;
--text-faint: #4a4038;
--text-dim: #9a8a7a;
--accent: #c4a882;
--accent-border: #7a5a38;
--topbar-bg: #0f0a04;
--topbar-text: #e8ddd0;
--topbar-sub: #6a5040;
--topbar-icon: #5a4a38;
--menu-bg: #201810;
--menu-border: #3a2e22;
}
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: var(--bg);
font-family: Georgia, 'Times New Roman', serif;
color: var(--text);
min-height: 100vh;
}
#setup {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 24px;
}
.setup-box {
max-width: 420px;
width: 100%;
background: var(--bg-raise);
border: 1px solid var(--border);
border-radius: 4px;
padding: 32px;
box-shadow: 0 2px 12px rgba(0,0,0,0.06);
}
.setup-eyebrow {
font-size: 11px;
letter-spacing: 0.15em;
text-transform: uppercase;
color: var(--accent);
margin-bottom: 8px;
}
.setup-box h1 { font-size: 26px; margin-bottom: 8px; line-height: 1.2; }
.setup-box p { font-size: 14px; color: var(--text-muted); line-height: 1.6; margin-bottom: 24px; }
.setup-box a { color: var(--accent); }
#key-input {
width: 100%;
padding: 10px 12px;
border: 1px solid var(--border);
border-radius: 3px;
font-family: monospace;
font-size: 13px;
background: var(--bg);
color: var(--text);
margin-bottom: 12px;
outline: none;
}
#key-submit {
width: 100%;
padding: 10px;
background: var(--text);
color: var(--bg);
border: none;
border-radius: 3px;
font-family: Georgia, serif;
font-size: 14px;
cursor: pointer;
}
.setup-note { font-size: 12px; color: var(--text-faint); margin-top: 14px; line-height: 1.5; }
#app { display: none; }
#header {
position: sticky;
top: 0;
z-index: 10;
}
#topbar {
background: var(--topbar-bg);
padding: 10px 16px;
display: flex;
align-items: center;
justify-content: space-between;
}
.topbar-title { color: var(--topbar-text); font-weight: bold; font-size: 16px; }
.topbar-sub { color: var(--topbar-sub); font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; margin-left: 8px; }
#topbar-right { display: flex; align-items: center; gap: 2px; }
#refresh-btn, #settings-btn {
background: none;
border: none;
color: var(--topbar-icon);
padding: 4px 6px;
cursor: pointer;
line-height: 0;
border-radius: 2px;
display: flex;
align-items: center;
}
#refresh-btn:hover, #settings-btn:hover { color: var(--topbar-text); }
#settings-wrap { position: relative; }
#settings-menu {
display: none;
position: absolute;
right: 0;
top: calc(100% + 6px);
background: var(--menu-bg);
border: 1px solid var(--menu-border);
border-radius: 3px;
min-width: 130px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
z-index: 100;
}
#settings-menu.open { display: block; }
#reset-btn {
display: block;
width: 100%;
background: none;
border: none;
color: var(--text-faint);
font-size: 12px;
padding: 9px 14px;
text-align: left;
cursor: pointer;
font-family: Georgia, serif;
white-space: nowrap;
}
#reset-btn:hover { background: var(--menu-border); color: var(--topbar-text); }
#tabs {
overflow-x: auto;
background: var(--bg-alt);
border-bottom: 1px solid var(--border);
display: flex;
scrollbar-width: none;
-webkit-overflow-scrolling: touch;
}
#tabs::-webkit-scrollbar { display: none; }
.tab-btn {
padding: 9px 14px;
white-space: nowrap;
background: none;
border: none;
border-bottom: 2px solid transparent;
color: var(--text-muted);
font-family: Georgia, serif;
font-size: 13px;
cursor: pointer;
flex-shrink: 0;
}
.tab-btn.active { border-bottom-color: var(--text); color: var(--text); font-weight: bold; }
#feed { max-width: 680px; margin: 0 auto; padding: 0 16px; }
#feed-meta { padding: 10px 0 4px; font-size: 11px; color: var(--text-faint); letter-spacing: 0.08em; text-transform: uppercase; }
.article {
border-bottom: 1px solid var(--border-art);
padding: 14px 0;
cursor: pointer;
display: flex;
gap: 10px;
align-items: flex-start;
transition: opacity 0.2s;
}
.article.read { opacity: 0.45; }
.article-num { font-size: 13px; color: var(--text-faint); min-width: 18px; padding-top: 2px; font-style: italic; flex-shrink: 0; }
.article-body { flex: 1; min-width: 0; }
.article-content { min-width: 0; }
.article-tag {
display: inline-block;
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.08em;
padding: 2px 6px;
border-radius: 2px;
color: var(--accent);
background: var(--bg-alt);
}
.tag-politics { background: #f5e8e8; color: #8b3a3a; }
.tag-us { background: #e8edf5; color: #3a527a; }
.tag-world { background: #e8f0e8; color: #3a6645; }
.tag-business { background: #f5f0e0; color: #7a5e1a; }
.tag-technology { background: #e5f0f0; color: #2a6666; }
.tag-science { background: #ede8f5; color: #5a3a85; }
.tag-health { background: #f5e8f0; color: #7a3a62; }
.tag-arts { background: #f5ece8; color: #7a4030; }
.tag-sports { background: #f0ede0; color: #6b5520; }
.tag-opinion { background: #e8e8e8; color: #3a3a3a; }
.tag-media { background: #e8f0f5; color: #2a5570; }
.tag-nyregion { background: #eae8f5; color: #42387a; }
.tag-realestate { background: #edf5e8; color: #3d6630; }
.tag-travel { background: #e8f5f0; color: #2a6655; }
.tag-food { background: #f5ede8; color: #7a4820; }
.tag-middleeast { background: #f5f0e8; color: #7a5530; }
@media (prefers-color-scheme: dark) {
.article-tag { background: #2a2018; color: #9a8070; }
.tag-politics { background: #2a1818; color: #c47878; }
.tag-us { background: #181e2a; color: #7a9acc; }
.tag-world { background: #181f18; color: #6a9e72; }
.tag-business { background: #282010; color: #b89050; }
.tag-technology { background: #182020; color: #50a0a0; }
.tag-science { background: #201828; color: #9070c0; }
.tag-health { background: #281820; color: #b06090; }
.tag-arts { background: #281e18; color: #b07060; }
.tag-sports { background: #242018; color: #a08040; }
.tag-opinion { background: #222222; color: #909090; }
.tag-media { background: #182028; color: #5090b0; }
.tag-nyregion { background: #201828; color: #8070c0; }
.tag-realestate { background: #1a2018; color: #70a060; }
.tag-travel { background: #182820; color: #50a090; }
.tag-food { background: #282018; color: #b07840; }
.tag-middleeast { background: #282018; color: #b08858; }
}
.article-title { font-size: 16px; line-height: 1.35; font-weight: bold; margin-bottom: 4px; }
.article-abstract { font-size: 14px; line-height: 1.6; color: var(--text-dim); margin: 6px 0; display: none; }
.article.open .article-abstract { display: block; }
.article-meta { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 5px; }
.article-new {
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.1em;
color: var(--accent);
border: 1px solid var(--accent-border);
border-radius: 2px;
padding: 1px 4px;
font-style: normal;
font-family: Georgia, serif;
}
.article-byline { font-size: 12px; color: var(--text-muted); font-style: italic; }
.article-time { font-size: 12px; color: var(--text-faint); }
.article-link { font-size: 12px; color: var(--accent); text-decoration: none; border-bottom: 1px solid var(--accent-border); }
#status { padding: 40px 0; text-align: center; color: var(--text-faint); font-style: italic; font-size: 14px; }
#status.error { color: #c0392b; text-align: left; padding: 20px 0; }
.spacer { height: 32px; }
</style>
</head>
<body>
<div id="setup">
<div class="setup-box">
<div class="setup-eyebrow">NYT Reader</div>
<h1>Text-first feed.<br>No data waste.</h1>
<p>
Enter your free NYT Developer API key. Get one in 2 minutes at
<a href="https://developer.nytimes.com/get-started" target="_blank">developer.nytimes.com</a>.
This is separate from your subscription and costs nothing.
</p>
<input id="key-input" type="text" placeholder="Paste API key here..." />
<button id="key-submit">Open the feed →</button>
<p class="setup-note">Your key is saved only in this browser. No tracking, no accounts.</p>
</div>
</div>
<div id="app">
<div id="header">
<div id="topbar">
<div>
<span class="topbar-title">NYT</span>
<span class="topbar-sub">feed</span>
</div>
<div id="topbar-right">
<button id="refresh-btn" title="Refresh">
<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 2v6h-6"/><path d="M21 13a9 9 0 1 1-3-7.7L21 8"/></svg>
</button>
<div id="settings-wrap">
<button id="settings-btn" title="Settings">
<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"/><circle cx="12" cy="12" r="3"/></svg>
</button>
<div id="settings-menu">
<button id="reset-btn">Reset API key</button>
</div>
</div>
</div>
</div>
<div id="tabs">
<button class="tab-btn active" data-section="home">Top</button>
<button class="tab-btn" data-section="trending">Trending</button>
<button class="tab-btn" data-section="us">U.S.</button>
<button class="tab-btn" data-section="world">World</button>
<button class="tab-btn" data-section="politics">Politics</button>
<button class="tab-btn" data-section="business">Business</button>
<button class="tab-btn" data-section="technology">Tech</button>
<button class="tab-btn" data-section="science">Science</button>
<button class="tab-btn" data-section="health">Health</button>
<button class="tab-btn" data-section="arts">Arts</button>
<button class="tab-btn" data-section="sports">Sports</button>
<button class="tab-btn" data-section="opinion">Opinion</button>
</div>
</div>
<div id="feed">
<div id="feed-meta"></div>
<div id="articles"></div>
<div class="spacer"></div>
</div>
</div>
<script>
let currentSection = "home";
const ICON_EXTERNAL = '<svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" style="vertical-align:middle;position:relative;top:-1px"><path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></svg>';
let apiKey = "";
// Persisted state
let readSet = new Set(JSON.parse(localStorage.getItem("nyt_read") || "[]"));
let lastVisitMap = JSON.parse(localStorage.getItem("nyt_last_visit") || "{}");
function saveRead() {
localStorage.setItem("nyt_read", JSON.stringify([...readSet]));
}
function timeAgo(dateStr) {
const diff = Date.now() - new Date(dateStr).getTime();
const m = Math.floor(diff / 60000);
if (m < 1) return "just now";
if (m < 60) return m + "m ago";
const h = Math.floor(m / 60);
if (h < 24) return h + "h ago";
return Math.floor(h / 24) + "d ago";
}
function showApp() {
document.getElementById("setup").style.display = "none";
document.getElementById("app").style.display = "block";
buildTabs();
loadSection(currentSection);
}
function buildTabs() {
document.querySelectorAll(".tab-btn").forEach(btn => {
btn.onclick = () => {
currentSection = btn.dataset.section;
document.querySelectorAll(".tab-btn").forEach(b => b.classList.remove("active"));
btn.classList.add("active");
loadSection(currentSection);
};
});
}
async function loadSection(sec) {
const articlesEl = document.getElementById("articles");
const metaEl = document.getElementById("feed-meta");
articlesEl.innerHTML = '<div id="status">Loading...</div>';
metaEl.textContent = "";
// Track visit: grab previous visit time, then update to now
const prevVisit = lastVisitMap[sec];
lastVisitMap[sec] = Date.now();
localStorage.setItem("nyt_last_visit", JSON.stringify(lastVisitMap));
try {
// Trending uses the Most Popular API (7-day most viewed); all others use Top Stories API
const url = sec === "trending"
? "https://api.nytimes.com/svc/mostpopular/v2/viewed/7.json?api-key=" + apiKey
: "https://api.nytimes.com/svc/topstories/v2/" + sec + ".json?api-key=" + apiKey;
const res = await fetch(url);
if (!res.ok) {
if (res.status === 401 || res.status === 403) throw new Error("Invalid API key — check and try again.");
throw new Error("Error " + res.status);
}
const data = await res.json();
const rawArticles = data.results || [];
// Most Popular API uses different field names — normalize to Top Stories shape
const articles = sec === "trending"
? rawArticles.map(a => ({ ...a, published_date: a.published_date || a.updated, abstract: a.abstract, section: a.section, subsection: a.subsection || "" }))
: rawArticles;
const freshest = articles.reduce((max, a) => {
const d = a.published_date || a.updated_date;
return d && new Date(d) > new Date(max) ? d : max;
}, "");
const updatedStr = freshest ? " · updated " + timeAgo(freshest) : "";
metaEl.textContent = articles.length + " stories" + updatedStr + " · tap headline to expand";
articlesEl.innerHTML = "";
articles.forEach((a, i) => {
const rawByline = (a.byline || "").replace(/^By\s+/i, "");
const bylineAuthors = rawByline ? rawByline.split(/,\s*|\s+and\s+/i).map(s => s.trim()).filter(Boolean) : [];
const byline = bylineAuthors.length > 2
? bylineAuthors.slice(0, 2).join(", ") + " and " + (bylineAuthors.length - 2) + " other" + (bylineAuthors.length - 2 > 1 ? "s" : "")
: rawByline;
const sectionName = a.section || "";
const subsectionName = a.subsection || "";
const sectionKey = sectionName.toLowerCase().replace(/\s+/g, '');
const abbrev = (s) => s.replace(/^technology$/i, "Tech");
let tagHtml = "";
if (sec === "home" || sec === "trending") {
if (sectionName) tagHtml = '<span class="article-tag tag-' + sectionKey + '">' + abbrev(sectionName) + '</span>';
} else {
const rawLabel = subsectionName || sectionName;
const label = rawLabel.toLowerCase() !== sec.toLowerCase() ? rawLabel : "";
if (label) tagHtml = '<span class="article-tag tag-' + sectionKey + '">' + abbrev(label) + '</span>';
}
const isRead = readSet.has(a.url);
const isNew = prevVisit != null && new Date(a.published_date || a.updated_date).getTime() > prevVisit;
const div = document.createElement("div");
div.className = "article" + (isRead ? " read" : "");
div.innerHTML =
'<div class="article-num">' + (i + 1) + '.</div>' +
'<div class="article-body">' +
'<div class="article-content">' +
'<div class="article-title">' + a.title + '</div>' +
'<div class="article-abstract">' + (a.abstract || "") + '</div>' +
'<div class="article-meta">' +
(isNew ? '<span class="article-new">new</span>' : '') +
(byline ? '<span class="article-byline">' + byline + '</span>' : '') +
(tagHtml ? tagHtml : '') +
'<span class="article-time">' + timeAgo(a.published_date || a.updated_date) + '</span>' +
'<a class="article-link" href="' + a.url + '" target="_blank">Read full ' + ICON_EXTERNAL + '</a>' +
'</div>' +
'</div>' +
'</div>';
const link = div.querySelector(".article-link");
link.addEventListener("click", () => {
readSet.add(a.url);
saveRead();
div.classList.add("read");
});
div.onclick = function(e) {
if (e.target.classList.contains("article-link")) return;
div.classList.toggle("open");
};
articlesEl.appendChild(div);
});
} catch (e) {
articlesEl.innerHTML = '<div id="status" class="error">' + e.message + '</div>';
}
}
// Swipe between tabs
let touchStartX = 0, touchStartY = 0;
document.getElementById("feed").addEventListener("touchstart", e => {
touchStartX = e.touches[0].clientX;
touchStartY = e.touches[0].clientY;
}, { passive: true });
document.getElementById("feed").addEventListener("touchend", e => {
const dx = e.changedTouches[0].clientX - touchStartX;
const dy = e.changedTouches[0].clientY - touchStartY;
if (Math.abs(dx) > 60 && Math.abs(dx) > Math.abs(dy) * 1.5) {
const tabs = Array.from(document.querySelectorAll(".tab-btn"));
const idx = tabs.findIndex(b => b.dataset.section === currentSection);
const next = dx < 0 ? Math.min(idx + 1, tabs.length - 1) : Math.max(idx - 1, 0);
if (next !== idx) {
tabs[next].click();
tabs[next].scrollIntoView({ inline: "center", behavior: "smooth" });
}
}
}, { passive: true });
// Init — restore saved key
const saved = localStorage.getItem("nyt_api_key");
if (saved) {
apiKey = saved;
showApp();
}
document.getElementById("key-submit").onclick = function() {
const val = document.getElementById("key-input").value.trim();
if (!val) return;
apiKey = val;
localStorage.setItem("nyt_api_key", val);
showApp();
};
document.getElementById("key-input").addEventListener("keydown", function(e) {
if (e.key === "Enter") document.getElementById("key-submit").click();
});
document.getElementById("refresh-btn").onclick = function() {
loadSection(currentSection);
};
const settingsBtn = document.getElementById("settings-btn");
const settingsMenu = document.getElementById("settings-menu");
settingsBtn.onclick = function(e) {
e.stopPropagation();
settingsMenu.classList.toggle("open");
};
document.addEventListener("click", function() {
settingsMenu.classList.remove("open");
});
document.getElementById("reset-btn").onclick = function() {
localStorage.removeItem("nyt_api_key");
apiKey = "";
settingsMenu.classList.remove("open");
document.getElementById("app").style.display = "none";
document.getElementById("setup").style.display = "flex";
document.getElementById("key-input").value = "";
};
</script>
</body>
</html>