How do I use jQuery in Windows Script Host?

前端 未结 2 519
感动是毒
感动是毒 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:49

    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.

    0 讨论(0)
  • 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());
            }  
        }
    })();
    
    0 讨论(0)
提交回复
热议问题