如何在EDGE.js(nodeJS.NET数据包)中重用代码
How to reuse code in EDGE.js (nodeJS .NET packet)
好的,我目前正在使用nodeJS开发UIAutomation,并使用EDGE.js节点模块。一切都很好(哇),但我对代码的可重用性有问题。
我有几个基本相同的函数,它们由超过50%的相同代码组成。当然,我想把这个代码移到一个地方,但问题是这个代码放在了js注释(EDGE的东西)中。
如何重用我的代码以避免EDGE.js中的重复?
是的。。作为最后的手段,我可以把所有东西放在一个c#"程序"中,并根据参数调用不同的c#函数,但可能有一种方法可以保留几个js函数?谢谢
以下是2个函数的示例。我想在每个块的底部只保留不同的"公共异步任务"部分。有什么想法吗?
BTW:任何关于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字符串。下面是一个简单的例子,其中一个函数被分解为两个独立的变量,第1行和第2行。您可以将代码拆分为包含可重用代码的多个函数/变量,然后通过连接各个位来构建代码。
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);
});
相关文章:
- 在侦听特定端口时接收广播的数据包
- Modx数据包管理器ext-all.js错误
- 从Node.js UDP发送大量小数据包;Don’不要全部寄出去
- WebRTC是否为您处理数据包丢失和数据包接收确认等,或者您是否需要为此编写自己的解决方案
- 如果在节点 JS 中落后于 Socket.IO,则丢弃数据包
- 如何将旧版 Dojo 工具包代码转换为 AMD
- Socket.io 数据包在断开连接和重新连接之间丢失
- Nodejs 查询数据和代码组织
- 节点.JS Socket.IO 将数据包发送到特定连接 ID
- 如何使用 c# 将服务器端处理的数据从代码隐藏添加到 asp.net.aspx 页中的 javascript
- 将一个q promise数组拆分为多个数据包,并依次运行这些数据包
- 如何通过本地大于1500字节的UDP数据包发送浮点值并对其进行解析
- Javascript CORS被阻止,但仍在数据包捕获中显示数据
- 将数据从代码隐藏(c#)传递到javascript
- 将数据从代码隐藏发布到另一个网站
- JavaScript:调用已启动类(数据包处理程序)的变量函数
- 如何在EDGE.js(nodeJS.NET数据包)中重用代码
- Windows RT应用程序未接收UDP数据报数据包.微软DatagramSample中我的nor中的以太币
- 您应该在socket.io中手动批处理可扩展应用程序的数据包吗
- 获取周围的JS闭包代码作为文本