public string GetJSCode() { this.TagIDSuffixAddedIDSuffixPairs.Clear(); // 2bs this.CallOtherComponentAppendCodes.Clear(); // 2bs this.CallOtherComponentMethodCodes.Clear(); // 2bs this.VarIndex = 0; JSCode code = this.GetJSCode(this.Html.Root); this.VarIndex = -1; List <string> dest = new List <string>(); dest.Add("function " + this.CoNamePrefix + "CreateElement(coName) {"); dest.AddRange(code.Lines); dest.Add(JS_INDENT + "Riot_Event_Early(function() {"); foreach (string line in this.CallOtherComponentAppendCodes) { dest.Add(line); } dest.Add(JS_INDENT + "});"); dest.Add(JS_INDENT + "Riot_Event_Add(function() {"); foreach (string line in this.CallOtherComponentMethodCodes) { dest.Add(line); } dest.Add(JS_INDENT + "});"); dest.Add(JS_INDENT + "Riot_PostCreateElement(coName, " + code.VarName + ", " + this.CoNamePrefix + "Loaded, " + this.CoNamePrefix + "Shown, " + this.CoNamePrefix + "Refresh, " + this.CoNamePrefix + "Destroy);"); dest.Add(JS_INDENT + "return " + code.VarName + ";"); dest.Add("}"); dest.Add(""); // 空行 dest.Add("function " + this.CoNamePrefix + "GetElements(coName) {"); dest.Add(JS_INDENT + "return Riot_GetElements(coName, ["); foreach (string[] pair in this.TagIDSuffixAddedIDSuffixPairs) { dest.Add(JS_INDENT + JS_INDENT + "[ \"" + pair[0] + "\", \"" + pair[1] + "\" ],"); } dest.Add(JS_INDENT + "]);"); dest.Add("}"); this.TagIDSuffixAddedIDSuffixPairs.Clear(); this.CallOtherComponentAppendCodes.Clear(); this.CallOtherComponentMethodCodes.Clear(); return(FileTools.LinesToText(dest.ToArray())); }
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { Int32 delayAfter = GetValueOrDefault(context, this.DelayAfter, 300); Int32 delayBefore = GetValueOrDefault(context, this.DelayBefore, 300); Int32 timeoutMS = GetValueOrDefault(context, this.TimeoutMS, 10000); object[] args; Thread.Sleep(delayBefore); if (Parameters.Count == 0) { args = new object[0]; } else { args = new object[Parameters.Count]; int i = 0; foreach (InArgument para in Parameters) { args[i++] = context.GetValue(para); } } //var selStr = Selector.Get(context); string jsCode = JSCode.Get(context); if (File.Exists(jsCode)) { string jsContent = File.ReadAllText(jsCode); jsCode = jsContent; } m_Delegate = new runDelegate(Run); //流程运行 只允许一个输入参数 //chrome运行JS与IE不同,首先命名函数,而后执行。IE命名后可直接执行 //返回值必须调用return func(),参数传递必须使用func(arguments[i]) try { /******流程运行******/ //根据OpenBrowser标志获取其抽象化属性 PropertyDescriptor property = context.DataContext.GetProperties()[OpenBrowser.OpenBrowsersPropertyTag]; if (property == null) { //根据AttachBrowser标志获取其抽象化属性 property = context.DataContext.GetProperties()[AttachBrowser.OpenBrowsersPropertyTag]; } if (property == null) { return(m_Delegate.BeginInvoke(callback, state)); } //抽象化属性转换为可用Browser属性 Browser getBrowser = property.GetValue(context.DataContext) as Browser; ChromeDriver chromeDriver = getBrowser.getICFBrowser() as ChromeDriver; InternetExplorerDriver ieDriver = getBrowser.getICFBrowser() as InternetExplorerDriver; FirefoxDriver fxDriver = getBrowser.getICFBrowser() as FirefoxDriver; //流程运行 object returnVaue = null; Int32 circleTimes = timeoutMS / 1000; CallWithTimeout(new Action(() => { if (isCircle) { for (int i = 0; i <= circleTimes; i++) { try { if (chromeDriver != null) { returnVaue = chromeDriver.ExecuteScript(jsCode, args); } else if (fxDriver != null) { returnVaue = fxDriver.ExecuteScript(jsCode, args); } else if (ieDriver != null) { returnVaue = ieDriver.ExecuteScript(jsCode, args); } break; } catch { Thread.Sleep(500); continue; } } } else { if (chromeDriver != null) { returnVaue = chromeDriver.ExecuteScript(jsCode, args); } else if (fxDriver != null) { returnVaue = fxDriver.ExecuteScript(jsCode, args); } else if (ieDriver != null) { returnVaue = ieDriver.ExecuteScript(jsCode, args); } } }), timeoutMS); if (returnVaue != null && JSOut != null) { JSOut.Set(context, returnVaue); } } catch (Exception e) { SharedObject.Instance.Output(SharedObject.enOutputType.Error, "运行JS出错", e + " JS: " + jsCode); if (ContinueOnError.Get(context)) { return(m_Delegate.BeginInvoke(callback, state)); } else { throw new NotImplementedException("执行JS过程出错!"); } } if (isErrorFlag == true && !ContinueOnError.Get(context)) { throw new NotImplementedException("执行JS过程出错!"); } else { Thread.Sleep(delayAfter); return(m_Delegate.BeginInvoke(callback, state)); } }
/// <summary> /// Initialize a new instance of ReplaceRootOperator /// </summary> /// <param name="NewRoot"></param> public ReplaceRootOperator(JSCode NewRoot) { this.NewRoot = NewRoot; }
private JSCode GetJSCode(HtmlDataModel.Tag rootTag) { JSCode code = new JSCode(); code.VarName = "t" + this.VarIndex; this.VarIndex++; List <string> lines = new List <string>(); string otherCoName = null; rootTag.IDSuffix = ""; if (rootTag is HtmlDataModel.ScriptComponent) { string script = ((HtmlDataModel.ScriptComponent)rootTag).Script; script = CommonUtils.ResolveAlias(script, this.CoName); lines.Add(JS_INDENT + "var " + code.VarName + " = " + script); } else if (rootTag is HtmlDataModel.OCMethodCall) { otherCoName = CommonUtils.ResolveAlias(rootTag.Parent.Name, this.CoName); string methodName = rootTag.Name; string args2ndAndLater = ((HtmlDataModel.OCMethodCall)rootTag).Args2ndAndLater; string script; if (args2ndAndLater == null) { script = otherCoName + "_" + methodName + "(coName + \"_" + rootTag.Parent.ID + rootTag.Parent.IDSuffix + "\"); // " + code.VarName; } else { args2ndAndLater = CommonUtils.ResolveAlias(args2ndAndLater, this.CoName); script = otherCoName + "_" + methodName + "(coName + \"_" + rootTag.Parent.ID + rootTag.Parent.IDSuffix + "\", " + args2ndAndLater + "); // " + code.VarName; } this.CallOtherComponentMethodCodes.AddRange(new string[] { JS_INDENT + JS_INDENT + script, }); lines.Add(JS_INDENT + "// " + script); // ToDouble 不要 code.NotUsingVar = true; } else if (rootTag is HtmlDataModel.OtherComponent) { otherCoName = CommonUtils.ResolveAlias(rootTag.Name, this.CoName); if (rootTag.IDCollision) { //idSuffix = "_" + CommonUtils.Unq(); // 重複したときだけ階層が増えてしまう。 rootTag.IDSuffix = CommonUtils.Unq(); } this.TagIDSuffixAddedIDSuffixPairs.Add(new string[] { "_" + rootTag.ID, rootTag.IDSuffix }); lines.Add(JS_INDENT + "var " + code.VarName + " = " + otherCoName + "_CreateElement(coName + \"_" + rootTag.ID + rootTag.IDSuffix + "\");"); } else { if (rootTag.IDCollision) { //idSuffix = "_" + CommonUtils.Unq(); // 重複したときだけ階層が増えてしまう。 rootTag.IDSuffix = CommonUtils.Unq(); } this.TagIDSuffixAddedIDSuffixPairs.Add(new string[] { "_" + rootTag.ID, rootTag.IDSuffix }); lines.Add(JS_INDENT + "var " + code.VarName + " = document.createElement(\"" + rootTag.Name + "\");"); lines.Add(JS_INDENT + code.VarName + ".id = coName + \"_" + rootTag.ID + rootTag.IDSuffix + "\";"); lines.Add(JS_INDENT + "Riot_SetClassName(" + code.VarName + ", \"" + this.CoNamePrefix + rootTag.ID + "\");"); //lines.Add(JS_INDENT + code.VarName + ".className = \"" + this.CoNamePrefix + rootTag.ID + "\";"); // old } foreach (HtmlDataModel.Attribute attribute in rootTag.Attributes) { string attrName = attribute.Name; string attrValue = attribute.Value; attrValue = CommonUtils.ResolveAlias(attrValue, this.CoName); lines.Add(JS_INDENT + code.VarName + ".setAttribute(\"" + attrName + "\", \"" + attrValue + "\");"); } foreach (HtmlDataModel.Property property in rootTag.Properties) { string propName = property.Name; string propValue = property.Value; propValue = CommonUtils.ResolveAlias(propValue, this.CoName); switch (property.Kind) { case HtmlDataModel.Property.Kind_e.REGULAR: lines.Add(JS_INDENT + code.VarName + "." + propName + " = \"" + propValue + "\";"); break; case HtmlDataModel.Property.Kind_e.EVENT: lines.Add(JS_INDENT + code.VarName + "." + propName + " = function() { " + propValue + " };"); break; default: throw null; // never } } foreach (HtmlDataModel.Tag tag in rootTag.Children) { int cocacInsertIndex = this.CallOtherComponentAppendCodes.Count; JSCode subCode = this.GetJSCode(tag); lines.AddRange(subCode.Lines); if (subCode.NotUsingVar) { //lines.Add(JS_INDENT + "// " + subCode.VarName); // HACK: 不要 } else if (rootTag is HtmlDataModel.OtherComponent) { this.CallOtherComponentAppendCodes.InsertRange(cocacInsertIndex, new string[] { //JS_INDENT + "Riot_Event_Early(function() {", JS_INDENT + JS_INDENT + otherCoName + "_Append(coName + \"_" + rootTag.ID + rootTag.IDSuffix + "\", " + subCode.VarName + ");", //JS_INDENT + "});", }); } else { lines.Add(JS_INDENT + code.VarName + ".appendChild(" + subCode.VarName + ");"); } } code.Lines = lines.ToArray(); return(code); }