How do I use jQuery in Windows Script Host?
Asked Answered
S

2

10

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

Sweetmeat answered 3/12, 2012 at 19:0 Comment(15)
#2941911Courbevoie
Wrong tool for the job, say I. If you're into jQuery selectors check out code.google.com/p/phpquery, pypi.python.org/pypi/pyquery and others.Saltsman
If you use Node you can use Underscore.js, which should fit most if not all of your needs.Selfgoverned
@Courbevoie that does not work for jscript or vbscriptAuthoritarian
@Courbevoie Not the same thing. Daniel is already running JavaScript outside of the browser, his issue is with jQuery in particular.Mattland
@Saltsman why? If you already know the jQuery API, it can be a good way to do some HTML parsing on the serverAverett
Why won't vanilla JavaScript work for you? jQuery is largely helpful for animations, and normalizing cross-browser differences in APIs. If you're not dealing with browsers, much of jQuery's reason to exist has been done away with.Cornel
On Node.js you can use jsdom, which implements the DOM API as jQuery requires. However, I don't know anything about WScript or CScript. Are you tied them to them in particular? If so, please edit the title to reflect that, because "use jsdom!" might not help you but it would be a valid answer to the question as currently written.Mattland
@Jeremy Banks , yes, sorry, you're right. However, most of the JS interpreters listed in that link can have a knowledge of the 'window' object, which may help the OP.Courbevoie
@JeremyBanks using node.js you can use jquery (there's an npm package for jquery)Obligate
@RuneFS That packages itself includes jsdom as a dependency. :)Mattland
i like using htmlagilitypack htmlagilitypack.codeplex.com/wikipage?title=Examples for things like this.Nonparticipation
@JeremyBanks yes but way learn to use jsdom when he wishes to use jquery :)Obligate
importantly, node also has file i/o for running through all those files. Otherwise, you're just dealing with a big string and you'll need to use regular expressions to find the right thingsImmortality
I know this is an old thread, but I think PhantomJS would also be helpful for this purpose. "PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG."Torrential
T
3

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());
        }  
    }
})();
Torrential answered 5/12, 2012 at 18:57 Comment(0)
S
0

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.

Spurgeon answered 15/9, 2015 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.