How do I use jQuery in Windows Script Host?

前端 未结 2 566
感动是毒
感动是毒 2021-02-14 12:41

I\'m working on some code that needs to parse numerous files that contain fragments of HTML. It seems that jQuery would be very useful for this, but when I try to load jQuery i

2条回答
  •  不要未来只要你来
    2021-02-14 12:51

    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());
            }  
        }
    })();
    

提交回复
热议问题