Spaces:
Running
Running
File size: 6,641 Bytes
9a0b1a5 |
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 |
// Shared JavaScript across all pages
// Form submission handler
document.addEventListener('DOMContentLoaded', function() {
const scrapeForm = document.getElementById('scrapeForm');
const resultsSection = document.getElementById('results');
const loadingDiv = document.getElementById('loading');
const errorDiv = document.getElementById('error');
const errorMessage = document.getElementById('errorMessage');
const successResults = document.getElementById('successResults');
const articleTitle = document.getElementById('articleTitle').querySelector('h2');
const pointsList = document.getElementById('pointsList');
const imagesGrid = document.getElementById('imagesGrid');
const imagesSection = document.getElementById('imagesSection');
if (scrapeForm) {
scrapeForm.addEventListener('submit', async function(e) {
e.preventDefault();
const urlInput = document.getElementById('newsUrl');
const url = urlInput.value.trim();
if (!url) {
showError('Please enter a valid URL');
return;
}
// Show loading state
showLoading();
try {
// Simulate API call to news extraction service
// In a real implementation, this would call a backend service
const result = await simulateNewsExtraction(url);
displayResults(result);
} catch (error) {
showError('Failed to extract content from the URL. Please try again with a different news article.');
}
});
}
function showLoading() {
resultsSection.classList.remove('hidden');
loadingDiv.classList.remove('hidden');
errorDiv.classList.add('hidden');
successResults.classList.add('hidden');
}
function showError(message) {
resultsSection.classList.remove('hidden');
loadingDiv.classList.add('hidden');
errorDiv.classList.remove('hidden');
successResults.classList.add('hidden');
errorMessage.textContent = message;
}
function displayResults(data) {
loadingDiv.classList.add('hidden');
errorDiv.classList.add('hidden');
successResults.classList.remove('hidden');
// Display article title
articleTitle.textContent = data.title || 'Article Title';
// Display key points
pointsList.innerHTML = '';
if (data.keyPoints && data.keyPoints.length > 0) {
data.keyPoints.forEach(point => {
const li = document.createElement('li');
li.className = 'flex items-start gap-3';
li.innerHTML = `
<i data-feather="check-circle" class="w-5 h-5 text-green-500 mt-1 flex-shrink-0"></i>
<span>${point}</span>
`;
pointsList.appendChild(li);
});
} else {
pointsList.innerHTML = '<li class="text-gray-500">No key points extracted</li>';
}
// Display images
imagesGrid.innerHTML = '';
if (data.images && data.images.length > 0) {
data.images.forEach((image, index) => {
const imgCard = document.createElement('div');
imgCard.className = 'image-card bg-gray-100 rounded-xl overflow-hidden shadow-md';
imgCard.innerHTML = `
<img src="${image.url}" alt="${image.alt || 'News image'}" class="w-full h-48 object-cover" loading="lazy">
<div class="p-4">
<p class="text-sm text-gray-600">Image ${index + 1}</p>
</div>
`;
imagesGrid.appendChild(imgCard);
});
imagesSection.classList.remove('hidden');
} else {
imagesSection.classList.add('hidden');
}
// Refresh feather icons
feather.replace();
}
// Simulate news extraction (replace with actual API call)
async function simulateNewsExtraction(url) {
// Simulate API delay
await new Promise(resolve => setTimeout(resolve, 2000));
// Mock data for demonstration
return {
title: "Breaking: Major Technological Advancement in AI Research",
keyPoints: [
"Researchers have developed a new AI model that can understand and generate human-like text with unprecedented accuracy",
"The technology has potential applications in education, healthcare, and customer service industries",
"Ethical considerations and regulations are being discussed by industry leaders",
"The breakthrough could revolutionize how we interact with computers in daily life",
"Open-source implementation is expected to be released in the coming months"
],
images: [
{
url: "http://static.photos/technology/640x360/1",
alt: "AI Research Laboratory"
},
{
url: "http://static.photos/science/640x360/2",
alt: "Neural Network Visualization"
},
{
url: "http://static.photos/office/640x360/3",
alt: "Team Collaboration"
}
]
};
}
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// Add intersection observer for animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in');
}
});
}, observerOptions);
// Observe elements for animation
document.addEventListener('DOMContentLoaded', () => {
const animatableElements = document.querySelectorAll('.image-card, .bg-white');
animatableElements.forEach(el => {
observer.observe(el);
});
});
}); |