user1275375
user1275375

Reputation: 1391

Writing a Parser for javascript code

I want to extract javasscript code and find out if there are any dynamic tag creations like document.createElement('script'); I have tried to do this with Regular expressions but using regular expressions restricts me to get only some formats so i thought of writing a javascript parser which extracts all the keywords, strings and functions from the javascript code.

Upvotes: 1

Views: 250

Answers (3)

Farid Nouri Neshat
Farid Nouri Neshat

Reputation: 30430

Well the first rule is never use regex for big things like this, or DOM, or ... . You have to parse it by tokens. The good news is that you don't have to write your own. There are a few JS to JS parsers.

They may be a bit hard to work with it. But well better to work with them. There are other projects that are uses these such as burrito or code surgeon. So you can have a look at the source code and see how they uses them.

But there is bad news too, which people can still outsmart other people, let alone the parsers and the code they write. At least you need to evaluate the code with some execution time variables and see if it tries to access the DOM or not.

Upvotes: 0

Adam Bergmark
Adam Bergmark

Reputation: 7536

In general there is no way to know if a given line of code will ever run, you would need to solve the halting problem. If you restrict your analysis to just finding occurances of a function call you don't make much progress. Naive methods will still be easy to trick, if you just regex match for document.createElement, you would not be able to match something as simple as document["create" + "Element"]. In general you would need to not only parse the code but evaluate it as well to get around this. And to be sure that you can evaluate the code you would again need to solve the halting problem.

Upvotes: 2

Andrija
Andrija

Reputation: 14483

Maybe you should try using Burrito

Upvotes: 0

Related Questions