From 03835e80177c9d54108ebadeeae342e778b80af2 Mon Sep 17 00:00:00 2001 From: Constantinos Tsiakouris Date: Sun, 5 Jul 2020 22:45:40 +0100 Subject: [PATCH] cleanup finding source data from html page --- app/src/background.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/background.js b/app/src/background.js index a2a5b5d..bdc579f 100644 --- a/app/src/background.js +++ b/app/src/background.js @@ -1,12 +1,20 @@ async function getDownloadLink({lessonID, lessonName}, echo360Domain, downloadHD) { - const regex = /(?:\(\")(?:.*)(?:\"\))/; + const classroomAppRegex = new RegExp('(classroomApp)'); + const dataRegex = /(?:\(\")(?:.*)(?:\"\))/; const lessonHTMLPageRequest = new Request(`${echo360Domain}/lesson/${lessonID}/classroom`, { method: 'GET', credentials: 'include' }); const lessonHTMLPageResponse = await fetch(lessonHTMLPageRequest) const lessonHTMLPageText = await lessonHTMLPageResponse.text(); const dummyEl = document.createElement('html') dummyEl.innerHTML = lessonHTMLPageText; + const videoDataScript = Array.from(dummyEl.getElementsByTagName('script')).filter((script) => classroomAppRegex.test(script.innerText)); - const videoDataString = dummyEl.getElementsByTagName('script')[11].innerText.match(regex)[0] + if (videoDataScript.length === 0) + { + return null; + } + + const videoDataString = videoDataScript[0].innerText.match(dataRegex)[0]; + const cleanString = videoDataString.substring(1, videoDataString.length - 1); const videoDataObject = JSON.parse(JSON.parse(cleanString));