var users_data = {};


chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.type == "getInfo") {
            if (users_data[request.twitterName]) {
                sendResponse(users_data[request.twitterName]);
            } else {
                $.getJSON({
                    url: "https://twitter.com/i/profiles/popup?screen_name=" + request.twitterName + "&wants_hovercard=true&_=" + new Date().getTime(),
                    success: function(data) {
                        var results = [];
                        var cards = $(data.html).find(".ProfileCardStats span.ProfileCardStats-statValue");
                        console.log(cards);
                        for (var card of cards) {
                            results.push($(card).attr("data-count"));
                        }
                        users_data[request.twitterName] = results;
                        users_data[request.twitterName][3] = request.twitterName;
                        console.log(users_data[request.twitterName]);
                        sendResponse(users_data[request.twitterName]);
                    }
                });
            }
        }
        return true;
    });