如何在EDGE.js(nodeJS.NET数据包)中重用代码

How to reuse code in EDGE.js (nodeJS .NET packet)

本文关键字:数据包 代码 NET nodeJS EDGE js      更新时间:2023-09-26

好的,我目前正在使用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);
});