4
\$\begingroup\$

Upon coming face to face with the mighty cross-origin request block, and needing to get a JSON file from an external source, I decided to use the Yahoo Query Language in order to achieve this.

I'm starting to wonder however whether this code is really the most efficient way of doing it, considering the source can be in JSON, XML and Steams own language.

function getID64(name) {
    var Site = 'https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=' + key + '&vanityurl=' + name;
    //console.debug("getID64 - " + Site);
    var yql = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from json where url="' + Site + '"') + '&format=json&callback=?';
    //console.debug("getID64 - " + yql);
    $.getJSON(yql, function(data) {
        app.id64 = data.query.results.response.steamid;
        //console.debug("getID64 - " + app.id64);
        //From here onwards it's just changing some elements on the webpage and starting the next function
        document.getElementById("Steam64ID2").innerHTML += " " + app.id64;
        document.getElementById("Steam64ID").value += "" + app.id64;
        document.getElementById("SteamID2").innerHTML += " " + document.getElementById("SteamID").value;
        getFriendList(document.getElementById('Steam64ID').value);
    });
}

Considering for the first half I am only trying to get and parse the file, is there an easier way of doing this?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Upon coming face to face with the mighty cross-origin request block, and needing to get a JSON file from an external source, I decided to use the Yahoo Query Language in order to achieve this.

Using YQL just to get a JSON and parse it sounds like a dirty workaround. I don't know the right answer about that part. You might be better off asking on stackoverflow.com instead, to overcome your original problem without resorting to such workaround.

About the code posted, these are only minor nitpicks:

  • Where does key come from? Perhaps it should be a method parameter
  • User lowercase for variable names (site (or url) instead of Site)
  • You look up the 'Steam64ID' element twice. It would be better to do it once and store in a variable to reuse, for example:

    var steam64ID = document.getElementById("Steam64ID").value += "" + app.id64;
    getFriendList(steam64ID);
    
  • The function is called getID64 which sounds like a getter, so it should return something, but it actually doesn't.

\$\endgroup\$
2
  • \$\begingroup\$ "key" is a unique identifier for my account on the SteamAPI, I did not want to include it in this extract due to the fact that it is meant to be kept private. Thanks for the tips on using lowercase variable names and that I called "Steam64ID" twice, sped up website by a surprising amount. "getID64" does not actually return anything, it outputs directly to the HTML. I'll ask on stackoverflow.com to overcome getting the JSON. \$\endgroup\$ Sep 29, 2014 at 10:57
  • 1
    \$\begingroup\$ It's good you didn't include your key. What I meant was that maybe it should be a method parameter or you should indicate somehow where it comes from. In any case never mind \$\endgroup\$
    – janos
    Sep 29, 2014 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.