Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: encode spaces as %20 #2585

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
47 changes: 36 additions & 11 deletions lms/static/js/courseware/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,33 @@
*/

/**
* Sends a data about student's answer to the IOS app.
*
* @param {string} message The stringified JSON object to be sent to the IOS app
* Sends a JSON-formatted message to the iOS bridge if available.
* @param {string} message - The JSON message to send.
*/
function sendMessageToIOS(message) {
window?.webkit?.messageHandlers?.IOSBridge?.postMessage(message);
try {
if (window?.webkit?.messageHandlers?.IOSBridge) {
window.webkit.messageHandlers.IOSBridge.postMessage(message);
console.log("Message sent to iOS:", message);
}
} catch (error) {
console.error("Failed to send message to iOS:", error);
}
}

/**
* Sends a data about student's answer to the Android app.
*
* @param {string} message The stringified JSON object to be sent to the native Android app
* Sends a JSON-formatted message to the Android bridge if available.
* @param {string} message - The JSON message to send.
*/
function sendMessageToAndroid(message) {
window?.AndroidBridge?.postMessage(message);
try {
if (window?.AndroidBridge) {
window.AndroidBridge.postMessage(message);
console.log("Message sent to Android:", message);
}
} catch (error) {
console.error("Failed to send message to Android:", error);
}
}

/**
Expand All @@ -34,7 +46,14 @@ function sendMessageToAndroid(message) {
* @param {string} message The stringified JSON object about the student's answer from the native mobile app.
*/
function markProblemCompleted(message) {
const data = JSON.parse(message).data;
let data;
try {
data = JSON.parse(message).data
} catch (error) {
console.error("Failed to parse message:", error)
return
}

const problemContainer = $(".xblock-student_view");

const submitButton = problemContainer.find(".submit-attempt-container .submit");
Expand Down Expand Up @@ -69,8 +88,14 @@ function markProblemCompleted(message) {
const originalAjax = $.ajax;
$.ajax = function (options) {
if (options.url && options.url.endsWith("handler/xmodule_handler/problem_check")) {
sendMessageToIOS(JSON.stringify(options));
sendMessageToAndroid(JSON.stringify(options));
if (options.data) {
// Replace spaces with URLEncoded value to ensure correct parsing on the backend
let formattedData = options.data.replace(/\+/g, '%20');
let jsonMessage = JSON.stringify(formattedData)

sendMessageToIOS(jsonMessage)
sendMessageToAndroid(jsonMessage)
}
}
return originalAjax.call(this, options);
}
Loading