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

时间:2015-11-24 15:35:17

标签: javascript c# node.js

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

1 个答案:

答案 0 :(得分:1)

您可以使用与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);
});