Reformat inline js (use @ notation so much of the string encapsulation can be removed)

Make ScriptedMethods class static and convert the methods into extension methods (really just for the hell of it)
Make some of the comments a little less verbose in terms of number of lines
Some Resharper inspired simplifications
Other code reformatting to make consistent with other parts of the project (switch case statements should have braces, ContinueWith blocks reformatted to match others)
Untabify ScriptedMethodsTest.html
Remove unused usings from ScriptedMethods
Add a few comments about camelCaseJavascriptNames
This commit is contained in:
amaitland
2015-12-30 09:57:34 +10:00
parent 0db20550ae
commit d6fbe2e60b
6 changed files with 140 additions and 145 deletions
+54 -71
View File
@@ -3,9 +3,6 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@@ -15,18 +12,14 @@ namespace CefSharp.Example
/// Methods whose functionaity is mostly implemented by evaluating or
/// executing scripts in the browser.
/// </summary>
public class ScriptedMethods
public static 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)
/// <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(this IFrame frame)
{
if (frame == null)
{
@@ -35,26 +28,27 @@ namespace CefSharp.Example
// Scripts should be minified for production builds. The script
// could also be read from a file...
var script =
"(function () {" +
" var isText = false;" +
" var activeElement = document.activeElement;" +
" if (activeElement) {" +
" if (activeElement.tagName.toLowerCase() === 'textarea') {" +
" isText = true;" +
" } else {" +
" if (activeElement.tagName.toLowerCase() === 'input') {" +
" if (activeElement.hasAttribute('type')) {" +
" var inputType = activeElement.getAttribute('type').toLowerCase();" +
" if (inputType === 'text' || inputType === 'email' || inputType === 'password' || inputType === 'tel' || inputType === 'number' || inputType === 'range' || inputType === 'search' || inputType === 'url') {" +
" isText = true;" +
" }" +
" }" +
" }" +
" }" +
" }" +
" return isText;" +
"})();";
const string script =
@"(function ()
{
var isText = false;
var activeElement = document.activeElement;
if (activeElement) {
if (activeElement.tagName.toLowerCase() === 'textarea') {
isText = true;
} else {
if (activeElement.tagName.toLowerCase() === 'input') {
if (activeElement.hasAttribute('type')) {
var inputType = activeElement.getAttribute('type').toLowerCase();
if (inputType === 'text' || inputType === 'email' || inputType === 'password' || inputType === 'tel' || inputType === 'number' || inputType === 'range' || inputType === 'search' || inputType === 'url') {
isText = true;
}
}
}
}
}
return isText;
})();";
var response = await frame.EvaluateScriptAsync(script);
if (!response.Success)
@@ -68,16 +62,10 @@ namespace CefSharp.Example
/// <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)
/// <param name="frame">Test the elements in this frame.</param>
/// <param name="id">The id to find.</param>
/// <returns>True if an element with the specified id exists in the frame.</returns>
public static async Task<bool> ElementWithIdExists(this IFrame frame, string id)
{
if (frame == null)
{
@@ -85,10 +73,10 @@ namespace CefSharp.Example
}
var script =
"(function () {" +
" var n = document.getElementById('##ID##');" +
" return n !== null && typeof n !== 'undefined';" +
"})();";
@"(function () {
var n = document.getElementById('##ID##');
return n !== null && typeof n !== 'undefined';
})();";
// For simple inline scripts you could use String.Format() but
// beware of braces in the javascript code. If reading from a file
@@ -111,16 +99,10 @@ namespace CefSharp.Example
/// 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)
/// <param name="frame">The element is in this frame.</param>
/// <param name="id">The id of an element that exists in the frame.</param>
/// <param name="eventName">Subscribe to this event. For example 'click'.</param>
public static void ListenForEvent(this IFrame frame, string id, string eventName)
{
if (frame == null)
{
@@ -129,28 +111,29 @@ namespace CefSharp.Example
// 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
// 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
// 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}.`);" +
"})();";
@"(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++;
//NOTE RaiseEvent was converted to raiseEvent in JS (this is configurable when registering the object)
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