Skip to content

Commit b11e8ea

Browse files
PrinceOfEgyptamaitland
authored andcommitted
Add overloads for EvaluateScriptAsync to support methodName and args (#1738)
* Add overloads for EvaluateScriptAsync to support passing in methodName and args * Fixing typo (teh => the)
1 parent 0282135 commit b11e8ea

File tree

1 file changed

+80
-39
lines changed

1 file changed

+80
-39
lines changed

CefSharp/WebBrowserExtensions.cs

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -194,45 +194,7 @@ public static Task<string> GetTextAsync(this IWebBrowser browser)
194194
/// <param name="args">the arguments to be passed as params to the method</param>
195195
public static void ExecuteScriptAsync(this IWebBrowser browser, string methodName, params object[] args)
196196
{
197-
var stringBuilder = new StringBuilder();
198-
stringBuilder.Append(methodName);
199-
stringBuilder.Append("(");
200-
201-
if(args.Length > 0)
202-
{
203-
for (int i = 0; i < args.Length; i++)
204-
{
205-
var obj = args[i];
206-
if (obj == null)
207-
{
208-
stringBuilder.Append("null");
209-
}
210-
else if (numberTypes.Contains(obj.GetType()))
211-
{
212-
stringBuilder.Append(Convert.ToString(args[i],CultureInfo.InvariantCulture));
213-
}
214-
else if (obj is bool)
215-
{
216-
stringBuilder.Append(args[i].ToString().ToLowerInvariant());
217-
}
218-
else
219-
{
220-
stringBuilder.Append("'");
221-
stringBuilder.Append(args[i].ToString().Replace("'","\\'"));
222-
stringBuilder.Append("'");
223-
}
224-
225-
stringBuilder.Append(", ");
226-
}
227-
228-
//Remove the trailing comma
229-
stringBuilder.Remove(stringBuilder.Length - 2, 2);
230-
}
231-
232-
stringBuilder.Append(");");
233-
234-
var script = stringBuilder.ToString();
235-
197+
var script = GetScript(methodName, args);
236198
browser.ExecuteScriptAsync(script);
237199
}
238200

@@ -686,6 +648,37 @@ public static Task<JavascriptResponse> EvaluateScriptAsync(this IWebBrowser brow
686648
}
687649
}
688650

651+
/// <summary>
652+
/// Evaluate some Javascript code in the context of this WebBrowser. The script will be executed asynchronously and the
653+
/// method returns a Task encapsulating the response from the Javascript
654+
/// This simple helper extension will encapsulate params in single quotes (unless int, uint, etc)
655+
/// </summary>
656+
/// <param name="browser">The ChromiumWebBrowser instance this method extends</param>
657+
/// <param name="methodName">The javascript method name to execute</param>
658+
/// <param name="args">the arguments to be passed as params to the method</param>
659+
/// <returns><see cref="Task{JavascriptResponse}"/> that can be awaited to perform the script execution</returns>
660+
public static Task<JavascriptResponse> EvaluateScriptAsync(this IWebBrowser browser, string methodName, params object[] args)
661+
{
662+
return browser.EvaluateScriptAsync(null, methodName, args);
663+
}
664+
665+
/// <summary>
666+
/// Evaluate some Javascript code in the context of this WebBrowser using the specified timeout. The script will be executed asynchronously and the
667+
/// method returns a Task encapsulating the response from the Javascript
668+
/// This simple helper extension will encapsulate params in single quotes (unless int, uint, etc)
669+
/// </summary>
670+
/// <param name="browser">The ChromiumWebBrowser instance this method extends</param>
671+
/// <param name="timeout">The timeout after which the Javascript code execution should be aborted.</param>
672+
/// <param name="methodName">The javascript method name to execute</param>
673+
/// <param name="args">the arguments to be passed as params to the method</param>
674+
/// <returns><see cref="Task{JavascriptResponse}"/> that can be awaited to perform the script execution</returns>
675+
public static Task<JavascriptResponse> EvaluateScriptAsync(this IWebBrowser browser, TimeSpan? timeout, string methodName, params object[] args)
676+
{
677+
var script = GetScript(methodName, args);
678+
679+
return browser.EvaluateScriptAsync(script, timeout);
680+
}
681+
689682
public static void SetAsPopup(this IWebBrowser browser)
690683
{
691684
var internalBrowser = (IWebBrowserInternal)browser;
@@ -702,6 +695,54 @@ public static void ThrowExceptionIfBrowserNotInitialized(this IWebBrowser browse
702695
}
703696
}
704697

698+
/// <summary>
699+
/// Transforms the methodName and arguments into valid Javascript code. Will encapsulate params in single quotes (unless int, uint, etc)
700+
/// </summary>
701+
/// <param name="methodName">The javascript method name to execute</param>
702+
/// <param name="args">the arguments to be passed as params to the method</param>
703+
/// <returns>The Javascript code</returns>
704+
private static string GetScript(string methodName, object[] args)
705+
{
706+
var stringBuilder = new StringBuilder();
707+
stringBuilder.Append(methodName);
708+
stringBuilder.Append("(");
709+
710+
if (args.Length > 0)
711+
{
712+
for (int i = 0; i < args.Length; i++)
713+
{
714+
var obj = args[i];
715+
if (obj == null)
716+
{
717+
stringBuilder.Append("null");
718+
}
719+
else if (numberTypes.Contains(obj.GetType()))
720+
{
721+
stringBuilder.Append(Convert.ToString(args[i], CultureInfo.InvariantCulture));
722+
}
723+
else if (obj is bool)
724+
{
725+
stringBuilder.Append(args[i].ToString().ToLowerInvariant());
726+
}
727+
else
728+
{
729+
stringBuilder.Append("'");
730+
stringBuilder.Append(args[i].ToString().Replace("'", "\\'"));
731+
stringBuilder.Append("'");
732+
}
733+
734+
stringBuilder.Append(", ");
735+
}
736+
737+
//Remove the trailing comma
738+
stringBuilder.Remove(stringBuilder.Length - 2, 2);
739+
}
740+
741+
stringBuilder.Append(");");
742+
743+
return stringBuilder.ToString();
744+
}
745+
705746
private static void ThrowExceptionIfFrameNull(IFrame frame)
706747
{
707748
if (frame == null)

0 commit comments

Comments
 (0)