From 6cbae7e442cdac20ce3160ae9bc3cf0065c98fba Mon Sep 17 00:00:00 2001 From: abose Date: Mon, 15 Jul 2024 13:08:15 +0530 Subject: [PATCH] fix: mitigate alert and confirm apis not working in embeded live preview in windows desktop --- .../LivePreviewTransportRemote.js | 32 ++++++++++++++++--- src/assets/phoenix-splash/index.html | 2 +- .../Phoenix-live-preview/main.js | 5 ++- src/nls/root/strings.js | 2 ++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js b/src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js index 8277e6aae..19345f6ba 100644 --- a/src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js +++ b/src/LiveDevelopment/BrowserScripts/LivePreviewTransportRemote.js @@ -329,15 +329,39 @@ } }); + let alertStr = "Alert", confirmStr = "Confirm"; + + function alertPatch(message) { + // in tauri windows, only the prompt API works. so we use it for alerts as well + // we cant use html alerts here as the alert api will pause js which we cant do via js alone. + prompt(alertStr, message); + } + + function confirmPatch(message) { + // in tauri windows, only the prompt API works. so we use it for confirm as well + // we cant use html alerts here as the alert api will pause js which we cant do via js alone. + const response = prompt(confirmStr, message); + // prompt will return null if cancel is pressed + return !!response; + } + // this is for managing who am i context in iframes embedded in phoenix to have special handling. window.addEventListener('message', function(event) { if (!TRANSPORT_CONFIG.TRUSTED_ORIGINS_EMBED[event.origin]) { return; // Ignore messages from unexpected origins } - - window.__PHOENIX_EMBED_INFO = { - isTauri: event.data.isTauri - }; + if(event.data.type === "WHO_AM_I_RESPONSE") { + window.__PHOENIX_EMBED_INFO = { + isTauri: event.data.isTauri + }; + alertStr = event.data.alertStr || alertStr; + confirmStr = event.data.confirmStr || confirmStr; + if(event.data.isTauri && event.data.platform === 'win') { + // patch alert and confirm as in windows iframes in tauri, these are not present. + window.alert = alertPatch; + window.confirm = confirmPatch; + } + } }); if(window.self !== window.parent){ // in an iframe diff --git a/src/assets/phoenix-splash/index.html b/src/assets/phoenix-splash/index.html index b671d64cf..435220579 100644 --- a/src/assets/phoenix-splash/index.html +++ b/src/assets/phoenix-splash/index.html @@ -12,7 +12,7 @@

Phoenix Code

- Code Creatively: Visual Editing Tailored for Developers + The text editor designed to make coding as simple and fun as playing a video game

...

diff --git a/src/extensionsIntegrated/Phoenix-live-preview/main.js b/src/extensionsIntegrated/Phoenix-live-preview/main.js index ec29bd47e..05e430df7 100644 --- a/src/extensionsIntegrated/Phoenix-live-preview/main.js +++ b/src/extensionsIntegrated/Phoenix-live-preview/main.js @@ -129,7 +129,10 @@ define(function (require, exports, module) { const iframeDom = $iframe[0]; iframeDom.contentWindow.postMessage({ type: "WHO_AM_I_RESPONSE", - isTauri: Phoenix.isNativeApp + isTauri: Phoenix.isNativeApp, + platform: Phoenix.platform, + alertStr: Strings.ALERT, + confirmStr: Strings.CONFIRM }, "*"); // this is not sensitive info, and is only dispatched if requested by the iframe } }); diff --git a/src/nls/root/strings.js b/src/nls/root/strings.js index 815307aa9..c234ced0e 100644 --- a/src/nls/root/strings.js +++ b/src/nls/root/strings.js @@ -128,6 +128,8 @@ define({ "ERROR_MAX_FILES": "This project contains more than 30,000 files. Features that operate across multiple files may be disabled or behave as if the project is empty. Read more about working with large projects.", // Live Preview error strings + "ALERT": "Alert", + "CONFIRM": "Confirm", "ERROR_LAUNCHING_BROWSER_TITLE": "Error Launching Browser", "ERROR_CANT_FIND_CHROME": "The Google Chrome browser could not be found. Please make sure it is installed.", "ERROR_LAUNCHING_BROWSER": "An error occurred when launching the browser. (error {0})",