mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 04:00:07 +00:00
Add menu to demonstrate javascript evaluation / execution.
This commit is contained in:
@@ -11,8 +11,21 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace CefSharp.Example
|
||||
{
|
||||
/// <summary>
|
||||
/// Methods whose functionaity is mostly implemented by evaluating or
|
||||
/// executing scripts in the browser.
|
||||
/// </summary>
|
||||
public class ScriptedMethods
|
||||
{
|
||||
/// <summary>
|
||||
/// Determine if the active element in a frame accepts text input.
|
||||
/// </summary>
|
||||
/// <param name="frame">
|
||||
/// Test the active element in this frame.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// True if the active element accepts text input.
|
||||
/// </returns>
|
||||
public static async Task<bool> ActiveElementAcceptsTextInput(IFrame frame)
|
||||
{
|
||||
if (frame == null)
|
||||
@@ -52,6 +65,18 @@ namespace CefSharp.Example
|
||||
return (bool)response.Result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the frame contains an element with the specified id.
|
||||
/// </summary>
|
||||
/// <param name="id">
|
||||
/// The id to find.
|
||||
/// </param>
|
||||
/// <param name="frame">
|
||||
/// Test the elements in this frame.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// True if an element with the specified id exists in the frame.
|
||||
/// </returns>
|
||||
public static async Task<bool> ElementWithIdExists(string id, IFrame frame)
|
||||
{
|
||||
if (frame == null)
|
||||
@@ -79,5 +104,62 @@ namespace CefSharp.Example
|
||||
|
||||
return (bool)response.Result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set an event listener on the element with the provided id. When the
|
||||
/// event listener callback is invoked an attempt will be made to pass
|
||||
/// event information to a .Net class bound to the browser. See
|
||||
/// ScriptedMethodsBoundObject.
|
||||
/// </summary>
|
||||
/// <param name="id">
|
||||
/// The id of an element that exists in the frame.
|
||||
/// </param>
|
||||
/// <param name="frame">
|
||||
/// The element is in this frame.
|
||||
/// </param>
|
||||
/// <param name="eventName">
|
||||
/// Subscribe to this event. For example 'click'.
|
||||
/// </param>
|
||||
public static void ListenForEvent(string id, IFrame frame, string eventName)
|
||||
{
|
||||
if (frame == null)
|
||||
{
|
||||
throw new ArgumentException("An IFrame instance is required.", "frame");
|
||||
}
|
||||
|
||||
// Adds a click event listener to a DOM element with the provided
|
||||
// ID. When the element is clicked the ScriptedMethodsBoundObject's
|
||||
// raiseEvent function is invoked. This is one way to get
|
||||
// asynchronous events from the web page. Typically though the web
|
||||
// page would be aware of window.boundEvent.raiseEvent and would
|
||||
// simply raise it as needed.
|
||||
//
|
||||
// Scripts should be minified for production builds. The script
|
||||
// could also be read from a file...
|
||||
var script =
|
||||
"(function () {" +
|
||||
" var counter = 0;" +
|
||||
" var elem = document.getElementById('##ID##');" +
|
||||
" elem.removeAttribute('disabled');" +
|
||||
" elem.addEventListener('##EVENT##', function(e){" +
|
||||
" if (!window.boundEvent){" +
|
||||
" console.log('window.boundEvent does not exist.');" +
|
||||
" return;" +
|
||||
" }" +
|
||||
" counter++;" +
|
||||
" window.boundEvent.raiseEvent('##EVENT##', {count: counter, id: e.target.id, tagName: e.target.tagName});" +
|
||||
" });" +
|
||||
" console.log(`Added ##EVENT## listener to ${elem.id}.`);" +
|
||||
"})();";
|
||||
|
||||
// For simple inline scripts you could use String.Format() but
|
||||
// beware of braces in the javascript code. If reading from a file
|
||||
// it's probably safer to include tokens that can be replaced via
|
||||
// regex.
|
||||
script = Regex.Replace(script, "##ID##", id);
|
||||
script = Regex.Replace(script, "##EVENT##", eventName);
|
||||
|
||||
frame.ExecuteJavaScriptAsync(script);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user