How do I use jQuery in Windows Script Host?

耗尽温柔 提交于 2019-12-04 18:33:51

问题


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 into something like WScript or CScript, it throws an error because of jQuery's many references to the window object.

What practical way is there to use jQuery in code that runs without a browser?

Update: In response to the comments, I have successfully written JavaScript code to read the contents of files using new ActiveXObject('Scripting.FileSystemObject');. I know that ActiveX is evil, but this is just an internal project to get some data out of some files that contain HTML fragments and into a proper database.

Another Update: My code so far looks about like this:

var fileIo, here;

fileIo = new ActiveXObject('Scripting.FileSystemObject');
here = unescape(fileIo.GetParentFolderName(WScript.ScriptFullName) + "\\");

(function() {
    var files, thisFile, thisFileName, thisFileText;

    for (files = new Enumerator(fileIo.GetFolder(here).files); !files.atEnd(); files.moveNext()) {
        thisFileName = files.item().Name;
        thisFile = fileIo.OpenTextFile(here + thisFileName);
        thisFileText = thisFile.ReadAll();        

        // I want to do something like this:
        s = $(thisFileText).find('input#txtFoo').val();    
    }

})();

Update: I posted this question on the jQuery forums as well: http://forum.jquery.com/topic/how-to-use-jquery-without-a-browser#14737000003719577


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/13689871/how-do-i-use-jquery-in-windows-script-host

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