1

This person has a very simple translation script working on the Wiktionary API.

How would I go about modifying this to return some dictionary definitions for English words?

http://jsfiddle.net/karlb/PxfrJ/11/

function show_result(data) {
    $("#result ul").text('');
    $.each(data.query.pages, function(page_id, page) {
        if (page.iwlinks === undefined) {
            $("#result ul").append('no results');
            return false; // break
        }
        $.each(page.iwlinks, function(i, el) {
            var trans = el['*'];
            trans = trans.replace('Special:Search/', '').replace('_', ' ');
            $("#result ul").append('<li>' + trans + '</li>');
        });
    });
    $("#result").fadeIn('fast');
}

function translate() {
    $("#result").fadeOut('fast');
    $.ajax({
        url: 'http://' + $('#from').val() + '.wiktionary.org/w/api.php',
        data: {
            action: 'query',
            prop: 'iwlinks',
            format: 'json',
            iwlimit: 30,
            iwprefix: $('#to').val(),
            titles: $('#word').val()
        },
        dataType: 'jsonp',
        success: show_result
    });
    return false;
}

$('form').submit(translate);
$('#word').focus();
Nemo
  • 2,441
  • 2
  • 29
  • 63
rolandnsharp
  • 281
  • 1
  • 4
  • 13
  • This is at least the third question of this week that involves the use of JSONP in Chrome extensions... Please do some prior research. – Rob W Aug 17 '13 at 13:24
  • @RobW What about this version that is not using jasonp https://gist.github.com/nichtich/674522 it still does not work in my chrome extension. – rolandnsharp Aug 17 '13 at 13:59
  • That's because of the CSP, see http://stackoverflow.com/a/17612988/938089. – Rob W Aug 17 '13 at 14:00
  • @RobW I'm not using inline javascript (I separated in in my extension) and I have this in my manifest file: "content_security_policy": "script-src 'self' http://en.wiktionary.org; object-src 'self'",. Also have these permissions "http://*/*", "https://*/*", – rolandnsharp Aug 17 '13 at 15:00
  • You can't whitelist a remote non-https resource. Just get rid of JSONP and use plain AJAX. – Rob W Aug 17 '13 at 15:01
  • https://gist.github.com/nichtich/674522 this is what i am using now. except I separated it. I don't think there is any JSONP there, just JSON? What's the difference? – rolandnsharp Aug 17 '13 at 15:38

0 Answers0