/* SGE addon - This a user-script that provides extra functionallity on top of steamgifts.com
Copyright (C) 2012 Rossen Georgiev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
(function($) {
// chrome/opera hack;
var unsafeWindow = this['unsafeWindow'] || window;
// check if already loaded
if(unsafeWindow.gafIsLoaded) return false;
unsafeWindow.gafIsLoaded = true;
// doesnt load on search page
if(window.location.href.indexOf("search/") > -1 ) return false;
// make sure we only load the code on supported pages
// --------
// This was functionality was moved from the init script.
// Adding support for certain parts of the site will no longer require an action on the user side
if(!window.location.href.match(/^https?\:\/\/www\.steamgifts\.com\/?(#|(open|new|forum).*)?$/))
if(typeof unsafeWindow.gafDev === 'undefined') return false;
// load DLC database
$('body').append('');
// fades background and shows+centre the popup
unsafeWindow.gafPopupShow = function() {
$("#gafPopup").css({
"top": document.documentElement.clientHeight / 2 - 150,
"left": document.documentElement.clientWidth / 2 - 200
});
$('#gafPopup').fadeIn('slow');
$('#gafPopupBackground').fadeIn('normal');
}
// hide popup and background
unsafeWindow.gafPopupHide = function() {
$('#gafOverlay').remove();
$('#gafPopup').fadeOut('normal');
$('#gafPopupBackground').fadeOut('slow');
$('.ajax_gifts>div.post.quickview').removeClass('quickview');
}
// function is called everytime there is a change to textarea for the filter
// and contents mirrored in the cookie
unsafeWindow.gafFormUpdate = function() {
unsafeWindow.localStorage.gafFilter = $('#gafFilterInput').val();
unsafeWindow.gafFilter = $('#gafFilterInput').val().split(',');
}
// called then ignore button is pressed
// * adds name of the game to the filter and updates the cookie
// * hides all giveaways for the same game
// * slide up animantion
unsafeWindow.gafIgnore = function(elm) {
var node = elm.parentNode.parentNode.parentNode.parentNode;
$(elm).hide();
$('#gafFilterInput').append(',' + $(node).find('.title').find('a').html());
unsafeWindow.gafFormUpdate();
unsafeWindow.gafApplyFilter($('.ajax_gifts>div.post'));
unsafeWindow.gafLoadingCheck();
}
// removes giveaway matching the filter
unsafeWindow.gafApplyFilter = function(gafs) {
// quick search
if($.trim($('input[name=gafSearch]').val()) != "") {
var match_pat = new RegExp($.trim($('input[name=gafSearch]').val()).replace(/[-[\]{}()+?.,\\^$|#\s]/g, '\\$&'),'i');
$(gafs).find('.title a').each(function(k,e) {
if($(e).text().match(match_pat)) {
$(e).parent().parent().parent().stop(true,true).slideDown();
} else {
$(e).parent().parent().parent().stop(true,true).slideUp();
}
});
setTimeout(unsafeWindow.gafLoadingCheck, 1000);
return false;
}
var fall = $('#sg_fid0').is(':checked');
var fftr = $('#sg_fid1').is(':checked');
var fwish = $('#sg_fid2').is(':checked');
var ffade = $('#sg_fid3').is(':checked');
var fpts = $('#sg_fid4').is(':checked');
var fdlc = $('#sg_fid5').is(':checked');
var freg = $('#sg_fid6').is(':checked');
var fgrp = $('#sg_fid7').is(':checked');
var fgreen = $('#sg_fid8').is(':checked');
var fred = $('#sg_fid9').is(':checked');
var fcont_min = parseFloat($('#gafViewLinks .price.min').text().replace('$',''));
var fcont_max = parseFloat($('#gafViewLinks .price.max').text().replace('$',''));
// show all giveaways
if(fall) {
gafs = $(gafs).filter(':hidden');
gafs.find('.description').attr('style','');
gafs.slice(0,5).slideDown(function() {
gafs.slice(5).show();
});
return true;
}
var finalset = []; // final set of giveways that will be displayed
var fgafs = [] // filtered set of giveaways, source for other filters
// filter already owned or ignored giveaways
if(fftr || fwish) {
var gaflist = $.unique(unsafeWindow.gafFilter.concat(unsafeWindow.gafSyncFilter).sort());
var gafstr;
// when the list is empty, additional filtering like for contributor giveaways doenst work
// this this is because, the code block for the second block doesnt execture
gaflist.push('_______'); // quick fix fake entry
// remove all DLC for games we dont own
if(fdlc) $.merge(gaflist, unsafeWindow.gafDLCSyncd);
fgafs = $(gafs).map(function(i,giveaway) {
var elm = $(giveaway).find('a')[0];
$(giveaway).find('.description').attr('style','');
match = false;
for(j = 0; j < gaflist.length; j++) {
gafstr = $.trim(gaflist[j]).toLowerCase().replace(/&/g,'&');
if(gafstr == "") continue;
// check if we are parsing a highlight entry
var highlight = false;
if(gafstr.match(/^\d+\. /)) {
highlight = true;
gafstr = gafstr.replace(/^\d+\. /,'');
}
var pat = new RegExp('^'+gafstr.replace(/[\_\-\[\]{}()+?.,\\^$|#\s]/g, '\\$&').replace(/\*/g,'.*')+'$');
if(highlight) {
// if element doenst match continue with next one
if(!elm.innerHTML.toLowerCase().match(pat)) continue;
// else highlight the element and make it visible
$(giveaway).find('.description').css({background: '#aaFFa8'});
if(fwish)
return giveaway; // append fgafs with highlighted giveway, if highlighted is enabled (fwish)
}
else if(elm.innerHTML.toLowerCase().match(pat))
{
match = true;
break;
}
}
if(!match && fftr) return giveaway; // append giveaway if not in filter list
}).get();
} else {
fgafs = $(gafs).find('.description').attr('style','').parent().parent().get();
}
// filter finalset by number of points
if(fpts) {
// need to get current number of points, if logged in
var p = $('#navigation li a.arrow').slice(1,2).text().match(/\-?(\d+)P/);
if(!p) return false;
else p = parseInt(p[1]);
if(p < 0) return false;
fgafs = $(fgafs).find('.title span').map(function(i,e) {
var s = e.innerHTML.match(/(\d+)P/);
if(s) {
if(s && parseInt(s[1]) <= p)
return $(e).parent().parent().parent().get();
}
}).get();
}
// include contributor giveaways
if(ffade) {
fgafs = $(fgafs).filter(':not(.fade)').get();
}
// include group giveaways
if(fgrp) {
// subset of group giveaways
var tmp = $(fgafs).find('.group_only').parent().parent().parent();
// susbet of tmp of red contributor giveways
var subset = tmp.find('.contributor_only:not(.green)').parent().parent().parent();
// subtract sets
$.merge(finalset, $.grep(tmp, function(e) { return $.inArray(e, subset) == -1 }));
}
// include contributor giveaways
if(fgreen) {
$.merge(finalset, $(fgafs).find('.contributor_only.green').parent().parent().parent().get());
}
if(fred) {
$.merge(finalset, $(fgafs).find('.contributor_only:not(.green)').parent().parent().parent().get());
}
// contributor range
finalset = $.grep(finalset, function(e) {
e = $(e);
if(e.find('.contributor_only').length > 0) {
var amount = parseFloat(e.find('.contributor_only').text().match(/[0-9\.,]+/)[0]);
return (amount >= fcont_min) && (amount <= fcont_max); // apply contributor range
}
return true;
}, false);
// include regular giveaways
if(freg) {
$.merge(finalset, $(fgafs).map(function(i,e) {
if($(e).find('.group_only,.contributor_only').length == 0) {
return e;
}
}).get());
}
// allways show developer giveaways on top
$.merge(finalset, $(".content>.post .new:contains('Developer')").parent().parent().parent().get());
//---------------------
// Filters end
//---------------------
// finalize finalset
// if no filters are selected simply output the whole list
if(!fgreen && !fred && !fgrp && !freg) finalset = fgafs;
else finalset = $.unique(finalset);
// excludeset consists of only giveaways that should be hidden
var excludeset = $(gafs).map(function() {
if( $.inArray(this,finalset) < 0) {
return this;
}
}).get();
// display results :)
excludeset = $(excludeset).filter(':visible');
finalset = $(finalset).filter(':hidden');
excludeset.slice(0,5).stop(true,true).slideUp(function() {
excludeset.slice(5).stop(true,true).hide();
});
finalset.slice(0,5).stop(true,true).slideDown(function() {
finalset.slice(5).stop(true,true).show();
});
}
// add ignore buttons to all giveaways :)
unsafeWindow.gafAddIgnoreButton = function(elms) {
var gafs = $(elms).find('a');
for(i = 0; i < gafs.length; i++) {
if(gafs[i].href.match(/\/giveaway.*?entries$/))
{
$(gafs[i].parentNode.parentNode).append("Ignore")
}
}
}
// function is called by iframe onLoad
// filters new giveaways and adds them to the current page for endless scroll
unsafeWindow.gafPageLoaded = function(data) {
if(data == '') {
return;
} else {
var loadedContent = $(data);
}
if(window.location.href.match(/forum/)) {
if(unsafeWindow.gafHrefPart.length > 5) {
unsafeWindow.gafAddHC(loadedContent);
if(unsafeWindow.gafReverse) {
loadedContent.find('.parent_container').reverse().insertAfter($('.parent_container').last());
}
else {
loadedContent.find('.parent_container').insertAfter($('.parent_container').last());
}
// restore click functionallity
// code taken from SG, credit: CG
$('.delete_comment').click(function(){
$("input[name='delete_comment_id']").val($(this).parent().attr("rel"));
$('#delete_comment').submit();
return false;
});
$('.comment_reply .edit_comment').click(function(){
$(this).parent().parent().parent().children('.user_body').hide();
$(this).parent().parent().parent().children('.user_edit').show();
return false;
});
$('.comment_reply .reply_link a').click(function(){
id=$(this).attr('rel');
depth=parseInt($(this).attr('class'))+1;
$("#comment_form #parent_id").val(id);
$("span.title").html("Add a reply");
$("span.cancel").show();
$("#comment_form").insertAfter('.comment#' + id);
$("#comment_form").addClass('border_container');
return false;
});
$('#comment_form span.cancel a').click(function(){
$("#comment_form #parent_id").val(0);
$("span.title").html("Add a comment");
$("span.cancel").hide();
$("#comment_form").insertAfter('#comment_location');
$("#comment_form").removeClass('border_container');
return false;
});
$('.cancel_edit_comment').click(function(){
$(this).parent().parent().parent().parent().children('.user_body').show();
$(this).parent().parent().parent().parent().children('.user_edit').hide();
return false;
});
}
else {
loadedContent.find('.row a').each(function(key,obj) {
if($(obj).attr('href').match(/^\/forum/)) {
var newURL = $(obj).attr('href') + "/page/31337";
$(obj).attr('href',newURL);
}
});
loadedContent.find('.row').appendTo('.discussions');
$('.row').css({padding: $('#gafSlider').slider("value") + "px 0"});
}
}
else
{
// remove Contributor links from loaded elements
// get all giveaways
var elms = loadedContent.find('.ajax_gifts>div.post');
// update points from endless scroll page load
var p = loadedContent.find('#navigation li a.arrow').slice(1,2).text().match(/(\d+)P/);
if(p && parseInt(p[1]) != NaN) {
unsafeWindow.gafSetPoints(p[1]);
}
// new page refresh
if(unsafeWindow.gafLoadingNew) {
var codebase = {};
var celms = $('.ajax_gifts>div.post').slice(0,15);
var code = celms.first().find('.title a').attr('href').match(/way\/([a-z0-9]{4,6})\//i)[1];
var i = 0;
for(; i < 10; i++) {
if($(elms[i]).find('.title a').attr('href').match(/way\/([a-z0-9]{4,6})\//i)[1] == code) break;
}
if(i > 0) {
$('div.pagination:first').after(elms.slice(0,i).hide().css({padding:unsafeWindow.gafPadding+'px 0'}));
unsafeWindow.gafAddIgnoreButton(elms.slice(0,i));
unsafeWindow.gafApplyFilter(elms.slice(0,i));
}
celms.each(function(k,v) {
codebase[ $(v).find('.title a').attr('href').match(/way\/([a-z0-9]{4,6})\//i)[1] ] = k;
});
elms.slice(i,20).each(function(k,v) {
var test = codebase[ $(v).find('.title a').attr('href').match(/way\/([a-z0-9]{4,6})\//i)[1] ];
if(test != undefined) {
$(celms[test]).find('.time_remaining').html('').append($(v).find('.time_remaining').html());
$(celms[test]).find('.entries span').slice(0,2).remove();
$(celms[test]).find('.entries').prepend($(v).find('.entries span').slice(0,2));
}
});
unsafeWindow.gafLoadingNew = false;
}
else {
unsafeWindow.gafAddIgnoreButton(elms);
if(elms.length > 0)
{
if($('div.post').length > 0) { $('div.post').last().after(elms); }
else { $('div.pagination:first').after(elms); }
}
$('div.post').css({padding: $('#gafSlider').slider("value") + "px 0"});
unsafeWindow.gafApplyFilter(elms.hide());
}
unsafeWindow.gafAddOverlay();
}
if( (!unsafeWindow.gafReverse && unsafeWindow.gafPage == unsafeWindow.gafMaxPage)
|| (unsafeWindow.gafReverse && unsafeWindow.gafPage == 1) ) {
$('#gafLoading').html("
Dear User, the SGE addon will be discontinued. Consider using SG+. Happy holidays! -Zo
');
$('input[name=gafSearchClear]').click(function() {
$('input[name=gafSearch]').val('');
unsafeWindow.gafApplyFilter($('.ajax_gifts>div.post')); });
$('input[name=gafSearch]').keyup(function() {
clearTimeout(unsafeWindow.gafssssssss);
unsafeWindow.gafssssssss = setTimeout("gafApplyFilter($('.ajax_gifts>div.post'));",400);
});
}
// reverse order settings on page
if(unsafeWindow.gafShowReverse && unsafeWindow.gafShowESS) {
$('.results:first').append(" and order "+((unsafeWindow.gafReverse)?'newest first':'oldest first')+"");
}
else if(unsafeWindow.gafShowESS) {
$('.results:first').append("");
$('#gafSlider').slider({
min:0,max:25,animate:true,value: unsafeWindow.gafPadding,
slide: function(event, ui) {
$('.post,.row').css({padding:ui.value+'px 0'});
},
stop: function(event, ui) {
unsafeWindow.gafPadding = ui.value;
document.cookie = "gafPadding=" + ui.value + "; expires=Fri, 27 Jul 2030 02:47:11 UTC; path=/";
}
});
$('.ajax_gifts>div.post,.row').css({padding: unsafeWindow.gafPadding +'px 0'});
}
if(unsafeWindow.gafScrollOn) {
// endless scrolling elements and adjustments
$('.numbers').remove()
// turn off scrolling if there is only one page
if(unsafeWindow.gafMaxPage == 1) {
unsafeWindow.gafScrollOn = false;
$('.pagination:last').html("
You've reached the end.
");
}
else {
unsafeWindow.gafPutLoading();
}
}
// reverse order
if(unsafeWindow.gafReverse) {
$('.parent_container').reverse().appendTo('.comment_container');
}
// fix urls for discussions in the forum page for reverse order
if(unsafeWindow.gafRepReverse) {
$('.row a').each(function(key,obj) {
if($(obj).attr('href').match(/^\/forum/)) {
var newURL = $(obj).attr('href') + "/page/31337";
$(obj).attr('href',newURL);
}
});
}
// replicate nagivation bar to use as float menu
$('body').prepend('
');
$('.gafNav').attr('style','display:none;overflow:hidden;box-shadow:0 0 8px #000000;z-index:55;background:#25303A url(http://www.steamgifts.com/img/background_clean.png) no-repeat;background-position: center -60px;position:fixed;left:0px;height:62px;width:100%;');
$('.gafNav').css({padding:0});
$("#navigation ol li:first").after('