diff --git a/main.js b/main.js index 2318c21..fcfe66c 100644 --- a/main.js +++ b/main.js @@ -24,7 +24,6 @@ function goToLogin() { if (!this.track || this.slides.length === 0) { console.warn("Slider DOM elements not found. Retrying..."); - // Retry after a short delay setTimeout(() => this.initSlider(), 200); return; } @@ -126,14 +125,12 @@ function goToLogin() { this.prevBtn.addEventListener('click', () => this.prevSlide()); } - // Pause on hover const container = document.querySelector('.slide1r-container'); if (container) { container.addEventListener('mouseenter', () => this.stopAutoPlay()); container.addEventListener('mouseleave', () => this.startAutoPlay()); } - // Touch support let startX = 0; let endX = 0; @@ -165,9 +162,7 @@ function goToLogin() { } bindKeyboardEvents() { - // Use a more specific event listener to avoid conflicts const keyHandler = (e) => { - // Only respond if the slider container is visible const container = document.querySelector('.slide1r-container'); if (container && this.isElementInViewport(container)) { if (e.key === 'ArrowLeft') { @@ -192,11 +187,10 @@ function goToLogin() { } } - // Initialize the slider immediately new Slide1rSlider(); })(); -const allCourses = [ +let allCourses = [ { category: "Software Development & Engineering", link: "https://kodepilot.in/course/view.php?id=16", @@ -222,64 +216,84 @@ const allCourses = [ (async () => { const baseUrl = 'https://kodepilot.in/course/index.php'; + let categories = []; + let serverReachable = false; - // Step 1: Fetch categories dynamically - const res = await fetch(baseUrl); - const html = await res.text(); - const categoryRegex = /

([^<]+)<\/a><\/h3>/g; + try { + const res = await fetch(baseUrl); + const html = await res.text(); + serverReachable = true; - const categories = []; - let match; - while ((match = categoryRegex.exec(html)) !== null) { - categories.push({ url: match[1], name: match[2] }); + // Clear allCourses only if server is reachable + allCourses = []; + + const categoryRegex = /

([^<]+)<\/a><\/h3>/g; + let match; + while ((match = categoryRegex.exec(html)) !== null) { + categories.push({ url: match[1], name: match[2] }); + } + + } catch (err) { + console.error("❌ Cannot reach server, showing default courses.", err); } - console.log('Categories found:', categories); - - // Step 2: Fetch courses for each category - const allCourses = []; - for (const category of categories) { - const resCat = await fetch(category.url); - const catHtml = await resCat.text(); - const cleanHtml = catHtml.replace(/\n/g, ' '); // Flatten HTML for easier regex + try { + const resCat = await fetch(category.url); + const catHtml = await resCat.text(); + const cleanHtml = catHtml.replace(/\n/g, ' '); - // Regex to extract course info - const courseRegex = /
]*>.*?]*>.*?background-image: url\(([^)]+)\);".*?(.*?)<\/span>.*?
\s*([^<]+)<\/div>.*?
.*?

(.*?)<\/p>/g; + const courseRegex = /

]*>.*?]*>.*?background-image: url\(([^)]+)\);".*?(.*?)<\/span>.*?
\s*([^<]+)<\/div>.*?
.*?

(.*?)<\/p>/g; - while ((match = courseRegex.exec(cleanHtml)) !== null) { - allCourses.push({ - category: match[4].trim(), - link: match[1].trim(), - image: match[2].trim(), - name: match[3].trim(), - description: match[5].replace(//g, ' ').trim() - }); + let found = false; + while ((match = courseRegex.exec(cleanHtml)) !== null) { + found = true; + allCourses.push({ + category: match[4].trim(), + link: match[1].trim(), + image: match[2].trim(), + name: match[3].trim(), + description: match[5].replace(//g, ' ').trim() + }); + } + + if (!found) { + console.warn("⚠️ No courses found in:", category.url); + } + + } catch (err) { + console.error("❌ Failed to fetch category:", category.url, err); } } - console.log('All courses:', allCourses); + renderCourses(); + })(); +function renderCourses() { + const container = document.querySelector('.corces'); + if (!container) { + console.error("⚠️ No .corces container found in DOM"); + return; + } -// Test printing all courses -console.log("Testing allCourses array:"); -const container = document.querySelector('.corces'); + container.innerHTML = ''; // clear before rendering -allCourses.forEach(course => { - const courseDiv = document.createElement('div'); - courseDiv.className = 'course-card'; // you can style this class later + allCourses.forEach(course => { + const courseDiv = document.createElement('div'); + courseDiv.className = 'course-card'; - courseDiv.innerHTML = ` - -

- -
-
${course.category}
- ${course.name} -
${course.description}
-
- `; + courseDiv.innerHTML = ` + +
+
+
+
${course.category}
+ ${course.name} +
${course.description}
+
+ `; - container.appendChild(courseDiv); -}); + container.appendChild(courseDiv); + }); +}