How do I use jQuery in Windows Script Host?

穿精又带淫゛_ 提交于 2019-12-03 12:03:49
TimHayes

Following along with your code, you could create an instance of IE using Windows Script Host, load your html file in to the instance, append jQuery dynamically to the loaded page, then script from that.

This works in IE8 with XP, but I'm aware of some security issues in Windows 7/IE9. IF you run into problems you could try lowering your security settings.

var fileIo, here, ie;

fileIo = new ActiveXObject('Scripting.FileSystemObject');
here = unescape(fileIo.GetParentFolderName(WScript.ScriptFullName) + "\\");
ie = new ActiveXObject("InternetExplorer.Application");
ie.visible = true

function loadDoc(src) {
  var head, script;
  ie.Navigate(src);
  while(ie.busy){
    WScript.sleep(100);
  }
  head =  ie.document.getElementsByTagName("head")[0];    
  script = ie.document.createElement('script');
    script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
  head.appendChild(script);
  return ie.document.parentWindow;
}

(function() {
    var files, thisFile, win; 
    for (files = new Enumerator(fileIo.GetFolder(here).files); !files.atEnd(); files.moveNext()) {
        thisFile = files.item();         
        if(fileIo.GetExtensionName(thisFile)=="htm") {
          win = loadDoc(thisFile);
          // your jQuery reference = win.$
          WScript.echo(thisFile + ": " + win.$('input#txtFoo').val());
        }  
    }
})();

This is pretty easy to do in Node.js with the cheerio package. You can read in arbitrary HTML from whatever source you want, parse it with cheerio and then access the parsed elements using jQuery style selectors.

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