-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
926 lines (793 loc) · 29.5 KB
/
Copy pathtest.html
File metadata and controls
926 lines (793 loc) · 29.5 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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Newshub</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background-color: #f9fafb;
color: #111827;
line-height: 1.6;
}
.container {
max-width: 1280px;
margin: 0 auto;
padding: 0 24px;
}
/* Header Styles */
.header {
background: white;
border-bottom: 1px solid #e5e7eb;
padding: 16px 0;
}
.header-content {
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
display: flex;
align-items: center;
gap: 8px;
}
.logo-icon {
width: 32px;
height: 32px;
background: #2563eb;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 16px;
}
.logo-text {
font-size: 20px;
font-weight: 600;
color: #111827;
}
.logo-text .highlight {
color: #2563eb;
}
.header-actions {
display: flex;
gap: 12px;
}
.icon-btn {
width: 40px;
height: 40px;
border: none;
background: none;
color: #6b7280;
cursor: pointer;
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
transition: background-color 0.2s;
}
.icon-btn:hover {
background-color: #f3f4f6;
}
/* Hero Section */
.hero {
text-align: center;
padding: 48px 0;
}
.hero h1 {
font-size: 36px;
font-weight: 700;
color: #2563eb;
margin-bottom: 16px;
}
.hero p {
font-size: 18px;
color: #6b7280;
}
/* Card Styles */
.card {
background: white;
border-radius: 12px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
margin-bottom: 32px;
}
.card-content {
padding: 24px;
}
.card-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 16px;
}
.card-header h3 {
font-size: 18px;
font-weight: 600;
color: #111827;
}
.api-notice {
background: #dcfce7;
border: 1px solid #16a34a;
border-radius: 8px;
padding: 16px;
margin-bottom: 16px;
}
.api-notice p {
color: #166534;
margin-bottom: 8px;
}
.api-notice small {
color: #15803d;
font-style: italic;
}
/* Form Controls */
.form-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 16px;
margin-bottom: 24px;
}
.form-group {
display: flex;
flex-direction: column;
gap: 8px;
}
.form-label {
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
font-weight: 500;
color: #374151;
}
.form-input,
.form-select {
padding: 10px 12px;
border: 1px solid #d1d5db;
border-radius: 6px;
font-size: 14px;
transition: border-color 0.2s, box-shadow 0.2s;
}
.form-input:focus,
.form-select:focus {
outline: none;
border-color: #2563eb;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
.search-section {
display: flex;
gap: 16px;
align-items: end;
}
.search-input {
flex: 1;
}
/* Button Styles */
.btn {
padding: 10px 20px;
border: none;
border-radius: 6px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
display: inline-flex;
align-items: center;
gap: 8px;
}
.btn-primary {
background: #2563eb;
color: white;
}
.btn-primary:hover:not(:disabled) {
background: #1d4ed8;
}
.btn-secondary {
background: #7c3aed;
color: white;
}
.btn-secondary:hover:not(:disabled) {
background: #6d28d9;
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/* Loading Styles */
.loading {
text-align: center;
padding: 64px 0;
}
.spinner {
width: 32px;
height: 32px;
border: 2px solid #e5e7eb;
border-top: 2px solid #2563eb;
border-radius: 50%;
animation: spin 1s linear infinite;
margin: 0 auto 16px;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/* Error Styles */
.error {
text-align: center;
color: #dc2626;
padding: 24px;
}
.error h3 {
font-size: 20px;
font-weight: 700;
margin-bottom: 8px;
}
/* Welcome Section */
.welcome {
text-align: center;
padding: 64px 0;
}
.welcome-icon {
width: 64px;
height: 64px;
background: #2563eb;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 24px;
color: white;
font-size: 32px;
}
.welcome h3 {
font-size: 24px;
font-weight: 700;
color: #111827;
margin-bottom: 8px;
}
.welcome p {
color: #6b7280;
}
/* News Grid */
.news-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
gap: 24px;
}
.news-card {
background: white;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
transition: box-shadow 0.2s, transform 0.2s;
position: relative;
}
.news-card:hover {
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
transform: translateY(-2px);
}
.news-image {
width: 100%;
height: 192px;
object-fit: cover;
}
.news-image-placeholder {
width: 100%;
height: 192px;
background: linear-gradient(135deg, #3b82f6, #8b5cf6);
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 48px;
opacity: 0.7;
}
.news-content {
padding: 24px;
}
.news-number {
position: absolute;
top: 16px;
right: 16px;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 4px 8px;
border-radius: 12px;
font-size: 12px;
font-weight: 600;
}
.news-title {
font-size: 18px;
font-weight: 700;
color: #111827;
margin-bottom: 12px;
cursor: pointer;
transition: color 0.2s;
line-height: 1.4;
}
.news-title:hover {
color: #2563eb;
}
.news-meta {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 12px;
font-size: 12px;
}
.news-source {
background: #dbeafe;
color: #1e40af;
padding: 4px 8px;
border-radius: 12px;
font-weight: 500;
}
.news-time,
.news-author {
background: #f3f4f6;
color: #6b7280;
padding: 4px 8px;
border-radius: 12px;
display: flex;
align-items: center;
gap: 4px;
}
.news-description {
color: #6b7280;
margin-bottom: 16px;
line-height: 1.5;
}
.news-actions {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
.btn-sm {
padding: 8px 12px;
font-size: 12px;
}
.btn-success {
background: #059669;
color: white;
}
.btn-success:hover {
background: #047857;
}
.btn-outline {
background: transparent;
border: 1px solid;
}
.btn-pink {
border-color: #f9a8d4;
color: #ec4899;
}
.btn-pink:hover {
background: #fdf2f8;
}
.btn-orange {
border-color: #fed7aa;
color: #ea580c;
}
.btn-orange:hover {
background: #fff7ed;
}
/* Responsive */
@media (max-width: 768px) {
.container {
padding: 0 16px;
}
.hero h1 {
font-size: 28px;
}
.form-grid {
grid-template-columns: 1fr;
}
.search-section {
flex-direction: column;
align-items: stretch;
}
.news-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<!-- Header -->
<header class="header">
<div class="container">
<div class="header-content">
<div class="logo">
<div class="logo-icon"></div>
<div class="logo-text">News <span class="highlight">Hub</span></div>
</div>
</div>
</div>
</header>
<!-- Main Content -->
<main class="container">
<!-- Hero Section -->
<section class="hero">
<h1>Discover Breaking News</h1>
<p>Stay informed with the latest updates from around the world</p>
</section>
<!-- API Configuration Card -->
<div class="card">
<div class="card-content">
<div class="card-header">
<span style="color: #f59e0b;"></span>
<h3>News Configuration</h3>
</div>
<!-- Form Controls (API Key field removed) -->
<div class="form-grid">
<div class="form-group">
<label class="form-label">
<span style="color: #ef4444;"></span>
Category
</label>
<select id="category" class="form-select">
<option value="">All News</option>
<option value="business">Business</option>
<option value="technology">Technology</option>
<option value="entertainment">Entertainment</option>
<option value="health">Health</option>
<option value="science">Science</option>
<option value="sports">Sports</option>
</select>
</div>
<div class="form-group">
<label class="form-label">
<span style="color: #2563eb;"></span>
Language
</label>
<select id="language" class="form-select">
<option value="en">English</option>
<option value="es">Spanish</option>
<option value="fr">French</option>
<option value="de">German</option>
<option value="it">Italian</option>
<option value="pt">Portuguese</option>
</select>
</div>
<div class="form-group">
<label class="form-label">
<span style="color: #10b981;"></span>
Country
</label>
<select id="country" class="form-select">
<option value="us">United States</option>
<option value="gb">United Kingdom</option>
<option value="ca">Canada</option>
<option value="au">Australia</option>
<option value="de">Germany</option>
<option value="fr">France</option>
</select>
</div>
<div class="form-group">
<label class="form-label">
<span style="color: #8b5cf6;"></span>
Results
</label>
<select id="pageSize" class="form-select">
<option value="20">20 articles</option>
<option value="50">50 articles</option>
<option value="100">100 articles</option>
</select>
</div>
</div>
<!-- Search Section -->
<div class="search-section">
<div class="search-input">
<input type="text" id="searchQuery" class="form-input"
placeholder="Search for specific topics...">
</div>
<button id="fetchBtn" class="btn btn-primary">
Get News
</button>
<button id="searchBtn" class="btn btn-secondary">
Search
</button>
</div>
</div>
</div>
<!-- News Container -->
<div id="newsContainer">
<div class="welcome">
<div class="welcome-icon"></div>
<h3>Welcome to Newshub</h3>
<p>Click "Get News" above to get started!</p>
</div>
</div>
</main>
<script>
// Hardcoded API key - users don't need to enter their own
const API_KEY = '8bbe24cce36041828e47d63200cfd435';
let isLoading = false;
function getTimeAgo(date) {
const now = new Date();
const diffInMinutes = Math.floor((now - date) / (1000 * 60));
if (diffInMinutes < 60) {
return `${diffInMinutes}m ago`;
} else if (diffInMinutes < 1440) {
return `${Math.floor(diffInMinutes / 60)}h ago`;
} else {
return `${Math.floor(diffInMinutes / 1440)}d ago`;
}
}
function showLoading() {
const container = document.getElementById('newsContainer');
container.innerHTML = `
<div class="loading">
<div class="spinner"></div>
<p>Fetching the latest news stories...</p>
</div>
`;
}
function showError(message) {
const container = document.getElementById('newsContainer');
container.innerHTML = `
<div class="card">
<div class="card-content">
<div class="error">
<h3> Oops! Something went wrong</h3>
<p>${message}</p>
<p>Please try again in a moment.</p>
</div>
</div>
</div>
`;
}
function displayNews(articles) {
const container = document.getElementById('newsContainer');
if (articles.length === 0) {
container.innerHTML = `
<div class="card">
<div class="card-content">
<div class="error">
<h3> No News Found</h3>
<p>No articles found for your criteria. Try different filters!</p>
</div>
</div>
</div>
`;
return;
}
const newsHTML = articles.map((article, index) => {
const timeAgo = getTimeAgo(new Date(article.publishedAt));
const title = article.title || 'Untitled';
const description = article.description || 'No description available.';
const source = article.source?.name || 'Unknown Source';
const author = article.author || '';
const url = article.url || '#';
const urlToImage = article.urlToImage;
const imageElement = urlToImage
? `<img src="${urlToImage}" alt="${title}" class="news-image" onerror="this.style.display='none'; this.nextElementSibling.style.display='flex';">
<div class="news-image-placeholder" style="display:none;"></div>`
: `<div class="news-image-placeholder"></div>`;
return `
<div class="news-card">
<div class="news-number">#${index + 1}</div>
${imageElement}
<div class="news-content">
<h3 class="news-title" onclick="window.open('${url}', '_blank')">${title}</h3>
<div class="news-meta">
<span class="news-source">${source}</span>
<span class="news-time"> ${timeAgo}</span>
${author ? `<span class="news-author"> ${author}</span>` : ''}
</div>
<p class="news-description">${description}</p>
<div class="news-actions">
<button class="btn btn-sm btn-success" onclick="window.open('${url}', '_blank')">
Read Full
</button>
<button class="btn btn-sm btn-outline btn-pink" onclick="shareArticle('${title.replace(/'/g, "\\'")}', '${url}')">
Share
</button>
<button class="btn btn-sm btn-outline btn-orange" onclick="bookmarkArticle('${title.replace(/'/g, "\\'")}', '${url}')">
Bookmark
</button>
</div>
</div>
</div>
`;
}).join('');
container.innerHTML = `<div class="news-grid">${newsHTML}</div>`;
}
async function fetchNews() {
if (isLoading) return;
isLoading = true;
const fetchBtn = document.getElementById('fetchBtn');
fetchBtn.disabled = true;
fetchBtn.textContent = 'Loading...';
const category = document.getElementById('category').value;
const country = document.getElementById('country').value;
const language = document.getElementById('language').value;
const pageSize = document.getElementById('pageSize').value;
showLoading();
try {
// Build API URL with hardcoded API key
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const fromDate = yesterday.toISOString();
let apiUrl = `https://newsapi.org/v2/top-headlines?apiKey=${API_KEY}&from=${fromDate}&sortBy=publishedAt&pageSize=${pageSize}&country=${country}&language=${language}`;
if (category) {
apiUrl += `&category=${category}`;
}
console.log('Fetching from:', apiUrl);
// Try direct fetch first
try {
const directResponse = await fetch(apiUrl);
const directData = await directResponse.json();
if (directData.status === 'ok') {
displayNews(directData.articles || []);
} else {
throw new Error(directData.message || 'Direct API call failed');
}
} catch (directError) {
console.log('Direct API failed, trying proxies:', directError);
// Try CORS proxies
const proxies = [
'https://api.allorigins.win/get?url=',
'https://corsproxy.io/?'
];
let success = false;
let lastError = null;
for (const proxy of proxies) {
try {
console.log('Trying proxy:', proxy);
const proxyUrl = proxy + encodeURIComponent(apiUrl);
const response = await fetch(proxyUrl);
let data;
if (proxy.includes('allorigins')) {
const proxyData = await response.json();
data = JSON.parse(proxyData.contents);
} else {
data = await response.json();
}
console.log('Proxy response:', data);
if (data.status === 'ok') {
displayNews(data.articles || []);
success = true;
break;
} else {
throw new Error(data.message || 'Proxy API call failed');
}
} catch (error) {
console.log(`Proxy ${proxy} failed:`, error);
lastError = error;
continue;
}
}
if (!success) {
throw new Error(`Unable to fetch news at this time. Please try again later.`);
}
}
} catch (error) {
console.error('Fetch error:', error);
showError(error.message);
} finally {
isLoading = false;
fetchBtn.disabled = false;
fetchBtn.textContent = ' Get News';
}
}
async function searchNews() {
if (isLoading) return;
const searchQuery = document.getElementById('searchQuery').value.trim();
if (!searchQuery) {
alert('Please enter a search term!');
return;
}
isLoading = true;
const searchBtn = document.getElementById('searchBtn');
searchBtn.disabled = true;
searchBtn.textContent = 'Searching...';
const language = document.getElementById('language').value;
showLoading();
try {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 7);
const fromDate = yesterday.toISOString();
const apiUrl = `https://newsapi.org/v2/everything?apiKey=${API_KEY}&q=${encodeURIComponent(searchQuery)}&from=${fromDate}&sortBy=publishedAt&pageSize=50&language=${language}`;
console.log('Searching with:', apiUrl);
// Try direct fetch first
try {
const directResponse = await fetch(apiUrl);
const directData = await directResponse.json();
if (directData.status === 'ok') {
displayNews(directData.articles || []);
} else {
throw new Error(directData.message || 'Direct search failed');
}
} catch (directError) {
console.log('Direct search failed, trying proxies:', directError);
const proxies = [
'https://api.allorigins.win/get?url=',
'https://corsproxy.io/?'
];
let success = false;
let lastError = null;
for (const proxy of proxies) {
try {
const proxyUrl = proxy + encodeURIComponent(apiUrl);
const response = await fetch(proxyUrl);
let data;
if (proxy.includes('allorigins')) {
const proxyData = await response.json();
data = JSON.parse(proxyData.contents);
} else {
data = await response.json();
}
if (data.status === 'ok') {
displayNews(data.articles || []);
success = true;
break;
} else {
throw new Error(data.message || 'Proxy search failed');
}
} catch (error) {
lastError = error;
continue;
}
}
if (!success) {
throw new Error(`Search failed. We're sorry, but it seems like you're currently offline. Please check your internet connection and try again later.Please try again later.`);
}
}
} catch (error) {
console.error('Search error:', error);
showError(error.message);
} finally {
isLoading = false;
searchBtn.disabled = false;
searchBtn.textContent = ' Search';
}
}
function shareArticle(title, url) {
if (navigator.share) {
navigator.share({
title: title,
url: url
}).catch(err => console.log('Error sharing:', err));
} else {
// Fallback to copying to clipboard
navigator.clipboard.writeText(`${title} - ${url}`).then(() => {
alert('Article link copied to clipboard!');
}).catch(() => {
alert('Unable to copy to clipboard');
});
}
}
function bookmarkArticle(title, url) {
// Store bookmarks in localStorage
const bookmarks = JSON.parse(localStorage.getItem('bookmarks') || '[]');
const bookmark = { title, url, date: new Date().toISOString() };
bookmarks.push(bookmark);
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
alert(`Bookmarked: ${title}`);
}
// Event listeners
document.getElementById('fetchBtn').addEventListener('click', fetchNews);
document.getElementById('searchBtn').addEventListener('click', searchNews);
// Allow Enter key to trigger search
document.getElementById('searchQuery').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
searchNews();
}
});
// Initialize
console.log('Newshub loaded successfully with API key ready!');
</script>
</body>
</html>