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

Vivian River
  • 28,530
  • 54
  • 179
  • 298
  • 4
    http://stackoverflow.com/questions/2941411/executing-javascript-without-a-browser – pistache Dec 03 '12 at 19:01
  • 1
    Wrong tool for the job, say I. If you're into jQuery selectors check out http://code.google.com/p/phpquery/, http://pypi.python.org/pypi/pyquery and others. – soulseekah Dec 03 '12 at 19:02
  • If you use Node you can use Underscore.js, which should fit most if not all of your needs. – Brian Driscoll Dec 03 '12 at 19:02
  • @pistache that does not work for jscript or vbscript – rekire Dec 03 '12 at 19:02
  • 3
    @pistache Not the same thing. Daniel is already running JavaScript outside of the browser, his issue is with jQuery in particular. – Jeremy Dec 03 '12 at 19:02
  • @soulseekah why? If you already know the jQuery API, it can be a good way to do some HTML parsing on the server – David Hellsing Dec 03 '12 at 19:03
  • 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. – Sampson Dec 03 '12 at 19:04
  • 1
    On Node.js you can use [jsdom](https://github.com/tmpvar/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. – Jeremy Dec 03 '12 at 19:04
  • @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. – pistache Dec 03 '12 at 19:05
  • 2
    @JeremyBanks using node.js you can use jquery (there's an npm package for jquery) – Rune FS Dec 03 '12 at 19:06
  • @RuneFS That packages itself includes jsdom as a dependency. :) – Jeremy Dec 03 '12 at 19:07
  • 1
    i like using htmlagilitypack http://htmlagilitypack.codeplex.com/wikipage?title=Examples for things like this. – John Boker Dec 03 '12 at 19:16
  • @JeremyBanks yes but way learn to use jsdom when he wishes to use jquery :) – Rune FS Dec 03 '12 at 19:23
  • 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 things – Dawn Dec 05 '12 at 17:43
  • 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." – TimHayes Jun 27 '13 at 18:58

2 Answers2

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());
        }  
    }
})();
Community
  • 1
  • 1
TimHayes
  • 3,404
  • 18
  • 26
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.

legalize
  • 2,032
  • 17
  • 21