In php how would I grab all javascript from a page given it's url? Is there a good regular expression to get the src of all javascript script tags or the script inside of them?
-
Javascript code is usually bound to the page they're into (excluding common libraries/frameworks) so they're pretty much useless without the original page and its backend, e.g. you can't grab Facebook's JS and put it in your page to get the chat bar on the bottomstagas– stagas2010-05-31 21:11:13 +00:00Commented May 31, 2010 at 21:11
-
I'm building a jsLint checker, just going to check the js for syntax errorsQuinnBaetz– QuinnBaetz2010-05-31 22:25:20 +00:00Commented May 31, 2010 at 22:25
3 Answers
You can use PHP Simple HTML DOM to traverse the DOM for <script> tags. You can grab inline scripts directly in a string and get the src attribute for externally linked scripts and download them directly with curl or something. It would require some coding, I don't know if there is a 'magic' script that would do that automatically for you.
3 Comments
javascript: URIs and intrinsic event handler attributes (such as onclick).This should place the values of all src attributes contained in script tags into an array in the variable $matches. Check out the documentation for the format of the the array, as there is another parameter that will allow you to modify it.
preg_match_all('/<script[^>]*src=[\'"]([^\'"])+[\'"]/', $string, $matches);
Comments
I would suggest htmlSQL.
http://www.jonasjohn.de/lab/htmlsql.htm
With that you can get the code with tags as well as inline javascript for onclick like events also.