Extract URLS from code statements in JS files

Extract URLS from code statements in JS files

How large is your file / how many do you have?

A very simple solution I use for this type of thing is excel. You could just copy / paste the file (as long as there’s only a couple) into excel, then sort the column. You should find all your vars in one place. Copy/paste those into a new sheet, and split the column on ‘”‘. This should give you a column of your urls.

Of course, this isn’t practical for many files — a script would work better there.

EXAMPLE

I find this is particularly useful for parsing json by hand. Here’s an example of how I typically use this. Here’s a snippit from a json array:

{‘english’ => ‘the animals, ‘spanish’ => ‘los animales’, ‘image_url’ => ‘http://www.resourcesgraphics.com/images/Animals-Set-icons.jpg}, {‘english’ => ‘the bear, ‘spanish’ => ‘ el oso , ‘image_url’ => ‘http://images.nationalgeographic.com/wpf/media-live/photos/000/005/cache/grizzly-bear_566_600x450.jpg}, {‘english’ => ‘the beaver, ‘spanish’ => ‘el castor, ‘image_url’ => ‘http://images.nationalgeographic.com/wpf/media-live/photos/000/004/cache/beaver_457_600x450.jpg}, etc… (I have ~2000 entries in this file.)

To get all the URLs out, I just copy/paste the entire file into excel, and use Data>Text to Columns>Delimited. If you split on a quote, it’ll pull out the URLs into their own column.

This also goes the other way. (I do this much more often.) If you wanted to make the table above, you could use this process in reverse. To quickly fill columns, I just make a first column like this:

{‘*english*’ => ‘*the animals*, ‘*spanish*’ => ‘*los animales*’, ‘*image_url*’ => ‘*http://www.resourcesgraphics.com/images/Animals-Set-icons.jpg*},

(* = column division)

So, now we have column1 = “{‘”, column2 = “english”, column3 = “‘ => ‘”, etc… columns 1, 3, 5, etc are fillers, and the data goes in the middle. I’d then fill in all the data, and copy in the fillers. A quick way to fill in filler data is to double click on the bottom right handle of the top filler cell. This should duplicate the data down the column to the end of your data.

After you’ve made your spreadsheet, save it. You can use it again later. When you need your data, just copy all, and paste into text editor.

There’s usualy a bunch of pesky tabs left over from the columns, but those can be removed with a simple find/replace.

Hope that helps — let me know if you’d like any more info! – L

Source

Leave a Reply