From 9d7b89d36c1561cc87feb8be58ae13f3d896a0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= <damian.buechel@let.ethz.ch> Date: Mon, 25 Jul 2022 18:37:43 +0200 Subject: [PATCH] SEBWIN-510, #278, #417: Fixed crash when attempting to execute JavaScript. --- SafeExamBrowser.Browser/BrowserControl.cs | 24 +++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/SafeExamBrowser.Browser/BrowserControl.cs b/SafeExamBrowser.Browser/BrowserControl.cs index 8b416bdb..75068c5f 100644 --- a/SafeExamBrowser.Browser/BrowserControl.cs +++ b/SafeExamBrowser.Browser/BrowserControl.cs @@ -7,6 +7,7 @@ */ using System; +using System.Threading.Tasks; using CefSharp; using SafeExamBrowser.Browser.Wrapper; using SafeExamBrowser.Browser.Wrapper.Events; @@ -64,15 +65,26 @@ namespace SafeExamBrowser.Browser public void ExecuteJavascript(string javascript, Action<JavascriptResult> callback) { - control.EvaluateScriptAsync(javascript).ContinueWith(t => + if ((control as IWebBrowser)?.CanExecuteJavascriptInMainFrame == true) { - callback(new JavascriptResult() + control.EvaluateScriptAsync(javascript).ContinueWith(t => { - Message = t.Result.Message, - Result = t.Result.Result, - Success = t.Result.Success + callback(new JavascriptResult + { + Message = t.Result.Message, + Result = t.Result.Result, + Success = t.Result.Success + }); }); - }); + } + else + { + Task.Run(() => callback(new JavascriptResult + { + Message = "JavaScript can't be executed in the main frame!", + Success = false + })); + } } public void Find(string term, bool isInitial, bool caseSensitive, bool forward = true)