<commit_before>define(
[
    'jQuery',
    'underscore',
    'color-thief'
],
function($, _, ColorThief) { 'use strict';

var myUpdateBackgroundFactory = function(appSettings, $http) {
    var thief = new ColorThief();

    var updateThumbnail = function(element, url){
        var background = 'url(' + url + ')';

        var thumbnail = $('.thumbnail', element.parent().parent().parent());
        thumbnail.css('background', background);
    };

    var updateThumbnailColor = function(element){
        var color = null;
        try {
            color = thief.getColor(element.get(0));
        } catch(e) {}

        var background = color ? 'rgb(' + color.join(',') + ')' : 'white';

        var thumbnail = $('.thumbnail-loading', element.parent().parent().parent());
        thumbnail
          .removeClass('thumbnail-loading')
          .addClass('thumbnail')
          .css('background', background);
    };

    return function(scope, element, attrs) {
        scope.$watch(attrs.dLoad, function(value) {
            element.on('load', function() {       
                
                updateThumbnailColor(element);

                if (appSettings.showThumbnails) {
          
                    $http({
                        url: 'http://api.page2images.com/restfullink?p2i_url=' +
                        encodeURIComponent(scope.bookmark.url) +
                        '&p2i_device=6&p2i_screen=1024x768&p2i_imageformat=jpg&p2i_wait=0&p2i_key=<KEY>',
                        method: "POST",
                        headers: {
                            'Content-Type': 'application/json; charset=utf-8'
                        }
                    }).success(function(responseData) {

                        if(!_.isUndefined(responseData.image_url)){
                            updateThumbnail(element, responseData.image_url);
                        }
                    });
                }
            });
        });
    };
};

return [
    'appSettings',<commit_msg>Handle processing responses from page2images in right way<commit_after>define(
[
    'jQuery',
    'underscore',
    'color-thief'
],
function($, _, ColorThief) { 'use strict';

var myUpdateBackgroundFactory = function(appSettings, $http) {
    var thief = new ColorThief();

    var updateThumbnail = function(element, url){
        var background = 'url(' + url + ')';

        var thumbnail = $('.thumbnail', element.parent().parent().parent());
        thumbnail.css('background', background);
    };

    var updateThumbnailColor = function(element){
        var color = null;
        try {
            color = thief.getColor(element.get(0));
        } catch(e) {}

        var background = color ? 'rgb(' + color.join(',') + ')' : 'white';

        var thumbnail = $('.thumbnail-loading', element.parent().parent().parent());
        thumbnail
          .removeClass('thumbnail-loading')
          .addClass('thumbnail')
          .css('background', background);
    };

    var requestThumbnail = function(element, url, tries) {

        return $http({
            url: 'http://api.page2images.com/restfullink?p2i_url=' +
            encodeURIComponent(url) +
            '&p2i_device=6&p2i_size=400x150&p2i_screen=1024x768&p2i_imageformat=jpg&p2i_wait=0&p2i_key=<KEY>',
            method: "POST",
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            }
        }).success(function(responseData) {
            if (responseData.status === "processing") {
                // Server processing results, let's try later in 3 seconds,
                // but not more than 40 times (120 seconds)
                if (tries <= 40) {
                    setTimeout(function() {
                        requestThumbnail(element, url, ++tries);
                    }, ((parseInt(responseData.estimated_need_time) || 2) + 1) * 1000);
                }
            } else if(responseData.status === "finished" && responseData.image_url){
                // Finished, let's update thumbnail
                updateThumbnail(element, responseData.image_url);
            }
        });
    };

    return function(scope, element, attrs) {
        scope.$watch(attrs.dLoad, function(value) {
            element.on('load', function() {       

                updateThumbnailColor(element);

                if (appSettings.showThumbnails) {
                    requestThumbnail(element, scope.bookmark.url, 0);
                }
            });
        });
    };
};

return [
    'appSettings',
{'fraction_non_alphanumeric': 0.13756613756613756, 'fraction_numerical': 0.01300705467372134, 'mean_word_length': 1.8215174129353233, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2470390', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1231, 'n_words': 219}