Last active
August 17, 2016 01:22
-
-
Save nabbynz/1c90482c0b6eeec413e70693bd9eb821 to your computer and use it in GitHub Desktop.
TagPro Stats Off Integrator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Stats Off Integrator | |
// @description Saves your game stats when you have official stats turned off, then integrates them into your profile page. | |
// @version 0.1.1 | |
// - Added Stats On/Off Checkbox (can turn stats on/off from anywhere) | |
// @include http://tagpro-*.koalabeast.com* | |
// @updateURL https://gist.github.com/nabbynz/1c90482c0b6eeec413e70693bd9eb821/raw/Stats_Off_Integrator.user.js | |
// @downloadURL https://gist.github.com/nabbynz/1c90482c0b6eeec413e70693bd9eb821/raw/Stats_Off_Integrator.user.js | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @grant GM_deleteValue | |
// @grant GM_addStyle | |
// @connect koalabeast.com | |
// @grant GM_xmlhttpRequest | |
// @author nabby | |
// ==/UserScript== | |
console.log('START: ' + GM_info.script.name + ' (v' + GM_info.script.version + ' by ' + GM_info.script.author + ')'); | |
var hideWinLossStats = false; //true = the "Win %/Wins/Losses/Ties/Saves/Save %/Disconnects/Points" rows are all hidden. false = shown | |
var defaultToStatsOffTab = true; //if true the "Stats Off" tab will be auto-selected when the page loads | |
var alwaysSaveStatsOffGames = false; //true = game stats are always saved by this script (whether stats are on or off). false = stats are only saved (by this script) when stats are off | |
var showStatsCheckboxOnNavBar = true; //shows the "Stats" checkbox on the Nav Bar (for turning stats on/off) | |
var showStatsCheckboxInGame = true; //shows the "Stats" checkbox during a game (for turning stats on/off next game) | |
var checkboxOnScoreboard = true; //if true the "Stats" checkbox will be on the scoreboard (in a game). false will be under the "Exit" link. | |
var $uHome = $('#userscript-home'); | |
var $uTop = $('#userscript-top'); | |
var $uBottom = $('#userscript-bottom'); | |
var options = { //defaults | |
'SavedGames': { type:'script', display:'', value:[] }, | |
}; | |
var SOI_Selections; | |
function secondsToHMS(d) { | |
d = Number(d); | |
var h = Math.floor(d / 3600); | |
var m = Math.floor(d % 3600 / 60); | |
var s = Math.floor(d % 3600 % 60); | |
return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s); | |
} | |
var WhichPageAreWeOn = function(){ | |
if (window.location.port) { //In a real game | |
return('ingame'); | |
} else if (document.URL.indexOf('/games/find') > 0) { //Joining page | |
return('joining'); | |
} else if ($('#userscript-home').length) { //Chosen server homepage | |
return('server'); | |
} else if (document.URL.indexOf('/profile/') > 0) { | |
if ($('#saveSettings').length) { | |
return('profile'); //Profile page and logged in | |
} else { | |
return('profileNotOurs'); //Profile page, but not our one (or we're logged out) | |
} | |
} else if (document.URL.indexOf('/groups') > 0) { | |
return('groups'); | |
} else if (document.URL.indexOf('/boards') > 0) { | |
return('boards'); | |
} else if (document.URL.indexOf('/maps') > 0) { | |
return('maps'); | |
} else if (document.URL.indexOf('/settings') > 0) { | |
return('settings'); | |
} | |
}; | |
var PageLoc = WhichPageAreWeOn(); | |
function loadData() { | |
var data = $.extend(true, [], SOI_Selections.SavedGames.value); | |
//console.log(data); | |
if (data.length > 0) { | |
var allData = { points:0, Games:0, Wins:0, Ties:0, Losses:0, Tags:0, Popped:0, Grabs:0, Drops:0, Captures:0, Hold:0, Prevent:0, Returns:0, Support:0, powerups:0, potentialPowerups:0, "Time Played":0 }; | |
$.each(data, function(key, value) { | |
allData.Games++; | |
if (value.outcome === 1) allData.Wins++; | |
if (value.outcome === 2) allData.Losses++; | |
if (value.outcome === 5) allData.Ties++; | |
allData.Tags += value.tags; | |
allData.Popped += value.pops; | |
allData.Grabs += value.grabs; | |
allData.Drops += value.drops; | |
allData.Captures += value.captures; | |
allData.Hold += value.hold; | |
allData.Prevent += value.prevent; | |
allData.Returns += value.returns; | |
allData.Support += value.support; | |
allData.powerups += value.powerups; | |
allData.potentialPowerups += value.potentialPowerups; | |
allData['Time Played'] += value.timePlayed; | |
}); | |
GM_setValue('allData', allData); | |
} | |
showData(); | |
} | |
function showData() { | |
var data = GM_getValue('allData', {}); | |
var winPercentage = (data.wins / (data.games - data.usa) * 100).toFixed(2); | |
var stats_R300 = $('#rolling').find('tbody'); | |
var stats_all = $('#all-stats').find('tbody'); | |
var off, stat, r300, all; | |
var winLossStats = ['Win %', 'Wins', 'Losses', 'Ties', 'Saves', 'Save %', 'Disconnects', 'Points']; //hide these rows if "hideWinLossStats" is true | |
$('.profile-stats .tab-list').append('<li id="SOI_StatsOff_Tab" data-target="#SOI_StatsOff"><a>Stats Off</a></li>'); | |
$('.profile-stats .tab-content').append('<div id="SOI_StatsOff" class="tab-pane"></div>'); | |
if (Object.keys(data).length) { | |
$('#SOI_StatsOff').append('<table id="SOI_StatsOff_Table" class="table table-stripped"></table></div>'); | |
$('#SOI_StatsOff_Table').append('<thead><tr><th style="text-align:center;">Statistic <span id="SOI_Reset" title="Reset/Delete All Stats Off Data">R</span></th><th style="text-align:center; border:1px solid;">Stats Off</th><th colspan="2" style="text-align:center; background:rgba(0,100,100,0.5); border:1px solid">Per Game</th><th colspan="2" style="text-align:center; background:rgba(0,100,100,0.5);">Per Minute</th></tr></thead>'); | |
$('#SOI_StatsOff_Table').append('<thead><tr><th colspan="2"></th><th style="text-align:center; background:rgba(0,100,100,0.5); border:1px solid">Off</th><th style="text-align:center; background:rgba(0,100,100,0.5); border:1px solid">R300</th><th style="text-align:center; background:rgba(0,100,100,0.5); border:1px solid">Off</th><th style="text-align:center; background:rgba(0,100,100,0.5); border:1px solid">R300</th></tr></thead>'); | |
$('#SOI_StatsOff_Table').append('<tbody></tbody>'); | |
var v1, v2, v3, v4, c1, c2, c3, c4; | |
var off_games = data.Games; | |
var off_time = data['Time Played']/60; | |
var r300_games = $(stats_R300).find('tr:eq(2)').find('td:eq(1)').text().trim(); | |
var r300_time = $(stats_R300).find('tr:eq(19)').find('td:eq(1)').data('seconds')/60; | |
$.each( $(stats_R300).find('tr'), function(key, value) { | |
stat = $(value).find('td:eq(0)').text().trim(); | |
off = (data.hasOwnProperty(stat) ? data[stat] : '-'); | |
c1=''; c2=''; c3=''; c4=''; | |
if ( !$(value).hasClass('hidden') && (!hideWinLossStats || (hideWinLossStats && winLossStats.indexOf(stat) < 0)) ) { | |
if (stat === 'Win %') { | |
off = (data.Wins / (data.Games) * 100).toFixed(1); | |
r300 = $(stats_R300).find('tr:eq('+key+')' ).find('td:eq(1)').text().replace('%', '').trim(); | |
if (off > r300) c1 = ' SOI_Highlight'; | |
if (off < r300) c2 = ' SOI_Highlight'; | |
$('#SOI_StatsOff_Table').append('<tr><td class="table-row-label">'+stat+'</td><td class="SOI_Off">'+off+'%</td><td class="SOI_PG'+c1+'">'+off+'%</td><td class="SOI_PG'+c2+'">'+r300+'%</td><td class="SOI_PM'+c1+'">'+off+'%</td><td class="SOI_PM'+c2+'">'+r300+'%</td></tr>'); | |
} else if (stat === 'Save %') { | |
r300 = $(stats_R300).find('tr:eq('+key+')' ).find('td:eq(1)').text().trim(); | |
$('#SOI_StatsOff_Table').append('<tr><td class="table-row-label">'+stat+'</td><td class="SOI_Off">-</td><td class="SOI_PG'+c1+'">-</td><td class="SOI_PG'+c2+'">'+r300+'</td><td class="SOI_PM'+c1+'">-</td><td class="SOI_PM'+c2+'">'+r300+'</td></tr>'); | |
} else if (stat === 'Power-up %') { | |
off = (data.powerups / data.potentialPowerups * 100).toFixed(1); | |
r300 = $(stats_R300).find('tr:eq('+key+')' ).find('td:eq(1)').text().replace('%', '').trim(); | |
if (off < r300) c1 = ' SOI_Highlight'; | |
if (off > r300) c2 = ' SOI_Highlight'; | |
$('#SOI_StatsOff_Table').append('<tr><td class="table-row-label">'+stat+'</td><td class="SOI_Off">'+off+'%</td><td class="SOI_PG'+c1+'">'+off+'%</td><td class="SOI_PG'+c2+'">'+r300+'%</td><td class="SOI_PM'+c1+'">'+off+'%</td><td class="SOI_PM'+c2+'">'+r300+'%</td></tr>'); | |
} else if ((stat === 'Hold') || (stat === 'Prevent') || (stat === 'Time Played')) { | |
r300 = $(stats_R300).find('tr:eq('+key+')' ).find('td:eq(1)').data('seconds'); | |
v1 = Math.round(off/off_games * 1e3) / 1e3; | |
v2 = Math.round(r300/r300_games * 1e3) / 1e3; | |
v3 = Math.round(off/off_time * 1e3) / 1e3; | |
v4 = Math.round(r300/r300_time * 1e3) / 1e3; | |
if (v1 > v2) c1 = ' SOI_Highlight'; | |
if (v1 < v2) c2 = ' SOI_Highlight'; | |
if (v3 > v4) c3 = ' SOI_Highlight'; | |
if (v3 < v4) c4 = ' SOI_Highlight'; | |
$('#SOI_StatsOff_Table').append('<tr><td class="table-row-label">'+stat+'</td><td class="SOI_Off">'+secondsToHMS(off)+'</td><td class="SOI_PG'+c1+'">'+secondsToHMS(v1)+'</td><td class="SOI_PG'+c2+'">'+secondsToHMS(v2)+'</td><td class="SOI_PM'+c3+'">'+secondsToHMS(v3)+'</td><td class="SOI_PM'+c4+'">'+secondsToHMS(v4)+'</td></tr>'); | |
} else { | |
r300 = parseInt($(stats_R300).find('tr:eq('+key+')' ).find('td:eq(1)').text().trim()); | |
if (off === '-') { | |
v1 = '-'; | |
v2 = (Math.round(r300/r300_games * 1e3) / 1e3).toFixed(3); | |
v3 = '-'; | |
v4 = (Math.round(r300/r300_time * 1e3) / 1e3).toFixed(3); | |
} else { | |
v1 = (Math.round(off/off_games * 1e3) / 1e3).toFixed(3); | |
v2 = (Math.round(r300/r300_games * 1e3) / 1e3).toFixed(3); | |
v3 = (Math.round(off/off_time * 1e3) / 1e3).toFixed(3); | |
v4 = (Math.round(r300/r300_time * 1e3) / 1e3).toFixed(3); | |
if ((stat === 'Popped') || (stat === 'Drops')) { //"negative" stats | |
if (v1 < v2) c1 = ' SOI_Highlight'; | |
if (v1 > v2) c2 = ' SOI_Highlight'; | |
if (v3 < v4) c3 = ' SOI_Highlight'; | |
if (v3 > v4) c4 = ' SOI_Highlight'; | |
} else { | |
if (v1 > v2) c1 = ' SOI_Highlight'; | |
if (v1 < v2) c2 = ' SOI_Highlight'; | |
if (v3 > v4) c3 = ' SOI_Highlight'; | |
if (v3 < v4) c4 = ' SOI_Highlight'; | |
} | |
} | |
$('#SOI_StatsOff_Table').append('<tr><td class="table-row-label">'+stat+'</td><td class="SOI_Off">'+off+'</td><td class="SOI_PG'+c1+'">'+v1+'</td><td class="SOI_PG'+c2+'">'+v2+'</td><td class="SOI_PM'+c3+'">'+v3+'</td><td class="SOI_PM'+c4+'">'+v4+'</td></tr>'); | |
} | |
} | |
}); | |
GM_addStyle('.profile .profile-stats .table { font-size:14px }'); | |
GM_addStyle('.SOI_Off { text-align:right; }'); | |
GM_addStyle('.SOI_PG { background:rgba(0,0,70,0.2); text-align:right; }'); | |
GM_addStyle('.SOI_PM { background:rgba(0,100,100,0.2); text-align:right; }'); | |
GM_addStyle('.SOI_Highlight { color:gold; }'); | |
GM_addStyle('#SOI_Reset { width:13px; float:right; font-size:11px; color:#fff; background:#aa0000; text-align:center; opacity:0.5; border-radius:4px; cursor:pointer; }'); | |
GM_addStyle('#SOI_Reset:hover { opacity:1 }'); | |
} else { | |
$('#SOI_StatsOff').append('No Data'); | |
} | |
$("#SOI_StatsOff_Tab").click(function() { | |
$(this).parents(".tab-list").find("li").removeClass("active"); | |
$(this).parents(".tab-list").parent().find(".tab-pane").removeClass("active"); | |
var e = $(this); | |
e.addClass("active"); | |
$(e.data("target")).addClass("active"); | |
}); | |
$("#SOI_Reset").click(function() { | |
var response = confirm("All your stats off data will be deleted and this page will reload.\n\nOK to continue?"); | |
if (response) { | |
GM_deleteValue('SOI_Selections'); | |
GM_deleteValue('allData'); | |
window.location.reload(); | |
} | |
}); | |
if (defaultToStatsOffTab) $("#SOI_StatsOff_Tab").trigger('click'); | |
//console.log(data); | |
} | |
tagpro.ready(function() { | |
if (PageLoc === 'ingame') { //in a game | |
var joinTime; | |
var gameData = {}; | |
var mapName='', mapAuthor='', mapType=''; | |
var pupsCount = 0; | |
var result = 0; | |
var groupPause = false; | |
if ((tagpro.group.socket) && (tagpro.group.socket.connected)) { | |
groupPause = true; | |
} | |
tagpro.socket.on('spectator', function(spectator) { | |
if (!spectator) { | |
joinTime = Date.now(); | |
} | |
}); | |
tagpro.socket.on('time', function(message) { | |
if (tagpro.state === 3) { //before the actual start | |
joinTime = Date.now(); | |
} else if (tagpro.state === 1) { //game has started | |
if (!joinTime) { | |
joinTime = Date.now(); //time we joined (mid-game) | |
} | |
} | |
}); | |
tagpro.socket.on('map', function(data) { | |
mapName = data.info.name; | |
mapAuthor = data.info.author; | |
setTimeout(function() { | |
for (var i=0; i<tagpro.map.length; i++) { //find the flags which will tell us if it's a CTF or NF map... | |
for (var j=0; j<tagpro.map[i].length; j++) { | |
if (tagpro.map[i][j] == 16 || (tagpro.map[i][j] == 16.1)) { //yellow flag found | |
mapType = 2; | |
} else if ((tagpro.map[i][j] == 3) || (tagpro.map[i][j] == 3.1) || (tagpro.map[i][j] == 4) || (tagpro.map[i][j] == 4.1)) { //red or blue flag found | |
mapType = 1; | |
} else if ((tagpro.map[i][j] == 6) || (tagpro.map[i][j] == 6.1) || (tagpro.map[i][j] == 6.2) || (tagpro.map[i][j] == 6.3) || (tagpro.map[i][j] == 6.4)) { //counts the pups so we can work out potential pups later | |
pupsCount++; | |
} | |
} | |
} | |
}, 100); | |
if (showStatsCheckboxInGame) { | |
setTimeout(function() { | |
var settings = GM_getValue('settings', {}); | |
if (settings.hasOwnProperty('stats')) { | |
if (checkboxOnScoreboard) { | |
$('#optionsName').append('<div style="font-size:12px;"><span style="color:'+(settings.stats ? 'chartreuse' : 'red')+'">Stats: ' + (settings.stats ? 'ON' : 'OFF') +'</span> <label> [<span id="SOI_PostMsg">Next Game:</span> <input id="SOI_StatsOnOff" type="checkbox"'+(settings.stats ? ' checked' : '')+' style="width:auto">]</label></div>'); | |
} else { | |
$('#exit').after('<div style="position:absolute; top:60px; left:5px; font-size:10px;"><span style="color:'+(settings.stats ? 'chartreuse' : 'red')+'">Stats: ' + (settings.stats ? 'ON' : 'OFF') +'</span> <label> [<span id="SOI_PostMsg">Next Game:</span> <input id="SOI_StatsOnOff" type="checkbox"'+(settings.stats ? ' checked' : '')+'>]</label></div>'); | |
} | |
$('#SOI_StatsOnOff').on('click', function() { | |
settings.stats = this.checked; | |
$('#SOI_StatsOnOff').prop('disabled', true); | |
$('#SOI_PostMsg').text('Wait').css('color', 'white'); | |
postSettings(this.checked, function(success) { | |
if (success) { | |
$('#SOI_PostMsg').text('Done').css('color', 'yellow'); | |
} else { | |
$('#SOI_PostMsg').text('Failed').css('color', 'red'); | |
} | |
}); | |
setTimeout(function() { | |
$('#SOI_StatsOnOff').prop('disabled', false); | |
$('#SOI_PostMsg').text('Next Game').css('color', 'inherit'); | |
}, 3000); | |
}); | |
} | |
}, 2500); | |
} | |
}); | |
tagpro.socket.on('end', function(data) { | |
var settings = GM_getValue('settings', {}); | |
if ((alwaysSaveStatsOffGames || (!alwaysSaveStatsOffGames && !settings.stats)) && (!tagpro.spectator) && !groupPause) { //(!tagpro.settings.stats) && | |
var fullTime = Date.parse(tagpro.gameEndsAt); //expected end of game time after 12 mins | |
var endTime = Date.now(); //actual end of game time | |
var startTime = fullTime - 12 * 60 * 1000; //start of game time | |
var fullGameLength = (endTime-startTime)/1000; //how long the whole game lasted (with or without us) | |
var playedGameLength = (endTime-joinTime)/1000; //how long we played for | |
var playerCount = 0; | |
for (var playerId in tagpro.players) { | |
playerCount++; | |
} | |
if ((joinTime+30000 < endTime) && (playerCount >= 4) && (playerCount <= 8) ) { //check we didn't join in the last 30 seconds of the game, and there was between 4-8 players | |
gameData.mapName = mapName; | |
gameData.mapAuthor = mapAuthor; | |
gameData.gameMode = mapType; | |
gameData.playerCount = playerCount; | |
if (tagpro.serverHost.indexOf('tagpro-') === 0) { | |
gameData.serverName = tagpro.serverHost.replace('tagpro-', '').replace('.koalabeast.com', ''); | |
} else { | |
gameData.serverName = ''; | |
} | |
gameData.played = new Date(joinTime).toISOString(); | |
gameData.timePlayed = playedGameLength; | |
gameData.fullGameLength = fullGameLength; | |
gameData.team = tagpro.players[tagpro.playerId].team; | |
gameData.redScore = tagpro.score.r; | |
gameData.blueScore = tagpro.score.b; | |
gameData.saved = 0; | |
if (data.winner === 'tie') { | |
gameData.outcome = 5; //tie | |
} else if ( ((data.winner === 'red') && (tagpro.players[tagpro.playerId].team === 1)) || ((data.winner === 'blue') && (tagpro.players[tagpro.playerId].team === 2)) ) { | |
gameData.outcome = 1; //win | |
} else if ( ((data.winner === 'red') && (tagpro.players[tagpro.playerId].team === 2)) || ((data.winner === 'blue') && (tagpro.players[tagpro.playerId].team === 1)) ) { | |
gameData.outcome = 2; //loss | |
} else { //probably an event, which we won't record... | |
return false; | |
} | |
gameData.tags = tagpro.players[tagpro.playerId]["s-tags"]; | |
gameData.pops = tagpro.players[tagpro.playerId]["s-pops"]; | |
gameData.grabs = tagpro.players[tagpro.playerId]["s-grabs"]; | |
gameData.drops = tagpro.players[tagpro.playerId]["s-drops"]; | |
gameData.hold = tagpro.players[tagpro.playerId]["s-hold"]; | |
gameData.captures = tagpro.players[tagpro.playerId]["s-captures"]; | |
gameData.prevent = tagpro.players[tagpro.playerId]["s-prevent"]; | |
gameData.returns = tagpro.players[tagpro.playerId]["s-returns"]; | |
gameData.support = tagpro.players[tagpro.playerId]["s-support"]; | |
gameData.powerups = tagpro.players[tagpro.playerId]["s-powerups"]; | |
gameData.score = tagpro.players[tagpro.playerId].score; | |
gameData.points = tagpro.players[tagpro.playerId].points; | |
gameData.potentialPowerups = pupsCount * Math.ceil(playedGameLength / 60); //is this right??? | |
//save scoreboard... | |
gameData.playersData = []; | |
for (playerId in tagpro.players) { | |
gameData.playersData.push({ | |
name:tagpro.players[playerId].name, | |
self:(tagpro.playerId === tagpro.players[playerId].id ? true : false), | |
id:tagpro.players[playerId].id, | |
team:tagpro.players[playerId].team, | |
auth:tagpro.players[playerId].auth, | |
tags:tagpro.players[playerId]["s-tags"], | |
pops:tagpro.players[playerId]["s-pops"], | |
grabs:tagpro.players[playerId]["s-grabs"], | |
drops:tagpro.players[playerId]["s-drops"], | |
hold:tagpro.players[playerId]["s-hold"], | |
captures:tagpro.players[playerId]["s-captures"], | |
prevent:tagpro.players[playerId]["s-prevent"], | |
returns:tagpro.players[playerId]["s-returns"], | |
support:tagpro.players[playerId]["s-support"], | |
powerups:tagpro.players[playerId]["s-powerups"], | |
score:tagpro.players[playerId].score, | |
points:tagpro.players[playerId].points | |
}); | |
} | |
gameData.playersData.sort(function(a, b) { | |
return (b.score - a.score ? b.score - a.score : a.id - b.id); | |
}); | |
SOI_Selections.SavedGames.value.push(gameData); | |
GM_setValue('SOI_Selections', SOI_Selections); | |
} | |
} | |
}); | |
} | |
}); | |
SOI_Selections = $.extend(true, {}, options, GM_getValue('SOI_Selections', options)); | |
GM_setValue('SOI_Selections', SOI_Selections); | |
function getSettings() { | |
var profileId = GM_getValue('profileId', ''); | |
if (profileId) { | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: 'http://' + window.location.hostname + '/profile/' + profileId, | |
onload: function(xhr) { | |
if ((xhr.readyState === 4) && (xhr.status === 200)) { | |
if ($(xhr.response).find('input#stats').length) { //will only be present if it's our profile | |
var $settings = $(xhr.response).find('#settings input:not(.js-cookie)'); | |
var settings = {}; | |
$.each($settings, function(k, v) { | |
var value = (v.type === 'checkbox' ? v.checked : v.value); | |
settings[v.name] = value; | |
}); | |
GM_setValue('settings', settings); | |
if (showStatsCheckboxOnNavBar) { | |
$('#site-nav').find('ul').append('<li><a><label style="margin:0; font-weight:normal"><input id="SOI_StatsOnOff" type="checkbox" style="margin:0"'+(settings.stats ? ' checked' : '')+'> <span id="SOI_PostMsg">Stats</span></label></a></li>'); | |
$('#SOI_StatsOnOff').on('click', function() { | |
$('#SOI_StatsOnOff').prop('disabled', true); | |
$('#SOI_PostMsg').text('Wait').css('color', 'white'); | |
postSettings(this.checked, function(success) { | |
if (success) { | |
$('#SOI_PostMsg').text('Done').css('color', 'yellow'); | |
} else { | |
$('#SOI_PostMsg').text('Failed').css('color', 'red'); | |
} | |
}); | |
setTimeout(function() { | |
$('#SOI_StatsOnOff').prop('disabled', false); | |
$('#SOI_PostMsg').text('Stats').css('color', 'inherit'); | |
}, 4000); | |
}); | |
} | |
} else { | |
GM_setValue('settings', {}); | |
} | |
} | |
}, | |
onerror: function(xhr) { | |
console.log('get settings error:', xhr); | |
} | |
}); | |
} | |
} | |
function postSettings(statsOn, callback) { | |
var settings = GM_getValue('settings'); | |
var settings_str = ''; | |
settings.stats = statsOn; | |
$.each(settings, function(k, v) { | |
settings_str += k + '=' + v + '&'; | |
}); | |
settings_str = settings_str.substr(0, settings_str.length-1); | |
GM_xmlhttpRequest({ | |
method: "POST", | |
data: settings_str, | |
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | |
url: 'http://' + window.location.hostname + '/profile/update', | |
onload: function(xhr) { | |
if ((xhr.readyState === 4) && (xhr.status === 200)) { | |
var data = JSON.parse(xhr.responseText); | |
if (data.success === true) { | |
GM_setValue('settings', settings); | |
$('#SOI_StatsOnOff').prop('checked', settings.stats); | |
$('#stats').prop('checked', settings.stats); | |
callback(true); | |
} else { | |
callback(false); | |
} | |
} | |
}, | |
onerror: function(xhr) { | |
console.log('post settings error:', xhr); | |
callback(false); | |
} | |
}); | |
} | |
if (PageLoc === 'profile') { | |
if ($('input#stats').length) { //will only be available if it's our profile | |
var profileId = document.URL.substr(document.URL.lastIndexOf('/profile/') + 9, 24); | |
GM_setValue('profileId', profileId); | |
} | |
loadData(); | |
} else if (PageLoc === 'server') { //this is just so it will show if you're using the "Homepage Redesigner" script. Nothing will happen if you're not. | |
(function init(initTime) { //we need a delay until the stats are loaded... | |
if (!$('#HR_Stats_Inner').length) { | |
if (Date.now() - initTime > 10000) return; | |
return setTimeout(init, 250, initTime); | |
} | |
loadData(); | |
})(Date.now()); | |
getSettings(); | |
} else if ( (PageLoc === 'joining') || (PageLoc === 'boards') || (PageLoc === 'maps') ) { //(PageLoc !== 'ingame') && (PageLoc !== 'profileNotOurs') | |
getSettings(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment