Is it possible to marry WSH (wscript) with nodejs

一世执手 提交于 2019-12-04 05:10:24

While not actually marrying as the question requires, @o_nix in the comments made the suggestion for https://github.com/idobatter/node-win32ole.

I'd suggest that this module satisfies many issues for people arriving here from Google (as I did). It is also available from npm here: https://www.npmjs.com/package/win32ole

The module also has quite a few examples, such as: https://github.com/idobatter/node-win32ole/blob/dev0.1.3/examples/activex_filesystemobject_sample.js

  var win32ole = require('win32ole');
  . . . 
  var withReadFile = function(filename, callback){
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    var fullpath = fso.GetAbsolutePathName(filename);
    var file = fso.OpenTextFile(fullpath, 1, false); // open to read
    try{
      callback(file);
    }finally{
      file.Close();
    }
  };
  var withEachLine = function(filename, callback){
    withReadFile(filename, function(file){
//    while(file.AtEndOfStream != true) // It works. (without unary operator !)
//    while(!file.AtEndOfStream) // It does not work.
      while(!file.AtEndOfStream._) // *** It works. oops!
        callback(file.ReadLine());
    });
  };
  withEachLine(testfile, function(line){
    console.log(line);
  });

So, to me, this is as good as marrying old WSH scripts as anything. Tweaks will be involved of course, but then it's goodbye WSH.

More specifically, to the question at hand, this is a snippet of a demo IE script: https://github.com/idobatter/node-win32ole/blob/master/examples/ie_sample.js

  var win32ole = require('win32ole');
  . . . 
  var ie = new ActiveXObject('InternetExplorer.Application');
  ie.Visible = true;
  for(var i = 0; i < uris.length; ++i){
    console.log(uris[i]);
    ie.Navigate(uris[i]);
    win32ole.sleep(15000, true, true);
  }
  ie.Quit();

WSH is a different runtime and set of libraries from nodejs. The only simple solution I can think of for your use case is to use child_process to run your WSH scripts and capture the output and parse it.

The other options are:

  • Look at other browser automation modules - selenium is not your only option, there are also headless browsers, which may appease the situation: zombiejs, phantomjs etc
  • Write native bindings to the APIs used by WSH for nodejs
  • Merge the event loops of WSH and nodejs, and expose WSH's API to nodejs: not a good idea for such a narrow use case.

The benefit of firing a child process is that WSH is able to issue HTTP requests. And Node, obviously, can serve HTTP.

One can imagine a Node.js library that would completely proxy ActiveXObject that way and give Node.js all the same powers as WSH.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!