0

I have done this a million times (and even in the same project), but somehow the following code does not work:

inside content-script:

function DrawTrophies()
{
    $(document).ready(function()
    {
        if (window.location.pathname.indexOf("/about") > 0)
        {
                if ($('#trophyDisplay').length > 0) {
                    return;
                }
                var userId=GetCurrentUserId();
                chrome.runtime.sendMessage(
                {
                    Action: "getTrophiesForUser",
                    UserId: userId
                }, function(response)
                {
                    console.log("YAY!");
                });
           // }
       }});}

inside Background-script:

// Trophies
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.Action==="getTrophiesForUser") {
        var file="http://www.example.com/php/loadtrophies.php?version=1.0&userId="+request.UserId;
        $.getJSON(file, function(trophies) {
            sendResponse({Result: JSON.stringify(trophies)});
        }); 
    }
});

The code is run and I even can watch the Value of JSON.stringify(trophies) from the background script, but the console.log("YAY!") never is called

No error messages in the console at all. What makes this a bit different from the other 20 places in code where this just works:

  1. I get external JSON-Content from Background Script.
  2. The page where the script is running is on https. Because the content I need is from a http - site this was the main reason to put the code in the background page at all, cause the content-script would not allow this

May any of these be the source of the problem?

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • Just for others who might end up here: The answer in the link did not work for me. But calling the Url Synchronously did. – Ole Albers Oct 31 '14 at 19:40
  • Then you did not understand the answer. It's perfectly okay to call it asynchronously, but then you have to `return true` from the handler. – Xan Nov 01 '14 at 11:31
  • I did. But it did not change a thing – Ole Albers Nov 02 '14 at 13:41
  • Where did you put it in your code? It must be after `$.getJSON();` call altogether. – Xan Nov 02 '14 at 13:43

0 Answers0