using RuriLib.LS; using System; using System.Windows.Media; namespace RuriLib { /// /// A block that executes javascript code in the selenium-driven browser. /// public class SBlockExecuteJS : BlockBase { private string javascriptCode = "alert('henlo');"; /// The javascript code. public string JavascriptCode { get { return javascriptCode; } set { javascriptCode = value; OnPropertyChanged(); } } /// /// Creates an ExecuteJS block. /// public SBlockExecuteJS() { Label = "EXECUTE JS"; } /// public override BlockBase FromLS(string line) { // Trim the line var input = line.Trim(); // Parse the label if (input.StartsWith("#")) Label = LineParser.ParseLabel(ref input); /* * Syntax: * EXECUTEJS "SCRIPT" * */ JavascriptCode = LineParser.ParseLiteral(ref input, "SCRIPT"); return this; } /// public override string ToLS(bool indent = true) { var writer = new BlockWriter(GetType(), indent, Disabled); writer .Label(Label) .Token("EXECUTEJS") .Literal(JavascriptCode.Replace("\r\n", " ").Replace("\n", " ")); return writer.ToString(); } /// public override void Process(BotData data) { base.Process(data); if (data.Driver == null) { data.Log(new LogEntry("Open a browser first!", Colors.White)); throw new Exception("Browser not open"); } data.Log(new LogEntry("Executing JS code!", Colors.White)); data.Driver.ExecuteScript(ReplaceValues(javascriptCode, data)); data.Log(new LogEntry("... executed!", Colors.White)); UpdateSeleniumData(data); } } }