javascript 如何使用 node.js 创建 ActiveXObject?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16408093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:32:09  来源:igfitidea点击:

how to create a ActiveXObject with node.js?

javascriptnode.js

提问by cybertextron

I'm a totally newbie in node.js. I'm trying to implement a browser performance tool using node.js, so I have the following piece of code:

我是node.js. 我正在尝试使用 node.js 实现浏览器性能工具,所以我有以下代码:

 for(var j=0; j < 14; j++) {
        // Create a new instance of HttpWatch in Firefox
        var control = new ActiveXObject('HttpWatch.Controller');
        var plugin = control.Firefox.New();
        // Start Recording HTTP traffic
        plugin.Log.EnableFilter(false);
        // Clear Cache and cookier before each test
        plugin.ClearCache();
        plugin.ClearAllCookies();   
        plugin.ClearSessionCookies();
        plugin.Record();
        // Goto to the URL and wait for the page to be loaded
        plugin.GotoURL(url);
        control.Wait(plugin, -1);
        // Stop recording HTTP
        plugin.Stop();
        if ( plugin.Log.Pages.Count != 0 )
        {           
           // Display summary statistics for page
           var summary = plugin.Log.Pages(0).Entries.Summary;
           //WScript.Echo( "Iteration number " + j + "for" + url + "   Total time to load page in (secs): " + summary.Time);
           cache[i].value.push(summary.Time);
        }
        // Close down Firefox
        plugin.CloseBrowser();
      }

I'm using httpwatchto measure the performance values, which are going to be stored in a MySQLdatabase. However, when I run:

我正在使用httpwatch来测量性能值,这些值将存储在MySQL数据库中。但是,当我运行时:

node test.js

I get:

我得到:

C:\xampp\htdocs\test\browser-perf>node test.js

C:\xampp\htdocs\test\browser-perf\test.js:37
                var control = new ActiveXObject('HttpWatch.Controller');
                                  ^
ReferenceError: ActiveXObject is not defined
    at Object.<anonymous> (C:\xampp\htdocs\test\browser-perf\test.
js:37:21)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

How can I create a similar object as ActiveXObjectin node.jsand obtain the same desired results?

我怎样才能创建一个类似的对象ActiveXObjectnode.js,并获得同样的预期效果?

回答by doctorlai

https://npmjs.org/package/win32ole

https://npmjs.org/package/win32ole

try the win32ole package, to install, open the node.js cmd, and type the following to install the package.

试试win32ole包,安装,打开node.js cmd,输入以下命令安装包。

npm install win32ole

Example usage:

用法示例:

var win32ole = require('win32ole');
var xl = win32ole.client.Dispatch('Excel.Application');
xl.Visible = true;

Please also see this post: Using COM object in NodeJS

另请参阅这篇文章:在 NodeJS 中使用 COM 对象

回答by Dai

I'm not a node.js user, so I can't comment on node.js specifically, but ActiveXObjectis a feature of the Microsoft Active Scripting JScript engine, as well as the new Chakra engine in IE9, so it is not available in other platforms because it's very Windows-specific.

我不是node.js用户,所以不能具体评论node.js,但它ActiveXObject是Microsoft Active Scripting JScript引擎的一个特性,以及IE9中新的Chakra引擎,所以它在其他平台,因为它非常特定于 Windows。

回答by isomorphismes

As in https://github.com/mynetx/codebird-js/issues/13, you can emulate it using xhr2: https://npmjs.org/package/xhr2.

正如在https://github.com/mynetx/codebird-js/issues/13 中一样,您可以使用xhr2https: //npmjs.org/package/xhr2模拟它。

npm install xhr2

I'm not sure if that will work for your browser performance tool but at least for someone else googling on the ActiveXObjecterror it should work.

我不确定这是否适用于您的浏览器性能工具,但至少对于谷歌搜索ActiveXObject它应该工作的错误的其他人。