Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions extension/background/canvas-crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,34 @@ export async function crawlCanvasCourse(origin, courseId, fetchImpl = fetch) {
throw new Error('Canvas origin and courseId are required for course crawling.');
}

// 1. Fetch Course details & syllabus
const courseUrl = `${origin}/api/v1/courses/${courseId}?include[]=syllabus_body`;
const courseRes = await fetchImpl(courseUrl, { credentials: 'include' });
if (!courseRes.ok) {
throw new Error(`Failed to fetch Canvas course info (${courseRes.status})`);
}
const courseJson = await courseRes.json();

// 2. Fetch Assignments list (up to 50)
const assignmentsUrl = `${origin}/api/v1/courses/${courseId}/assignments?per_page=50`;
let assignments = [];
try {
const assignRes = await fetchImpl(assignmentsUrl, { credentials: 'include' });
if (assignRes.ok) {
const assignJson = await assignRes.json();
if (Array.isArray(assignJson)) {
assignments = assignJson.map((a) => ({
title: a.name || 'Untitled Assignment',
description: stripHtml(a.description || '').slice(0, 1000),
points: a.points_possible || 0,
dueAt: a.due_at || null
}));
}

const coursePromise = fetchImpl(courseUrl, { credentials: 'include' }).then(async (res) => {
if (!res.ok) {
throw new Error(`Failed to fetch Canvas course info (${res.status})`);
}
} catch (e) {
console.warn('Could not fetch assignments list:', e);
}
return res.json();
});

const assignmentsPromise = fetchImpl(assignmentsUrl, { credentials: 'include' })
.then(async (res) => {
if (!res.ok) return [];
const json = await res.json();
if (!Array.isArray(json)) return [];
return json.map((a) => ({
title: a.name || 'Untitled Assignment',
description: stripHtml(a.description || '').slice(0, 1000),
points: a.points_possible || 0,
dueAt: a.due_at || null
}));
})
.catch((e) => {
console.warn('Could not fetch assignments list:', e);
return [];
});

const [courseJson, assignments] = await Promise.all([coursePromise, assignmentsPromise]);

return {
title: courseJson.name || courseJson.course_code || 'Canvas Course',
Expand Down
Loading