SEBWIN-510, #278, #417: Fixed crash when attempting to execute JavaScript.

This commit is contained in:
Damian Büchel 2022-07-25 18:37:43 +02:00
parent 82e8166fd5
commit 9d7b89d36c

View file

@ -7,6 +7,7 @@
*/ */
using System; using System;
using System.Threading.Tasks;
using CefSharp; using CefSharp;
using SafeExamBrowser.Browser.Wrapper; using SafeExamBrowser.Browser.Wrapper;
using SafeExamBrowser.Browser.Wrapper.Events; using SafeExamBrowser.Browser.Wrapper.Events;
@ -64,15 +65,26 @@ namespace SafeExamBrowser.Browser
public void ExecuteJavascript(string javascript, Action<JavascriptResult> callback) 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, callback(new JavascriptResult
Result = t.Result.Result, {
Success = t.Result.Success 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) public void Find(string term, bool isInitial, bool caseSensitive, bool forward = true)