From d284599e193ff13967faef9ea88be969a7352dbf Mon Sep 17 00:00:00 2001 From: Glib Glugovskiy Date: Wed, 26 Jun 2024 10:28:46 +0300 Subject: [PATCH] fix: encode spaces as %20 --- lms/static/js/courseware/bridge.js | 47 +++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/lms/static/js/courseware/bridge.js b/lms/static/js/courseware/bridge.js index c8b095f905d5..73f8439a16c9 100644 --- a/lms/static/js/courseware/bridge.js +++ b/lms/static/js/courseware/bridge.js @@ -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); + } } /** @@ -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"); @@ -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); }