/// <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)); }
public string GetCssMarkRemovalScript(string cssSelector, string markerClass) { return (@"(function(cssSelector, markerClass) { window.jQuery(cssSelector).removeClass(markerClass); })(" + JavascriptStringEncoder.Encode(cssSelector) + ", " + JavascriptStringEncoder.Encode(markerClass) + ");"); }
/// <summary> /// Runs the javascript code in IE. /// </summary> /// <param name="javaScriptCode">The javascript code.</param> public virtual void RunScript(string javaScriptCode) { if (javaScriptCode.Length > this.RunScriptMaxChunk) { RunScript("_watinChunkJS='';"); const string head = "_watinChunkJS+=\""; const string tail = "\";"; const int insertSize = 1024; const int worstCaseEncodingLengthOfInsert = 1024 * 6; StringBuilder scriptChunk = new StringBuilder(); int maxSizeToInsertInto = this.RunScriptMaxChunk - worstCaseEncodingLengthOfInsert - tail.Length; int stringPosition = 0; while (stringPosition < javaScriptCode.Length) { scriptChunk.Append(head); while (scriptChunk.Length < maxSizeToInsertInto && stringPosition < javaScriptCode.Length) { int amountToWrite = Math.Min(insertSize, javaScriptCode.Length - stringPosition); JavascriptStringEncoder.JsonStringEncodeWithinDoubleParenthesis(scriptChunk, javaScriptCode.Substring(stringPosition, amountToWrite)); stringPosition += amountToWrite; } scriptChunk.Append(tail); RunScript(scriptChunk.ToString(), "javascript"); scriptChunk.Length = 0; } RunScript("eval(_watinChunkJS);_watinChunkJS=null;", "javascript"); } else { RunScript(javaScriptCode, "javascript"); } }