My JSONP call works in Chrome's Tampermonkey, but not Firefox's Greasemonkey

414 Views Asked by At

I wrote a userscript to call the API at Forvo.com and display the results on a third-party page, Memrise.com. Accessing Forvo's API requires a private key; I use it while testing, but I've erased it in the publicly-available code. The script works perfectly in Chrome, but not in Firefox.

The code below is the important parts of this user script, with some extra debugging comments added in. When run in Firefox, the log shows that the callback function exists, the ajax call is sent, and the success event is triggered,, but "callback function triggered" never appears and none of the code in the callback function is executed.

What am I doing wrong? Is the jsonp code run in a different context somehow?

document.addEventListener("DOMNodeInserted", function(e) {

    if (e.relatedNode.className != "level-things table") return false;

    $(e.relatedNode).find('th').eq(4).after('<th class="column"><span class="txt">Other Audio</span></th>');
    $(e.relatedNode).find('tr').each(function(){
        var word = $(this).find('td').eq(1).find('.text').text();
        $(this).find('td').eq(4).after('<td><div class="btn-group forvo-check" data-word="' + word + '"><button class="btn btn-mini dropdown-toggle" data-toggle="dropdown" style="overflow:hidden;">Check Forvo<i class="ico ico-s ico-arr-down"></i></button><div class="dropdown-menu audios"><img src="https://d107cgb5lgj7br.cloudfront.net/img/icons/[email protected]" style="width:30px;" /></div></div></td>');
    });

    $('.forvo-check .dropdown-menu').css({'min-width':'30px', padding:'5px'});

    $('.forvo-check').click(function(){
        var languageCode = forvoCodes[ $('.add-level .dropdown-menu a:first').text().trim() ];
        //console.log(languageCode);

        var word = encodeURI( $(this).attr("data-word") );

        console.log("Confirming callback function");
        console.log(showForvoLinks);

        console.log("Sending ajax");
        $.ajax({
            url: "http://apifree.forvo.com/action/word-pronunciations/format/json/word/" + word + "/language/" + languageCode + "/order/rate-desc/limit/4/key/" + forvoApiKey + "/",
            jsonpCallback: "showForvoLinks",
            dataType: "jsonp",
            type: "jsonp",
            cache: false,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                // typically only one of textStatus or errorThrown
                // will have info
                console.log("Error occured textStatus=" + textStatus + " errorThrown=" + errorThrown);
            },
            success: function(data) {
                console.log('API response successful');    
            }
        });
    });

}, true);  //end of addEventListener




showForvoLinks = function(data){
    console.log("callback function triggered")
    popupHTML = '';
    for (i in data.items) popupHTML += '<p><a class="audio-player audio-player-hover" href="' + decodeURI(data.items[i].pathmp3) + '"></a></p>';
    if (popupHTML=='') popupHTML = '<a href="http://www.forvo.com/word/' + encodeURI( $('.forvo-check.open').attr("data-word") ) + '/" target="_blank">nothing</a>';
    $('.forvo-check.open .dropdown-menu').html(popupHTML);
}
0

There are 0 best solutions below