Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save inothilmi/4fa5d26c80b199826bf2c48d16a30bf7 to your computer and use it in GitHub Desktop.

Select an option

Save inothilmi/4fa5d26c80b199826bf2c48d16a30bf7 to your computer and use it in GitHub Desktop.

Revisions

  1. @SelmanH SelmanH revised this gist Oct 14, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,9 @@ function getMean(word){
    res = JSON.parse( massaged );
    } catch( e ) {
    res = new Function( 'return ' + massaged )();
    $('#result').val(res);
    }

    $('#result').val(res);
    $('#result').val(process(res));
    }
    }

  2. @SelmanH SelmanH revised this gist Oct 14, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,10 @@ function getMean(word){
    res = JSON.parse( massaged );
    } catch( e ) {
    res = new Function( 'return ' + massaged )();
    return res;
    $('#result').val(res);
    }

    return res;
    $('#result').val(res);
    }
    }

  3. @SelmanH SelmanH revised this gist Oct 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ function getMean(word){

    ajax = new XMLHttpRequest();

    // result
    // result callback
    ajax.onreadystatechange = function(){
    if(ajax.readyState==4 && ajax.status==200){
    var massaged = ajax.responseText.replace(/^[^(]+\(|[^}]+$/g, ''), res;
  4. @SelmanH SelmanH revised this gist Oct 14, 2013. 1 changed file with 9 additions and 12 deletions.
    21 changes: 9 additions & 12 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,25 @@
    function getMean(word){
    if(word==""|| word=="Enter a word")
    return;
    return;

    ajax = new XMLHttpRequest();

    // result
    // result
    ajax.onreadystatechange = function(){
    if(ajax.readyState==4 && ajax.status==200){
    var massaged = ajax.responseText.replace(/^[^(]+\(|[^}]+$/g, ''), res;
    try {
    res = JSON.parse( massaged );
    } catch( e ) {
    res = new Function( 'return ' + massaged )();
    try {
    res = JSON.parse( massaged );
    } catch( e ) {
    res = new Function( 'return ' + massaged )();
    return res;
    }

    }
    return res;
    }
    else{
    document.getElementsByTagName("div")[1].innerHTML="<h3 class=\"loading\">Loading</h3>";
    }
    }

    // make the call
    // make the call
    var encoded = encodeURIComponent("http://www.google.com/dictionary/json?callback=process&sl=en&tl=en&restrict=pr,de&client=te&q=" + word);
    ajax.open("GET","proxy.php?url=" + encoded,true);
    ajax.send(null);
  5. @SelmanH SelmanH created this gist Oct 14, 2013.
    79 changes: 79 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    function getMean(word){
    if(word==""|| word=="Enter a word")
    return;

    ajax = new XMLHttpRequest();

    // result
    ajax.onreadystatechange = function(){
    if(ajax.readyState==4 && ajax.status==200){
    var massaged = ajax.responseText.replace(/^[^(]+\(|[^}]+$/g, ''), res;
    try {
    res = JSON.parse( massaged );
    } catch( e ) {
    res = new Function( 'return ' + massaged )();
    return res;
    }

    return res;
    }
    else{
    document.getElementsByTagName("div")[1].innerHTML="<h3 class=\"loading\">Loading</h3>";
    }
    }

    // make the call
    var encoded = encodeURIComponent("http://www.google.com/dictionary/json?callback=process&sl=en&tl=en&restrict=pr,de&client=te&q=" + word);
    ajax.open("GET","proxy.php?url=" + encoded,true);
    ajax.send(null);
    }

    // iterates through json result object, returns word definition object
    function process(json_obj){
    var result = {
    sound : null,
    pronunciation : null,
    primary_means : null
    };

    for (var prop in json_obj){
    if(prop=="primaries"){
    var stuff = json_obj["primaries"][0];

    // get pronunciation and sound
    var isa = "";
    var sound;
    var pro = stuff["terms"];
    for(var i=0;i<pro.length;i++){
    if(pro[i]["type"]=="phonetic"){
    isa+=pro[i]["text"];
    isa+=" ";
    }
    else if(pro[i]["type"]=="sound"){
    sound = pro[i]["text"];
    }
    }
    result.sound = sound;
    result.pronunciation = isa;

    // get meaning array
    var primary_mean = stuff["entries"];
    var primary_mean_list = [];
    var k=0;
    for(var i=0;i<primary_mean.length;i++){
    if(primary_mean[i]["type"]=="meaning"){
    primary_mean_list[k]=primary_mean[i]["terms"][0]["text"];
    k++;
    }
    }
    result.primary_means = primary_mean_list;
    }
    else
    continue;
    }

    return result;
    }

    var result = getMean('Philodendron');
    console.log(result);