Skip to content
This repository was archived by the owner on Aug 28, 2021. It is now read-only.

Commit 8a627be

Browse files
committed
ability to parse group items and download videos
1 parent 9425471 commit 8a627be

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

app/src/popup.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ const echo360BaseURIs = [
3030
* @param {*} param0
3131
* @returns {boolean}
3232
*/
33-
function canDownload({ lesson }) {
33+
function canDownload(lesson) {
3434
if (lesson.medias.length <= 0) {
3535
return false;
3636
}
3737

3838
return lesson.medias.some((media) => media.mediaType === "Video")
3939
}
4040

41-
function getVideoFileName({lesson}) {
42-
const {updatedAt} = lesson.lesson;
41+
function getVideoFileName(lesson) {
42+
const { updatedAt } = lesson.lesson;
4343
return updatedAt.slice(0, updatedAt.indexOf("T"));
4444
}
4545

@@ -55,7 +55,7 @@ async function getCourseName(courseSectionId) {
5555

5656
const courseMatch = courseDataJson.data[0].userSections.find((userSection) => userSection.sectionId === courseSectionId)
5757

58-
return courseMatch.courseName.replace(/[/\\?%*:|"<>]/g, ' ')
58+
return courseMatch ? courseMatch.courseName.replace(/[/\\?%*:|"<>]/g, ' ') : 'unknown course';
5959
}
6060

6161
/**
@@ -68,6 +68,20 @@ async function getMediaData({ url }) {
6868
return getMediaLessonsResponse.json();
6969
}
7070

71+
function getLessons(mediaLessonsJson) {
72+
const lessons = [];
73+
mediaLessonsJson.data.forEach((mediaLessonData) => {
74+
if (mediaLessonData.lessons) {
75+
mediaLessonData.lessons.forEach((lesson) => lessons.push(lesson.lesson));
76+
}
77+
else {
78+
lessons.push(mediaLessonData.lesson);
79+
}
80+
})
81+
82+
return lessons;
83+
}
84+
7185
// Job of this function is to listen init mediaLessons once per click.
7286
async function webRequestOnComplete(xhrRequest) {
7387
console.log("Media Lessons obtained!");
@@ -76,11 +90,12 @@ async function webRequestOnComplete(xhrRequest) {
7690
mediaLessons = xhrRequest;
7791

7892
const getMediaLessonsJson = await getMediaData(mediaLessons);
79-
const courseSectionId = getMediaLessonsJson.data[0].lesson.lesson.sectionId;
93+
const lessons = getLessons(getMediaLessonsJson);
94+
const courseSectionId = lessons[0].lesson.sectionId;
8095
courseName = await getCourseName(courseSectionId);
8196

8297
console.log(getMediaLessonsJson);
83-
downloadables = getMediaLessonsJson.data.filter((dataItem) => {
98+
downloadables = lessons.filter((dataItem) => {
8499
return canDownload(dataItem);
85100
});
86101

@@ -169,7 +184,7 @@ document.addEventListener('DOMContentLoaded', function () {
169184
options.forEach((option, i) => {
170185
if (option.selected) {
171186
toDownload.push({
172-
lessonID: downloadables[i].lesson.lesson.id,
187+
lessonID: downloadables[i].lesson.id,
173188
lessonName: getVideoFileName(downloadables[i]),
174189
})
175190
}

0 commit comments

Comments
 (0)