d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What's The Best Way To Get The Contents Of A > Declared Variable In A Javascript File?
Add Reply New Topic New Poll
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Feb 18 2016 08:49pm
Hey. Ok, so, I http-get'd a javascript file from a website.

It's got all sorts of functions in it, and one of those functions has a dictionary variable with lots of valuable information.

e.g.

Code
file.js

...

function (..., ..., ...) {

....

var dictionary = {

"item1" : "123123",
"item2" : "123123123",
...
item11: "123",
item12: "456",
item13: "789",
...

}

...

}


What would ya'll say is the best way to get the contents of that dictionary variable into my own variable?

I really don't want to just parse it and finangle it into a json and then into a variable, but I will if I have to...
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 18 2016 08:57pm
if it's a one-time thing, just copy-paste it. if the data is static in the file (but might change daily), it might be simpler for you to regex it out as a string then use JSON.parse since you dont need any other functions to execute.

if it's a complex script, you can always append your own function to return it and eval it. but eval'ing someone else's unknown code doesn't sound like a great idea.

This post was edited by carteblanche on Feb 18 2016 09:11pm
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
Feb 18 2016 09:48pm
Quote (carteblanche @ Feb 18 2016 08:57pm)
if it's a one-time thing, just copy-paste it. if the data is static in the file (but might change daily), it might be simpler for you to regex it out as a string then use JSON.parse since you dont need any other functions to execute.

if it's a complex script, you can always append your own function to return it and eval it. but eval'ing someone else's unknown code doesn't sound like a great idea.


Yeah, we actually don't have access to edit it, unfortunately. Otherwise I totally would just throw my own function in there.

Ok, I'll just regex it. Thanks!
Member
Posts: 161,550
Joined: Oct 18 2006
Gold: 4.03
Warn: 20%
Feb 21 2016 06:33am
var yourvar = dictionary

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll