Пример #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));
        }