Parsing JavaScript Using Ruby Code

How to parse JavaScript using Nokogiri and Ruby

If I read you correctly you're trying to parse the JavaScript and get a Ruby array with your image URLs yes?

Nokogiri only parses HTML/XML so you're going to need a different library; A cursory search turns up the RKelly library which has a parse function that takes a JavaScript string and returns a parse tree.

Once you have a parse tree you're going to need to traverse it and find the nodes of interest by name (e.g. _arPic) then get the string content on the other side of the assignment.

Alternatively, if it doesn't have to be too robust (and it wouldn't be) you can just use a regex to search the JavaScript if possible:

/^\s*_arPic\[\d\] = "(.+)";$/

might be a good starter regex.

How to parse a JavaScript-based page

You need to use a Watir gem for this one since it is loaded through ajax.
And also, it seems they have an API, you may also want to take look at this.

How can I use JavaScript to parse Ruby JSON string

Why don't you do:

data = <%= @chromosomes.html_safe %>;

Sidenote:

I hope you do something like:

@chromosomes = [{ name: "YHet", organism_id: "foo" }].to_json

What tools exist for parsing Javascript and reading the results in Javascript or Ruby?

Check out UglifyJS: https://github.com/mishoo/UglifyJS

Parse ruby object in JavaScript (Rails)

JSON is valid Javascript right out of the box, so why not just do:

var js_obj = <%= @rails_obj.to_json %>;

Markdown parser in Javascript

Showdown? -

The http://attacklab.net/showdown/ site is defunct.

Showdown is now available on git, here: https://github.com/coreyti/showdown



Related Topics



Leave a reply



Submit