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) => {
const href = link.getAttribute("href");
// Check if the href includes "/feedback/"
if (href && href.includes("/feedback/")) {
if (href) {
if (href.includes("/feedback/")) {
feedbackLinks.push(href);
} else if (href.includes("/quiz/")) {
feedbackLinks.push(href);
}
}
});
@ -91,7 +95,7 @@ loadScript(
openInModal("about:blank");
}
function nextContent() {
async function nextContent() {
// Get the currently executing <script> element
var modal = $("#fullScreenModal");
var currentLi = $("#" + modal.attr("data-block"));
@ -104,17 +108,27 @@ loadScript(
if (aLink.length > 0) {
// 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
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
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
hrefValue = hrefValue + "&embed=1";
}
@ -135,10 +149,26 @@ loadScript(
nextActivity = nextContent;
window.top.nextContent = nextContent;
function openQuizModal(number) {
async function openQuizModal(number) {
// Get the feedback link
var href = getFeedbackLinks()[number - 1];
let href = getFeedbackLinks()[number - 1];
if (href.includes("/feedback/")) {
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
href = href + "&embed=1&modal=1";