好的,我目前正在使用nodeJS进行UIAutomation,并且正在使用EDGE.js节点模块。一切正常(哇),但是我对代码可重用性有疑问。
我几乎没有超过50%的由相同代码组成的几乎相同的函数。当然,我想将此代码移到单个位置,但是问题是此代码放在js注释中(EDGE东西)。
如何重用代码以避免在EDGE.js中重复?
是的..作为最后的手段,我可以将所有内容放到一个c#“程序”中,并根据参数调用不同的c#函数,但是也许有一种方法可以保留多个js函数?谢谢!
这是2个功能的示例。我想在每个块的底部保留不同的仅“公共异步任务”部分。有任何想法吗?
顺便说一句:也欢迎任何有关C#代码的建议!因为我很确定,这完全是废话^^
getWindows: function() {
/*
using System;
using System.Windows;
using System.Windows.Automation;
using System.Threading.Tasks;
using System.Collections.Generic;
public class myRect
{
public int width { get; set; }
public int height { get; set; }
public int top { get; set; }
public int left { get; set; }
public myRect( AutomationElement el ) {
System.Windows.Rect r = (System.Windows.Rect)(
el.GetCurrentPropertyValue(
AutomationElement.BoundingRectangleProperty,
true));
width = (int)r.Width;
height = (int)r.Height;
top = (int)r.Top;
left = (int)r.Left;
}
}
public class Winfo
{
public string name { get; set; }
public string automationId { get; set; }
public int processId { get; set; }
public myRect window { get; set; }
public myRect browser { get; set; }
}
public class Startup {
private Winfo getWinInfo( AutomationElement el ) {
if ( el == null ) return( null );
Winfo winfo = new Winfo {
name = el.Current.Name,
automationId = el.Current.AutomationId,
processId = el.Current.ProcessId,
window = new myRect(el)
};
try {
var tmpWeb = el
.FindFirst( TreeScope.Descendants,
new PropertyCondition(
AutomationElement.ClassNameProperty,
"CefBrowserWindow") )
.FindFirst( TreeScope.Descendants,
new PropertyCondition(
AutomationElement.NameProperty,
"Chrome Legacy Window"));
winfo.browser = new myRect(tmpWeb);
} catch { winfo.browser = null; }
return(winfo);
}
public async Task<object> Invoke(dynamic input) {
var els = AutomationElement.RootElement.FindAll(
TreeScope.Children,
Condition.TrueCondition);
List<Winfo> windowList = new List<Winfo>{};
bool all;
try { all = (bool)input.all; } catch { all = false; };
foreach (AutomationElement el in els) {
Winfo winfo = getWinInfo(el);
if ((winfo!=null) && (all || (winfo.browser!=null))) {
windowList.Add( winfo );
}
}
return(windowList);
}
}
*/
}
还有一个
waitWindow: function() {
/*
using System;
using System.Windows;
using System.ComponentModel;
using System.Windows.Automation;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
public class myRect {
public int width { get; set; }
public int height { get; set; }
public int top { get; set; }
public int left { get; set; }
public myRect( AutomationElement el ) {
System.Windows.Rect r = (System.Windows.Rect)(
el.GetCurrentPropertyValue(
AutomationElement.BoundingRectangleProperty,
true));
width = (int)r.Width;
height = (int)r.Height;
top = (int)r.Top;
left = (int)r.Left;
}
}
public class Winfo
{
public string name { get; set; }
public string automationId { get; set; }
public int processId { get; set; }
public myRect window { get; set; }
public myRect browser { get; set; }
}
public class Startup {
private static AutoResetEvent waitHandle;
private Winfo getWinInfo( AutomationElement el ) {
if ( el == null ) return( null );
Winfo winfo = new Winfo {
name = el.Current.Name,
automationId = el.Current.AutomationId,
processId = el.Current.ProcessId,
window = new myRect(el)
};
try {
var tmpWeb = el
.FindFirst( TreeScope.Descendants,
new PropertyCondition(
AutomationElement.ClassNameProperty,
"CefBrowserWindow") )
.FindFirst( TreeScope.Descendants,
new PropertyCondition(
AutomationElement.NameProperty,
"Chrome Legacy Window"));
winfo.browser = new myRect(tmpWeb);
} catch { winfo.browser = null; }
return(winfo);
}
public async Task<object> Invoke(dynamic input) {
int t;
try { t = (int)input.timeout; } catch { t = 0; };
string wname;
try { wname = (string)input.name; } catch { wname = ""; };
AutomationElement el = AutomationElement.RootElement.FindFirst(
TreeScope.Children,
new PropertyCondition( AutomationElement.NameProperty, wname ));
if ( el == null ) {
waitHandle = new AutoResetEvent(false);
Automation.AddAutomationEventHandler(
WindowPattern.WindowOpenedEvent,
AutomationElement.RootElement,
TreeScope.Children,
(sender, e) => {
var obj = sender as AutomationElement;
if (obj.Current.Name == wname) {
el = obj;
waitHandle.Set();
}
}
);
waitHandle.WaitOne(t);
Automation.RemoveAllEventHandlers();
}
return( getWinInfo(el) );
}
}
*/
}
};
参考方案
您可以使用edgejs使用的相同技术将可重用的C#代码拆分为单独的多行javascript字符串。
下面是一个简单的示例,其中一个函数已分为两个单独的变量Line1和Line2。您可以将代码拆分为多个包含可重用代码的函数/变量,然后通过串联各个位来构建代码。
var edge = require('edge');
function getMultilineString(fn){
return (fn).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
}
var line1 = getMultilineString(function () {/*
async (input) => {
*/});
var line2 = getMultilineString(function () {/*
return ".NET welcomes " + input.ToString();
}
*/});
//var hello = edge.func(function () {/*
// async (input) => {
// return ".NET welcomes " + input.ToString();
// }
//*/});
var hello = edge.func(line1 + line2);
hello('Node.js', function (error, result) {
if (error) throw error;
console.log(result);
});
Javascript-从当前网址中删除查询字符串 - javascript单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…
Javascript IF语句 - javascript嗨,我有这段代码可以正常工作,并将两个日历显示为一个日历。我还有一个php变量$login_session,其中包含登录电子邮件地址的用户。关于如何显示[email protected]日历的任何想法(伪代码)IF $login_session == "[email protected]…
如何根据手势滚动网页 - javascript如何使用pyhton / javascript使用手势向上/向下或向左/向右滚动页面。我尝试了很多找到解决方案,但无法解决。 参考方案 Javascript手势插件(JSHG)是一个很棒的插件,它使您可以通过现有网站或Web应用程序中的网络摄像头来支持用户的手势识别。您无需具有计算机视觉的任何背景即可使用此插件。当前,它提供了一组基本的手势,其中包括手的位置…
从一个脚本到另一个脚本的统一变量访问 - javascript所以我有一个统一的3D文本上的文本网格脚本,我希望它从具有OnMouseDown函数的多维数据集中访问一个变量,在该函数内部它会更改分数,并且我希望将该分数输出到文本。当我在CS中编写文本脚本时,多维数据集脚本是用JavaScript编写的。谁有想法? 参考方案 如果我误解了您的问题,请纠正我。选项1:C#代码在js代码之前进行编译,这意味着您无权从c#代码…
在文本字段中键入时显示预制建议列表 - javascript如标题所说我正在处理一个mysql项目,我希望该文本字段在键入它时向现有用户提供建议(管理员控制页以禁止用户..类似的东西)一个简单的例子来解释:当我输入文字时..我想给用户输入建议假设他正在输入颜色名称所以当他在其中输入b在他继续之前,会出现一个列表,向他提供我们预先设定的建议,例如:黑色棕色蓝色然后如果他输入l为bl该列表将是:黑色蓝色布拉布拉有什么建议…