feat(navigation): enhance link handling and fetch redirects

Refactored feedback and quiz link detection to push both types
to the feedbackLinks array. Updated nextContent and openQuizModal
functions to handle "/quiz/" links similarly to "/feedback/" links,
including replacing "view" with "attempt" and ensuring they are
fetched to resolve potential redirects. This async handling ensures
users are directed accurately. Also, consolidated logic for URL manipulation to maintain consistency.
This commit is contained in:
Kumi 2024-07-04 14:31:27 +02:00
parent 1f00277f60
commit b3b9ea55d1
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -21,8 +21,12 @@ function getFeedbackLinks() {
links.forEach((link) => { links.forEach((link) => {
const href = link.getAttribute("href"); const href = link.getAttribute("href");
// Check if the href includes "/feedback/" // Check if the href includes "/feedback/"
if (href && href.includes("/feedback/")) { if (href) {
if (href.includes("/feedback/")) {
feedbackLinks.push(href); feedbackLinks.push(href);
} else if (href.includes("/quiz/")) {
feedbackLinks.push(href);
}
} }
}); });
@ -91,7 +95,7 @@ loadScript(
openInModal("about:blank"); openInModal("about:blank");
} }
function nextContent() { async function nextContent() {
// Get the currently executing <script> element // Get the currently executing <script> element
var modal = $("#fullScreenModal"); var modal = $("#fullScreenModal");
var currentLi = $("#" + modal.attr("data-block")); var currentLi = $("#" + modal.attr("data-block"));
@ -104,17 +108,27 @@ loadScript(
if (aLink.length > 0) { if (aLink.length > 0) {
// Get the "href" attribute of the <a> element // Get the "href" attribute of the <a> element
var hrefValue = aLink.attr("href"); let hrefValue = aLink.attr("href");
// Check if "feedback" is in the "href" attribute // Check if "feedback" is in the "href" attribute
if (hrefValue.includes("feedback")) { if (hrefValue.includes("feedback")) {
// Get the id URL parameter for debugging purposes
var url = new URL(hrefValue);
var id = url.searchParams.get("id");
// Replace "view" with "complete" in the "href" attribute // Replace "view" with "complete" in the "href" attribute
hrefValue = hrefValue.replace("view", "complete"); hrefValue = hrefValue.replace("view", "complete");
// Go to embed mode
hrefValue = hrefValue + "&embed=1";
} else if (hrefValue.includes("/quiz/")) {
// Replace "view" with "attempt" in the "href" attribute
hrefValue = hrefValue.replace("view", "attempt");
try {
// Call URL to see where it redirects to
const response = await fetch(hrefValue);
hrefValue = response.url;
} catch (error) {
console.error('Error fetching the URL:', error);
}
// Go to embed mode // Go to embed mode
hrefValue = hrefValue + "&embed=1"; hrefValue = hrefValue + "&embed=1";
} }
@ -135,10 +149,26 @@ loadScript(
nextActivity = nextContent; nextActivity = nextContent;
window.top.nextContent = nextContent; window.top.nextContent = nextContent;
function openQuizModal(number) { async function openQuizModal(number) {
// Get the feedback link // Get the feedback link
var href = getFeedbackLinks()[number - 1]; let href = getFeedbackLinks()[number - 1];
if (href.includes("/feedback/")) {
href = href.replace("view", "complete"); href = href.replace("view", "complete");
} else if (href.includes("/quiz/")) {
// Replace "view" with "attempt" in the "href" attribute
href = href.replace("view", "attempt");
try {
// Call URL to see where it redirects to
const response = await fetch(href);
href = response.url;
} catch (error) {
console.error('Error fetching the URL:', error);
}
console.log(href);
}
// Go to embed mode // Go to embed mode
href = href + "&embed=1&modal=1"; href = href + "&embed=1&modal=1";