<commit_before>var parser = new DOMParser();
var my_games_list = {};
var my_wishlist = {};
var my_steam_name = null;
var table = null;
var page = 1;
var shouldScroll = true;

function getGames(callback, forceupdate) {
    chrome.extension.sendRequest({method: (forceupdate === true) ? "updateGames" : "getGames"}, function(settings) {
        games = settings.games;
        if ( games === undefined ) {
            games = {};
        }

        for (var i = 0; i < games.length; i++) {
            my_games_list[games[i].id] = true;
        }

        if (typeof callback == "function") {
            callback();
        }
    });
}

function getWishlist(callback, forceupdate) {
    chrome.extension.sendRequest({method: (forceupdate === true) ? "updateWishlist" : "getWishlist"}, function(settings) {
        wishlist = settings.wishlist;

        if (wishlist === undefined) {
            wishlist = {};
        }

        for (var i = 0; i < wishlist.length; i++) {
            my_wishlist[wishlist[i].id] = true;
        }

        if (typeof callback == "function") {
            callback();
        }
    });
}

function getUser(callback) {
    chrome.extension.sendRequest({method: "getSteamName"}, function(settings) {
        my_steam_name = settings.user;
        if (typeof callback == "function") {
            callback();
        }
    });
}

getUser(function() {
    getWishlist(function() {
        getGames(main);
    });
});

$("head").append('<link href="//netdna.bootstrapcdn.com/font-awesome/3.0/css/font-awesome.css" rel="stylesheet">');

function rearrangeTable(table, elements, desired_count, detach) {
    elements.each(function() {
        if (detach) {
            $(this).detach();
        }
        var row = $("> tbody > tr", table).filter(function() {
            return $(this).children("td").length < desired_count;
        }).first();

        if (row.length === 0) {
            row = $("<tr>").appendTo(table);
        }
        row.append($(this));
    });
}


function getLinks(doc) {
    return $('td:not(.tabella-features1, tabella-features2) > a[href^="/GA/"]:has(img)', doc);
}

function getIdFromLink(elem) {
    td = $(elem).closest("td");
    var match = (/(?:apps|subs)\/(\d+)/).exec(td.attr("style"));
    return match[1];
}


function hasLinks(doc) {
    return getLinks(doc).length > 0;
}

function cleanUp(doc) {
    var links = getLinks(doc);

    links.each(function(){
        if (getIdFromLink(this) in my_games_list) {
            $(this).closest("td").remove();
        }
    });
}

function lineUp(doc) {
    var links = getLinks(doc);
    rearrangeTable(table, links.closest("td"), 4, true);
}

function showWishlist(doc) {
    var hilite = chrome.extension.getURL("wishlist.png");
    var links = getLinks(doc);

    links.each(function (){
        if (getIdFromLink(this) in my_wishlist) {
            $("img", this).attr("src", hilite);
        }
    });
}

function quickView(content) {
    if ($("div.quickview.overlay").length === 0) {
        var div = $('<div>').appendTo("body").addClass("quickview overlay")
        .css({
            opacity: 0.4,
            backgroundColor: "black",
            position: "fixed",
            top: 0,
            left: 0,
            width: "100%",
            height: "100%",
            zIndex: 25
        }).click(function() {
            $(".quickview").remove();
        });
    }
    
    if ($("div.quickview.waiting").length === 0) {
        $("<div>").addClass("quickview waiting tabella-forum").appendTo("body").css({
            opacity: 1,
            position: "fixed",
            top: "50%",
            left: "50%",
            width: "297px",
            height: "136px",
            marginLeft: "-149px",
            marginTop: "-68px",
            textAlign: "center",
            zIndex: 100
        }).append($('<i class="icon-spinner icon-spin icon-4x"></i>')).hide();
    }

    if ($("div.quickview.content").length === 0) {
        $("<div>").addClass("quickview content").appendTo("body").css({
            opacity: 1,
            position: "fixed",
            top: "50%",
            left: "50%",
            textAlign: "center",
            zIndex: 100
        }).hide();
    }

    $("div.quickview.overlay").show();
    if (content !== undefined) {
        handleSteamLinks(content);
        $("div.quickview.waiting").hide();
        $("div.quickview.content").show().empty().append(content).css({
            width: $(content).width(),
            height: $(content).height(),
            marginLeft: -($(content).width() / 2),
            marginTop: -($(content).height() / 2)
        });
    } else {
        $("div.quickview.waiting").show();
        $("div.quickview.content").hide();
    }
}

function handleSteamLinks(doc) {
    $('a[href^="http://store.steampowered.com/app/"]', doc).attr("target", "_blank");
}

function handleGiveawayForm(event) {
    $(this).find("button").attr("disabled", "disabled");
    $("#resp_message").remove();
    event.preventDefault();
    var form = event.target;
    $.post($(this).attr("action"), $(this).serialize(), function(data) {
        var resp = parser.parseFromString(data, "text/html");
        var respMsg = $("table ~ script", resp)[0].nextSibling.textContent;
        var div = $("<div id='resp_message'>").text(respMsg);
        
        if (/You entered this Giveaway/.exec(respMsg) ||
            /You left this Giveaway/.exec(respMsg)) {
            div.css("color", "green");
        }
        else {
            div.css("color", "red");
        }

        var newForm = $('table:has(form[action^="/enterGA"],form[action^="/leaveGA"]) form', resp);
        newForm.submit(handleGiveawayForm);
        $(form).after(div).replaceWith(newForm);
    });
}

function quickLook(data) {
    var doc = parser.parseFromString(data, "text/html");

    // Look for table with form (this means the giveaway is active)
    var _table = $('table:has(form[action^="/enterGA"],form[action^="/leaveGA"])', doc);
    if (_table.length !== 0) {
        $(_table).find("form").submit(handleGiveawayForm);
    }
    else {
        // Otherwise grab the info and the winner's list
        _table = $('table.tabella-forum:first, table.tabella-forum:last', doc);
        if ($(_table).length === 0) {
            _table = $('a[href^="/GA/"]', doc).closest('table:not(.riga3,.riga)').addClass("tabella-forum").css({
                padding: 5
            });
            if ($(_table).length !== 0) {
                quickEnter(_table);
            }
        }
    }
    quickView(_table);
}

function quickEnter(doc) {
    entry = $('a[href^="/GA/"]:has(b > u):not(.quick)', doc).addClass('quick');

    td = $("<td>").css({
        textAlign: "center"
    }).appendTo($(entry).closest("tr").prev("tr"));

    $('<i class="icon-info-sign"></i>').click(function(event) {
        event.preventDefault();
        window.open("http://store.steampowered.com/app/" + getIdFromLink($(this).closest(".riga")));
    }).append("&nbsp;").appendTo(td);

    $('<i class="icon-search"></i>').click(function(){
        event.preventDefault();
        quickView();
        $.get("http://www.galagiveaways.com/games/" + getIdFromLink($(this).closest(".riga")), quickLook);
    }).append("&nbsp;").appendTo(td);

    $('<i class="icon-bolt"></i>').click(function(event) {
        event.preventDefault();
        quickView();
        $.get($(this).closest("tr").next("tr").find("a").attr("href"), quickLook);
    }).appendTo(td);
}

function cleanGameList(doc) {
    if (hasLinks(doc)) {
        cleanUp(doc);
        showWishlist(doc);
        handleSteamLinks(doc);
        quickEnter(doc);
        // this needs to be the last line as it attachs stuff to the actual view
        lineUp(doc);
        shouldScroll = true;
    }
}

function getNextPage() {
    page += 1;
    shouldScroll = false;
    td = $("<td class='spinner'>").css({
            verticalAlign: "middle",
            textAlign: "center"
        }).append($('<i class="icon-spinner icon-spin icon-4x"></i>'));
    rearrangeTable(table, td, 4, false);

    $.get("http://www.galagiveaways.com/home/" + page, function(data) {
        $('td.spinner').remove();
        cleanGameList(parser.parseFromString(data, "text/html"));
    });
}

function main() {
    var icon = $("<i class='icon-repeat'></i>").appendTo("body");

    icon.css({
        position: "fixed",
        top: $(window).innerHeight() - 21,
        left: $(window).innerWidth() - 21
    }).click(function(event){
        if ($(this).hasClass("icon-spin")) return;
        $(this).addClass('icon-spin');
        getGames(function() {
            getWishlist(function() {
                $(icon).removeClass('icon-spin');
                if (/^\/home/.test(window.location.pathname)) {
                    cleanGameList(document);
                }
            }, true);
        }, true);
    });

    $(window).resize(function() {
        icon.css({
            top: $(window).innerHeight() - 21,
            left: $(window).innerWidth() - 21
        });
    });

    $("<div id='iges_header'>").css({
        width: "100%",
        position: "fixed",
        backgroundImage: "url(http://1-ps.googleusercontent.com/x/s.galagive.appspot.com/www.galagiveaways.com/img/xslate.jpg.pagespeed.ic.K9-NSGsDLG.jpg)",
        top: 0,
        left: 0
    }).hide().appendTo("body");
    
    $(window).scroll(function(event) {
        if (window.pageYOffset >= 1) {
            $("div#iges_header").append($("table.header")).show();
        }
        else {
            if (shouldScroll)
            $("table.header").prependTo("body");
            $("div#iges_header").hide();
        }

        if (window.innerHeight + window.pageYOffset >= document.height && shouldScroll) {
            getNextPage();
        }
    });

    if (/^\/home/.test(window.location.pathname)) {
        // Store the table we are about to fuck up
        table = getLinks(document).closest("table");
        cleanGameList(document);
    }
    else if (/^\/games\/\d+/.test(window.location.pathname)) {
        quickEnter(document);
    }
    else if (/^\/profile/.test(window.location.pathname)) {
        var GA = $('a[href^="/GA/"]').closest("td");
        GA.filter(function() {
            return (/status: Lost/).test(this.innerText);
        }).css('opacity', 0.3).addClass('lost');//.detach();
        GA.filter(function() {
            return (/status: Won/).test(this.innerText);
        }).addClass('won');

        rearrangeTable(GA.filter(':not(.lost,.won)').closest("table"), GA.filter(':not(.lost,.won)'), 4, true);
        rearrangeTable(GA.filter('.lost').closest("table"), GA.filter('.lost'), 4, true);
        rearrangeTable(GA.filter('.won').closest("table"), GA.filter('.won'), 4, true);

        $('img', GA).click(function(event){
            event.preventDefault();
            quickView();
            $.get($(this).closest("a").attr("href"), quickLook);
        });
    }
    else if (/^\/topic\/[a-f0-9]+/.test(window.location.pathname)) {
        $('.tabella-forum:not(:first) tr:nth-child(2) td:last-child, .tabella-forum tr:nth-child(3) td:last')
            .each(function() {
                $(this).html($(this).html().replace(/(\S+:\/\/\S+)/g, "<u><a href='$1'>$1</a></u>"));
            });
    }
}<commit_msg>Remove unnecessary reorganization code for profiles.  Fixed issue #1.<commit_after>var parser = new DOMParser();
var my_games_list = {};
var my_wishlist = {};
var my_steam_name = null;
var table = null;
var page = 1;
var shouldScroll = true;

function getGames(callback, forceupdate) {
    chrome.extension.sendRequest({method: (forceupdate === true) ? "updateGames" : "getGames"}, function(settings) {
        games = settings.games;
        if ( games === undefined ) {
            games = {};
        }

        for (var i = 0; i < games.length; i++) {
            my_games_list[games[i].id] = true;
        }

        if (typeof callback == "function") {
            callback();
        }
    });
}

function getWishlist(callback, forceupdate) {
    chrome.extension.sendRequest({method: (forceupdate === true) ? "updateWishlist" : "getWishlist"}, function(settings) {
        wishlist = settings.wishlist;

        if (wishlist === undefined) {
            wishlist = {};
        }

        for (var i = 0; i < wishlist.length; i++) {
            my_wishlist[wishlist[i].id] = true;
        }

        if (typeof callback == "function") {
            callback();
        }
    });
}

function getUser(callback) {
    chrome.extension.sendRequest({method: "getSteamName"}, function(settings) {
        my_steam_name = settings.user;
        if (typeof callback == "function") {
            callback();
        }
    });
}

getUser(function() {
    getWishlist(function() {
        getGames(main);
    });
});

$("head").append('<link href="//netdna.bootstrapcdn.com/font-awesome/3.0/css/font-awesome.css" rel="stylesheet">');

function rearrangeTable(table, elements, desired_count, detach) {
    elements.each(function() {
        if (detach) {
            $(this).detach();
        }
        var row = $("> tbody > tr", table).filter(function() {
            return $(this).children("td").length < desired_count;
        }).first();

        if (row.length === 0) {
            row = $("<tr>").appendTo(table);
        }
        row.append($(this));
    });
}


function getLinks(doc) {
    return $('td:not(.tabella-features1, tabella-features2) > a[href^="/GA/"]:has(img)', doc);
}

function getIdFromLink(elem) {
    td = $(elem).closest("td");
    var match = (/(?:apps|subs)\/(\d+)/).exec(td.attr("style"));
    return match[1];
}


function hasLinks(doc) {
    return getLinks(doc).length > 0;
}

function cleanUp(doc) {
    var links = getLinks(doc);

    links.each(function(){
        if (getIdFromLink(this) in my_games_list) {
            $(this).closest("td").remove();
        }
    });
}

function lineUp(doc) {
    var links = getLinks(doc);
    rearrangeTable(table, links.closest("td"), 4, true);
}

function showWishlist(doc) {
    var hilite = chrome.extension.getURL("wishlist.png");
    var links = getLinks(doc);

    links.each(function (){
        if (getIdFromLink(this) in my_wishlist) {
            $("img", this).attr("src", hilite);
        }
    });
}

function quickView(content) {
    if ($("div.quickview.overlay").length === 0) {
        var div = $('<div>').appendTo("body").addClass("quickview overlay")
        .css({
            opacity: 0.4,
            backgroundColor: "black",
            position: "fixed",
            top: 0,
            left: 0,
            width: "100%",
            height: "100%",
            zIndex: 25
        }).click(function() {
            $(".quickview").remove();
        });
    }
    
    if ($("div.quickview.waiting").length === 0) {
        $("<div>").addClass("quickview waiting tabella-forum").appendTo("body").css({
            opacity: 1,
            position: "fixed",
            top: "50%",
            left: "50%",
            width: "297px",
            height: "136px",
            marginLeft: "-149px",
            marginTop: "-68px",
            textAlign: "center",
            zIndex: 100
        }).append($('<i class="icon-spinner icon-spin icon-4x"></i>')).hide();
    }

    if ($("div.quickview.content").length === 0) {
        $("<div>").addClass("quickview content").appendTo("body").css({
            opacity: 1,
            position: "fixed",
            top: "50%",
            left: "50%",
            textAlign: "center",
            zIndex: 100
        }).hide();
    }

    $("div.quickview.overlay").show();
    if (content !== undefined) {
        handleSteamLinks(content);
        $("div.quickview.waiting").hide();
        $("div.quickview.content").show().empty().append(content).css({
            width: $(content).width(),
            height: $(content).height(),
            marginLeft: -($(content).width() / 2),
            marginTop: -($(content).height() / 2)
        });
    } else {
        $("div.quickview.waiting").show();
        $("div.quickview.content").hide();
    }
}

function handleSteamLinks(doc) {
    $('a[href^="http://store.steampowered.com/app/"]', doc).attr("target", "_blank");
}

function handleGiveawayForm(event) {
    $(this).find("button").attr("disabled", "disabled");
    $("#resp_message").remove();
    event.preventDefault();
    var form = event.target;
    $.post($(this).attr("action"), $(this).serialize(), function(data) {
        var resp = parser.parseFromString(data, "text/html");
        var respMsg = $("table ~ script", resp)[0].nextSibling.textContent;
        var div = $("<div id='resp_message'>").text(respMsg);
        
        if (/You entered this Giveaway/.exec(respMsg) ||
            /You left this Giveaway/.exec(respMsg)) {
            div.css("color", "green");
        }
        else {
            div.css("color", "red");
        }

        var newForm = $('table:has(form[action^="/enterGA"],form[action^="/leaveGA"]) form', resp);
        newForm.submit(handleGiveawayForm);
        $(form).after(div).replaceWith(newForm);
    });
}

function quickLook(data) {
    var doc = parser.parseFromString(data, "text/html");

    // Look for table with form (this means the giveaway is active)
    var _table = $('table:has(form[action^="/enterGA"],form[action^="/leaveGA"])', doc);
    if (_table.length !== 0) {
        $(_table).find("form").submit(handleGiveawayForm);
    }
    else {
        // Otherwise grab the info and the winner's list
        _table = $('table.tabella-forum:first, table.tabella-forum:last', doc);
        if ($(_table).length === 0) {
            _table = $('a[href^="/GA/"]', doc).closest('table:not(.riga3,.riga)').addClass("tabella-forum").css({
                padding: 5
            });
            if ($(_table).length !== 0) {
                quickEnter(_table);
            }
        }
    }
    quickView(_table);
}

function quickEnter(doc) {
    entry = $('a[href^="/GA/"]:has(b > u):not(.quick)', doc).addClass('quick');

    td = $("<td>").css({
        textAlign: "center"
    }).appendTo($(entry).closest("tr").prev("tr"));

    $('<i class="icon-info-sign"></i>').click(function(event) {
        event.preventDefault();
        window.open("http://store.steampowered.com/app/" + getIdFromLink($(this).closest(".riga")));
    }).append("&nbsp;").appendTo(td);

    $('<i class="icon-search"></i>').click(function(){
        event.preventDefault();
        quickView();
        $.get("http://www.galagiveaways.com/games/" + getIdFromLink($(this).closest(".riga")), quickLook);
    }).append("&nbsp;").appendTo(td);

    $('<i class="icon-bolt"></i>').click(function(event) {
        event.preventDefault();
        quickView();
        $.get($(this).closest("tr").next("tr").find("a").attr("href"), quickLook);
    }).appendTo(td);
}

function cleanGameList(doc) {
    if (hasLinks(doc)) {
        cleanUp(doc);
        showWishlist(doc);
        handleSteamLinks(doc);
        quickEnter(doc);
        // this needs to be the last line as it attachs stuff to the actual view
        lineUp(doc);
        shouldScroll = true;
    }
}

function getNextPage() {
    page += 1;
    shouldScroll = false;
    td = $("<td class='spinner'>").css({
            verticalAlign: "middle",
            textAlign: "center"
        }).append($('<i class="icon-spinner icon-spin icon-4x"></i>'));
    rearrangeTable(table, td, 4, false);

    $.get("http://www.galagiveaways.com/home/" + page, function(data) {
        $('td.spinner').remove();
        cleanGameList(parser.parseFromString(data, "text/html"));
    });
}

function main() {
    var icon = $("<i class='icon-repeat'></i>").appendTo("body");

    icon.css({
        position: "fixed",
        top: $(window).innerHeight() - 21,
        left: $(window).innerWidth() - 21
    }).click(function(event){
        if ($(this).hasClass("icon-spin")) return;
        $(this).addClass('icon-spin');
        getGames(function() {
            getWishlist(function() {
                $(icon).removeClass('icon-spin');
                if (/^\/home/.test(window.location.pathname)) {
                    cleanGameList(document);
                }
            }, true);
        }, true);
    });

    $(window).resize(function() {
        icon.css({
            top: $(window).innerHeight() - 21,
            left: $(window).innerWidth() - 21
        });
    });

    $("<div id='iges_header'>").css({
        width: "100%",
        position: "fixed",
        backgroundImage: "url(http://1-ps.googleusercontent.com/x/s.galagive.appspot.com/www.galagiveaways.com/img/xslate.jpg.pagespeed.ic.K9-NSGsDLG.jpg)",
        top: 0,
        left: 0
    }).hide().appendTo("body");
    
    $(window).scroll(function(event) {
        if (window.pageYOffset >= 1) {
            $("div#iges_header").append($("table.header")).show();
        }
        else {
            if (shouldScroll)
            $("table.header").prependTo("body");
            $("div#iges_header").hide();
        }

        if (window.innerHeight + window.pageYOffset >= document.height && shouldScroll) {
            getNextPage();
        }
    });

    if (/^\/home/.test(window.location.pathname)) {
        // Store the table we are about to fuck up
        table = getLinks(document).closest("table");
        cleanGameList(document);
    }
    else if (/^\/games\/\d+/.test(window.location.pathname)) {
        quickEnter(document);
    }
    else if (/^\/profile/.test(window.location.pathname)) {
        $('a[href^="/GA/"] img').click(function(event){
            event.preventDefault();
            quickView();
            $.get($(this).closest("a").attr("href"), quickLook);
        });
    }
    else if (/^\/topic\/[a-f0-9]+/.test(window.location.pathname)) {
        $('.tabella-forum:not(:first) tr:nth-child(2) td:last-child, .tabella-forum tr:nth-child(3) td:last')
            .each(function() {
                $(this).html($(this).html().replace(/(\S+:\/\/\S+)/g, "<u><a href='$1'>$1</a></u>"));
            });
    }
}
{'fraction_non_alphanumeric': 0.19962847175026052, 'fraction_numerical': 0.008155498165012914, 'mean_word_length': 2.280618311533888, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2669099', 'n_tokens_mistral': 7128, 'n_tokens_neox': 6855, 'n_words': 1164}