fix: cource load

This commit is contained in:
dhanush.s 2025-09-24 14:48:46 +05:30
parent eda7a7c463
commit bc0308421b

60
main.js
View File

@ -24,7 +24,6 @@ function goToLogin() {
if (!this.track || this.slides.length === 0) { if (!this.track || this.slides.length === 0) {
console.warn("Slider DOM elements not found. Retrying..."); console.warn("Slider DOM elements not found. Retrying...");
// Retry after a short delay
setTimeout(() => this.initSlider(), 200); setTimeout(() => this.initSlider(), 200);
return; return;
} }
@ -126,14 +125,12 @@ function goToLogin() {
this.prevBtn.addEventListener('click', () => this.prevSlide()); this.prevBtn.addEventListener('click', () => this.prevSlide());
} }
// Pause on hover
const container = document.querySelector('.slide1r-container'); const container = document.querySelector('.slide1r-container');
if (container) { if (container) {
container.addEventListener('mouseenter', () => this.stopAutoPlay()); container.addEventListener('mouseenter', () => this.stopAutoPlay());
container.addEventListener('mouseleave', () => this.startAutoPlay()); container.addEventListener('mouseleave', () => this.startAutoPlay());
} }
// Touch support
let startX = 0; let startX = 0;
let endX = 0; let endX = 0;
@ -165,9 +162,7 @@ function goToLogin() {
} }
bindKeyboardEvents() { bindKeyboardEvents() {
// Use a more specific event listener to avoid conflicts
const keyHandler = (e) => { const keyHandler = (e) => {
// Only respond if the slider container is visible
const container = document.querySelector('.slide1r-container'); const container = document.querySelector('.slide1r-container');
if (container && this.isElementInViewport(container)) { if (container && this.isElementInViewport(container)) {
if (e.key === 'ArrowLeft') { if (e.key === 'ArrowLeft') {
@ -192,11 +187,10 @@ function goToLogin() {
} }
} }
// Initialize the slider immediately
new Slide1rSlider(); new Slide1rSlider();
})(); })();
const allCourses = [ let allCourses = [
{ {
category: "Software Development & Engineering", category: "Software Development & Engineering",
link: "https://kodepilot.in/course/view.php?id=16", link: "https://kodepilot.in/course/view.php?id=16",
@ -222,32 +216,38 @@ const allCourses = [
(async () => { (async () => {
const baseUrl = 'https://kodepilot.in/course/index.php'; const baseUrl = 'https://kodepilot.in/course/index.php';
let categories = [];
let serverReachable = false;
// Step 1: Fetch categories dynamically try {
const res = await fetch(baseUrl); const res = await fetch(baseUrl);
const html = await res.text(); const html = await res.text();
const categoryRegex = /<h3 class="categoryname aabtn"><a href="([^"]+)">([^<]+)<\/a><\/h3>/g; serverReachable = true;
const categories = []; // Clear allCourses only if server is reachable
allCourses = [];
const categoryRegex = /<h3 class="categoryname aabtn"><a href="([^"]+)">([^<]+)<\/a><\/h3>/g;
let match; let match;
while ((match = categoryRegex.exec(html)) !== null) { while ((match = categoryRegex.exec(html)) !== null) {
categories.push({ url: match[1], name: match[2] }); categories.push({ url: match[1], name: match[2] });
} }
console.log('Categories found:', categories); } catch (err) {
console.error("❌ Cannot reach server, showing default courses.", err);
// Step 2: Fetch courses for each category }
const allCourses = [];
for (const category of categories) { for (const category of categories) {
try {
const resCat = await fetch(category.url); const resCat = await fetch(category.url);
const catHtml = await resCat.text(); const catHtml = await resCat.text();
const cleanHtml = catHtml.replace(/\n/g, ' '); // Flatten HTML for easier regex const cleanHtml = catHtml.replace(/\n/g, ' ');
// Regex to extract course info
const courseRegex = /<div class="card dashboard-card [^"]*"[^>]*>.*?<a href="([^"]+)"[^>]*>.*?background-image: url\(([^)]+)\);".*?<span class="sr-only">(.*?)<\/span>.*?<div class="course-category">\s*([^<]+)<\/div>.*?<div class="course-summary">.*?<p>(.*?)<\/p>/g; const courseRegex = /<div class="card dashboard-card [^"]*"[^>]*>.*?<a href="([^"]+)"[^>]*>.*?background-image: url\(([^)]+)\);".*?<span class="sr-only">(.*?)<\/span>.*?<div class="course-category">\s*([^<]+)<\/div>.*?<div class="course-summary">.*?<p>(.*?)<\/p>/g;
let found = false;
while ((match = courseRegex.exec(cleanHtml)) !== null) { while ((match = courseRegex.exec(cleanHtml)) !== null) {
found = true;
allCourses.push({ allCourses.push({
category: match[4].trim(), category: match[4].trim(),
link: match[1].trim(), link: match[1].trim(),
@ -256,19 +256,32 @@ const allCourses = [
description: match[5].replace(/<br\s*\/?>/g, ' ').trim() description: match[5].replace(/<br\s*\/?>/g, ' ').trim()
}); });
} }
if (!found) {
console.warn("⚠️ No courses found in:", category.url);
} }
console.log('All courses:', allCourses); } catch (err) {
console.error("❌ Failed to fetch category:", category.url, err);
}
}
renderCourses();
})(); })();
function renderCourses() {
const container = document.querySelector('.corces');
if (!container) {
console.error("⚠️ No .corces container found in DOM");
return;
}
// Test printing all courses container.innerHTML = ''; // clear before rendering
console.log("Testing allCourses array:");
const container = document.querySelector('.corces');
allCourses.forEach(course => { allCourses.forEach(course => {
const courseDiv = document.createElement('div'); const courseDiv = document.createElement('div');
courseDiv.className = 'course-card'; // you can style this class later courseDiv.className = 'course-card';
courseDiv.innerHTML = ` courseDiv.innerHTML = `
<a href="${course.link}" target="_blank"> <a href="${course.link}" target="_blank">
@ -282,4 +295,5 @@ allCourses.forEach(course => {
`; `;
container.appendChild(courseDiv); container.appendChild(courseDiv);
}); });
}