SG Entered Giveaway Page

Added point value, creator, and level at Giveaway > Entered page.

目前為 2016-10-09 提交的版本,檢視 最新版本

// ==UserScript==
// @name         SG Entered Giveaway Page
// @namespace    https://steamcommunity.com/id/Ruphine/
// @version      1
// @description  Added point value, creator, and level at Giveaway > Entered page.
// @author       Ruphine
// @match        https://www.steamgifts.com/giveaways/entered*
// @icon         https://cdn.steamgifts.com/img/favicon.ico
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// ==/UserScript==

const PROCESS_ENDED_GA = false; //change to [true] if you want to show info for ended GA too
const SHOW_POINT   = true; //change to [false] if you don't want to show giveaway point value
const SHOW_CREATOR = true; //change to [false] if you don't want to show giveaway creator
const SHOW_LEVEL   = true; //change to [false] if you don't want to show giveaway level
const CACHE_TIME = 60*60*1000; //1 hour. cache data will be deleted after 1 hour not opening https://www.steamgifts.com/giveaways/entered

var LastSavedData = GM_getValue("lastchecked", 0);
var CachedData;

if(LastSavedData <  Date.now() - CACHE_TIME) //delete cache if over CACHE_TIME has passed
	CachedData = [];
else
	CachedData = JSON.parse(GM_getValue("cache", "[]"));

//create column level
if(SHOW_LEVEL){
	$(".table__heading .table__column--width-fill").after('<div class="table__column--width-small text-center">Level</div>');
	$(".table__rows .table__column--width-fill").after('<div class="table__column--width-small text-center"></div>');
}

//process each giveaway
$(".table__row-outer-wrap").each(function(index, element){
	if($(element).find("input").length > 0 || PROCESS_ENDED_GA){ //check if giveaway is still running. ended giveaways don't have remove button.
		var GiveawayID = $(element).find(".table__column__heading")[0].href.split("/")[4];
		var Giveaway_data = $.grep(CachedData, function(e){ return e.id == GiveawayID; });
		if(Giveaway_data.length === 0)// if no data saved
			GetGiveawayData(element);
		else //if giveaway data is already saved
			ShowGiveawayData(element, Giveaway_data[0]);
	}
});

function GetGiveawayData(element){
	var GiveawayID = $(element).find(".table__column__heading")[0].href.split("/")[4];
	GM_xmlhttpRequest({
		method: "GET",
		timeout: 10000,
		url: "/giveaway/" + GiveawayID + "/",
		onload: function(result)
		{
			var page = result.responseText;
			var point = $(page).find(".featured__heading__small");
			point = point[point.length-1].textContent.replace("(", "").replace(")", "").replace("P", ""); //only retrieve point value
			var creator = $(page).find(".featured__column--width-fill.text-right a").text();

			var level = $(page).find(".featured__column--contributor-level").text().replace("Level ", "").replace("+", "");
			if(level === "") level = 0;
			var Giveaway_data = {id: GiveawayID, point: point, creator: creator, level: level};
			console.log(JSON.stringify(Giveaway_data));
			ShowGiveawayData(element, Giveaway_data);
			CachedData.push(Giveaway_data);
			GM_setValue("cache", JSON.stringify(CachedData));
			GM_setValue("lastchecked", Date.now());
		}
	});
}

function ShowGiveawayData(element, data){
	if(SHOW_POINT){
		var title = $(element).find(".table__column--width-fill p a");
		var text = $(title).text() + " (" + data.point + "P)";
		$(title).text(text);
	}
	if(SHOW_CREATOR){
		var timeleft = $(element).find(".table__column--width-fill p span");
		$(timeleft).after(" by <a class='giveaway__username' href='/user/" + data.creator + "'>" + data.creator + "</a>");
	}
	if(SHOW_LEVEL)
		$(element).find(".table__column--width-small.text-center")[0].innerHTML = data.level;
}