add JavascriptResult

This commit is contained in:
Jonas Sourlier 2022-05-03 12:53:07 +02:00
parent 117ced0bf5
commit dbecaff451
4 changed files with 40 additions and 3 deletions

View file

@ -15,6 +15,7 @@ using SafeExamBrowser.Configuration.Contracts;
using SafeExamBrowser.Configuration.Contracts.Cryptography;
using SafeExamBrowser.I18n.Contracts;
using SafeExamBrowser.UserInterface.Contracts.Browser;
using SafeExamBrowser.UserInterface.Contracts.Browser.Data;
using SafeExamBrowser.UserInterface.Contracts.Browser.Events;
namespace SafeExamBrowser.Browser
@ -147,10 +148,15 @@ namespace SafeExamBrowser.Browser
/// <summary>
/// Executes the given Javascript code in the browser.
/// </summary>
public async void ExecuteJavascript(string javascript, System.Action<dynamic> callback)
public async void ExecuteJavascript(string javascript, Action<JavascriptResult> callback)
{
var result = await this.control.EvaluateScriptAsync(javascript);
callback(result);
callback(new JavascriptResult()
{
Message = result.Message,
Result = result.Result,
Success = result.Success
});
}
}
}

View file

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SafeExamBrowser.UserInterface.Contracts.Browser.Data
{
/// <summary>
/// The data resulting from a Javascript expression evaluation.
/// </summary>
public class JavascriptResult
{
/// <summary>
/// Indicates if the Javascript was evaluated successfully or not.
/// </summary>
public bool Success { get; set; }
/// <summary>
/// The error message, in case of an unsuccessful evaluation of the Javascript expression.
/// </summary>
public string Message { get; set; }
/// <summary>
/// The data item returned by the Javascript expression.
/// </summary>
public dynamic Result { get; set; }
}
}

View file

@ -6,6 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using SafeExamBrowser.UserInterface.Contracts.Browser.Data;
using SafeExamBrowser.UserInterface.Contracts.Browser.Events;
namespace SafeExamBrowser.UserInterface.Contracts.Browser
@ -64,7 +65,7 @@ namespace SafeExamBrowser.UserInterface.Contracts.Browser
/// <summary>
/// Executes the given Javascript code in the browser.
/// </summary>
void ExecuteJavascript(string javascript, System.Action<dynamic> callback);
void ExecuteJavascript(string javascript, System.Action<JavascriptResult> callback);
/// <summary>
/// Attempts to find the given term on the current page according to the specified parameters.

View file

@ -55,6 +55,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Browser\Data\DownloadItemState.cs" />
<Compile Include="Browser\Data\JavascriptResult.cs" />
<Compile Include="Browser\Events\AddressChangedEventHandler.cs" />
<Compile Include="Browser\Events\FindRequestedEventHandler.cs" />
<Compile Include="Browser\Events\LoadFailedEventHandler.cs" />