[Win] WScript JavaScript操作IE控件 (即命令行生成GUI)

博客首页 » Win WScript JavaScript操作IE控件 (即命令行生成GUI)

发布于 02 Sep 2015 09:35
标签 blog
在Windows中,Script给人的印象是比Linux弱的。但是WScript JavaScript/VBS其实打开了一扇窗。比如使用系统自带的JavaScript,可以操作IE控件,也就是可以在命令Script中行生成GUI。如此一来,Windows自带的工具,完全不借助增加安装的语言系统,也可以实现许多功能。

废话不多说上代码:
ie.js

/// ie.js
/// Frank 2015
 
// common functions
var console = {"log":function(msg) {WScript.Echo(msg);}};
var sleep = function(ms) {WScript.Sleep(ms)};
var quit = function() {WScript.Quit()};
 
// constants
var READYSTATE_UNINITIALIZED  = 0;
var READYSTATE_LOADING        = 1;
var READYSTATE_LOADED         = 2;
var READYSTATE_INTERACTIVE    = 3;
var READYSTATE_COMPLETE       = 4;
 
// event handler, "ie_" prefix is specified by createObject
var ie_NavigateComplete2 = function (disp, url) {
    console.log('Event caught:' + disp + "," + url);
};
 
var ie = WScript.createObject("InternetExplorer.Application", "ie_" );
ie.visible = true;
 
// trigger the event
ie.navigate("about:blank");
while(ie.busy && ie.readyState != READYSTATE_COMPLETE)
    sleep(200);
var url = ie.locationName;
console.log("Navigated to URL: " + url);
 
// create GUI
var document = ie.document;
//var wnd = document.parentWindow;
document.write('<html><body><input type="button" id="sayHello" value="Say Hello"/><input type="button" id="close" value="Close"/></body></html>');
document.all("sayHello").onclick = function () {
  console.log("Hello!");
};
/* the close doesn't work with multi-tab ie
document.all("close").onclick = function () {
  console.log("Closing!");
  ie.quit();
  ie = null;
  quit();
};
*/
console.log("Created GUI via Updating innerHTML, please click 'Say Hello' button.");
 
// wait for clicking
while (true) {
  sleep (200);
}

执行代码

cscript /nologo ie.js

参考资料:
用VBS和WScript来实现
http://blog.csdn.net/xuleilx/article/details/7390501
MSDN中对于Event的讨论
http://blogs.msdn.com/b/oldnewthing/archive/2014/10/20/10565825.aspx
StackOverflow中对于IE控件的讨论
http://stackoverflow.com/questions/16523271/accessing-ie-tabs-once-created


本页面的文字允许在知识共享 署名-相同方式共享 3.0协议和GNU自由文档许可证下修改和再使用,仅有一个特殊要求,请用链接方式注明文章引用出处及作者。请协助维护作者合法权益。


系列文章

文章列表

  • Win WScript JavaScript操作IE控件 (即命令行生成GUI)

这篇文章对你有帮助吗,投个票吧?

rating: 0+x

留下你的评论

Add a New Comment