Пример #1
0
        /// <summary>
        /// Evaluates the specified JavaScript code within the scope of this
        /// document. Returns the value computed by the last expression in the
        /// code block after implicit conversion to a string.
        /// </summary>
        /// <example>
        /// The following example shows an alert dialog then returns the string "4".
        /// <code>
        /// Eval("window.alert('Hello world!'); 2 + 2");
        /// </code>
        /// </example>
        /// <param name="javaScriptCode">The JavaScript code</param>
        /// <returns>The result converted to a string</returns>
        /// <exception cref="JavaScriptException">Thrown when the JavaScript code cannot be evaluated
        /// or throws an exception during evaluation</exception>
        public virtual string Eval(string javaScriptCode)
        {
            var documentVariableName = NativeDocument.JavaScriptVariableName;

            var resultVar = documentVariableName + "." + RESULT_PROPERTY_NAME;
            var errorVar  = documentVariableName + "." + ERROR_PROPERTY_NAME;

            var exprWithAssignment = resultVar + " = '';" + errorVar + " = '';"
                                     + "try {"
                                     + resultVar + " = String(eval(" + JavascriptStringEncoder.Encode(javaScriptCode) + "))"
                                     + "} catch (error) {"
                                     + errorVar + " = 'message' in error ? error.name + ': ' + error.message : String(error)"
                                     + "};";

            // Run the script.
            RunScript(exprWithAssignment);

            // See if an error occured.
            var error = NativeDocument.GetPropertyValue(ERROR_PROPERTY_NAME);

            if (!string.IsNullOrEmpty(error))
            {
                throw new JavaScriptException(error);
            }

            // Return the result
            return(NativeDocument.GetPropertyValue(RESULT_PROPERTY_NAME));
        }
        /// <summary>
        /// Evaluates the specified JavaScript code within the scope of this
        /// document. Returns the value computed by the last expression in the
        /// code block after implicit conversion to a string.
        /// </summary>
        /// <example>
        /// The following example shows an alert dialog then returns the string "4".
        /// <code>
        /// Eval("window.alert('Hello world!'); 2 + 2");
        /// </code>
        /// </example>
        /// <param name="javaScriptCode">The JavaScript code</param>
        /// <returns>The result converted to a string</returns>
        /// <exception cref="JavaScriptException">Thrown when the JavaScript code cannot be evaluated
        /// or throws an exception during evaluation</exception>
        public virtual string Eval(string javaScriptCode)
        {
            var documentVariableName = NativeDocument.JavaScriptVariableName;
            var resultPropertyName   = RESULT_PROPERTY_NAME + RandomString(10);
            var errorPropertyName    = ERROR_PROPERTY_NAME + RandomString(10);

            var resultVar = documentVariableName + "." + resultPropertyName;
            var errorVar  = documentVariableName + "." + errorPropertyName;

            var exprWithAssignment = resultVar + " = '';" + errorVar + " = '';"
                                     + "try {"
                                     + resultVar + " = String(eval('" + javaScriptCode.Replace("'", "\\'") + "'))"
                                     + "} catch (error) {"
                                     + errorVar + " = 'message' in error ? error.name + ': ' + error.message : String(error)"
                                     + "};";

            // Run the script.
            RunScript(exprWithAssignment);
            var allElements = NativeDocument.AllElements; // forcing browser to refresh all elements
            // See if an error occured.
            var error = NativeDocument.GetPropertyValue(errorPropertyName);

            if (!string.IsNullOrEmpty(error))
            {
                throw new JavaScriptException(error);
            }

            // Return the result
            return(NativeDocument.GetPropertyValue(resultPropertyName));
        }
Пример #3
0
 /// <summary>
 /// Runs the script code in IE.
 /// </summary>
 /// <param name="scriptCode">The script code.</param>
 /// <param name="language">The language.</param>
 public virtual void RunScript(string scriptCode, string language)
 {
     NativeDocument.RunScript(scriptCode, language);
 }
 /// <summary>
 /// Determines whether the text inside the HTML Body element contains the given <paramref name="text" />.
 /// </summary>
 /// <param name="text">The text.</param>
 /// <returns>
 ///     <c>true</c> if the specified text is contained in <see cref="Html"/>; otherwise, <c>false</c>.
 /// </returns>
 public virtual bool ContainsText(string text)
 {
     return(NativeDocument.ContainsText(text));
 }