Advertisement
Guest User

Untitled

a guest
May 20th, 2012
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.08 KB | None | 0 0
  1. // ==UserScript==
  2. // @name QL Gametype Switcher
  3. // @version 1.4
  4. // @include http://*.quakelive.com/*
  5. // @exclude http://*.quakelive.com/forum*
  6. // @description Script that makes QuakeLive navigation between game types faster and user friendly.
  7. // @author aiken
  8. // @updateURL https://userscripts.org/scripts/source/120117.meta.js
  9. // ==/UserScript==
  10.  
  11. // Set up some stuff for user script updating
  12. var SCRIPT_NAME = "QL Gametype Switcher"
  13. , SCRIPT_VER = "1.4";
  14. GM_updatingEnabled = "GM_updatingEnabled" in window ? GM_updatingEnabled : false;
  15.  
  16. /**
  17. * Don't bother if Quake Live is down for maintenance or we're not the top frame
  18. * Note: Copied from wn's QL New Alt Browser script: http://userscripts.org/scripts/show/73076
  19. */
  20. if (new RegExp("offline", "i").test(document.title)
  21. || unsafeWindow.self != unsafeWindow.top)
  22. return;
  23.  
  24. // Script initializing function
  25. function QL_GTS_Init(unsafeWindow) {
  26.  
  27. /**
  28. * GM_ API emulation for Chrome
  29. * 2009, 2010 James Campos
  30. * cc-by-3.0; http://creativecommons.org/licenses/by/3.0/
  31. */
  32. if (typeof GM_getValue == "undefined") {
  33. GM_getValue = function(name, defaultValue) {
  34. var value = localStorage.getItem(name);
  35. if (!value)
  36. return defaultValue;
  37. var type = value[0];
  38. value = value.substring(1);
  39. switch (type) {
  40. case 'b':
  41. return value == 'true';
  42. case 'n':
  43. return Number(value);
  44. default:
  45. return value;
  46. }
  47. }
  48. GM_setValue = function(name, value) {
  49. value = (typeof value)[0] + value;
  50. localStorage.setItem(name, value);
  51. }
  52. GM_registerMenuCommand = function() {};
  53.  
  54.  
  55. };
  56. if (typeof GM_addStyle == "undefined") {
  57. GM_addStyle = function(css) {
  58. var style = document.createElement('style');
  59. style.textContent = css;
  60. document.getElementsByTagName('head')[0].appendChild(style);
  61. }
  62. }
  63.  
  64. // Variables from Quake Live site that will be accessed
  65. var $ = unsafeWindow.jQuery;
  66. var quakelive = unsafeWindow.quakelive;
  67. var qlPrompt = unsafeWindow.qlPrompt;
  68.  
  69. // Add style classes used in the script
  70. GM_addStyle("a.qfLink {color: black; text-decoration: none;} "+
  71. "a.qfLink img { margin: 0 2px; vertical-align: middle; }"+
  72. "a.qfSvTypeLink {display: none; color: grey; font-size: 11px; font-weight: bold; text-decoration: none; margin-left:250px; margin-top: 4px; float: left;}"+
  73. "a.qfSvTypeLink:hover { text-decoration: underline; }"+
  74. "div.quickFilter {margin-top: 8px; margin-left: 5px; padding: 2px; word-spacing: 3px; color: black; border-bottom: 1px solid grey; }");
  75.  
  76. //window.addEventListener("message", QL_GTS_messageHandler, false);
  77.  
  78. // Attach to QL function that initializes the whole page
  79. var oldHomeShowContent = quakelive.ShowContent;
  80. quakelive.ShowContent = function (v) {
  81. oldHomeShowContent(v);
  82.  
  83. QL_GTS = {
  84. // Local internal variables
  85. MAX_GAMETYPE_NUM: 24, // used for validation
  86. ID: 0,
  87. IMAGE: 1,
  88. NAME: 2,
  89. TITLE: 3,
  90. // Defined game types (used fields are: gametype number, icon name from QL repository, displayed text, title hint)
  91. GAME_TYPES: [ [0, 'ffa', 'All', 'All Game Types'],
  92. [12, 'tdm', 'Ranked', 'Any Ranked Game'],
  93. [1, 'tdm', 'Team', 'Any Team Game'],
  94. [13, 'tdm', 'Unranked', 'Any Unranked Game'],
  95. [7, 'duel', 'Duel', 'One On One'],
  96. [6, 'tdm', 'TDM', 'Team Deathmatch'],
  97. [3, 'ctf', 'CTF', 'Capture The Flag'],
  98. [4, 'ca', 'CA', 'Clan Arena'],
  99. [5, 'ft', 'FT', 'Freeze Tag'],
  100. [16, 'fctf', '1CTF', '1-Flag CTF'],
  101. [18, 'ad', 'A&D', 'Attack & Defend'],
  102. [15, 'dom', 'DOM', 'Domination'],
  103. [2, 'ffa', 'FFA', 'Free For All'],
  104. [17, 'harvester', 'HAR', 'Harvester'],
  105. [19, 'rr', 'RR', 'Red Rover'],
  106. [8, 'ffa', 'iFFA', 'Instagib FFA'],
  107. [21, 'fctf', 'i1CTF', 'Insta1FCTF'],
  108. [23, 'ad', 'iA&D', 'InstaA&D'],
  109. [14, 'ca', 'iCA', 'InstaCA'],
  110. [9, 'ctf', 'iCTF', 'InstaCTF'],
  111. [20, 'dom', 'iDOM', 'InstaDOM'],
  112. [10, 'ft', 'iFT', 'InstaFreeze'],
  113. [22, 'harvester', 'iHAR', 'InstaHAR'],
  114. [24, 'rr', 'iRR', 'InstaRR'],
  115. [11, 'tdm', 'iTDM', 'InstaTDM'],
  116. ],
  117.  
  118. // Helper function to check if game type is enabled in game type bitmap variable
  119. isGameTypeEnabled: function (bitmap, gameTypeNum) {
  120. return (bitmap & (1 << gameTypeNum)) > 0;
  121. },
  122. // Fills quick filter bar with available game type links
  123. fillModesBar: function () {
  124. var filtersBitmap = localStorage.getItem("ql_gts_filters_bitmap", 0xFFFFFFFF);
  125. var quickFilterContent = "";
  126. // Dynamically create links
  127. for (var i in QL_GTS.GAME_TYPES) {
  128. // Check if this game type is enabled in the settings
  129. if (QL_GTS.isGameTypeEnabled(filtersBitmap, QL_GTS.GAME_TYPES[i][QL_GTS.ID])) {
  130. quickFilterContent += '<a id="'+QL_GTS.GAME_TYPES[i][QL_GTS.ID]+'" title="'+QL_GTS.GAME_TYPES[i][3]+'" class="qfLink" href="javascript:;"><img src='+quakelive.resource('/images/gametypes/xsm/'+QL_GTS.GAME_TYPES[i][QL_GTS.IMAGE]+'.png')+' />'+QL_GTS.GAME_TYPES[i][QL_GTS.NAME]+'</a> ';
  131. }
  132. }
  133.  
  134. // Replace content of quick filter bar
  135. $('#quickFilterGTList').html(quickFilterContent);
  136.  
  137. // Attach action for game type links
  138. $('.qfLink').click(function() {
  139. $("#ctrl_filter_gametype").val(this.id).attr("selected", "selected");
  140. $("#ctrl_filter_gametype").change();
  141. quakelive.mod_home.SaveBrowserFilter();
  142. });
  143. return false;
  144. },
  145.  
  146. // Function that display QL Prompt asking for users customization settings
  147. configureFilters: function () {
  148. var filtersBitmap = localStorage.getItem("ql_gts_filters_bitmap", 0xFFFFFFFF);
  149. var promptContent = 'Select game modes that you want to display on the quick filter bar:<br /><br />';
  150.  
  151. for (var i in QL_GTS.GAME_TYPES) {
  152. var chboxIdentifier = 'ql_gts_chb_'+QL_GTS.GAME_TYPES[i][QL_GTS.ID];
  153. var checkedAttr = QL_GTS.isGameTypeEnabled(filtersBitmap, QL_GTS.GAME_TYPES[i][QL_GTS.ID]) ? 'checked="checked"' : '';
  154. promptContent += '<input type="checkbox" id="'+chboxIdentifier+'" value="'+QL_GTS.GAME_TYPES[i][QL_GTS.ID]+'" '+checkedAttr+' /> <label for="'+chboxIdentifier+'"><img src='+quakelive.resource('/images/gametypes/xsm/'+QL_GTS.GAME_TYPES[i][QL_GTS.IMAGE]+'.png')+' />'+QL_GTS.GAME_TYPES[i][QL_GTS.NAME]+'</label>&nbsp;&nbsp;&nbsp;';
  155. }
  156. promptContent += "<br /><br /><a id='ql_gts_selectAll' href='javascript:;'>Select All</a> | <a id='ql_gts_selectNone' href='javascript:;'>Select None</a><br />";
  157.  
  158. qlPrompt({
  159. customWidth: 850,
  160. title: "Customize Quick Game Type Filter",
  161. body: promptContent,
  162. input: false,
  163. inputReadOnly: false,
  164. alert: false,
  165. ok: function () {
  166. // Assume empty bitmap like nothing is selected
  167. var filtersBitmap = 0;
  168. $('#prompt input:checkbox:checked').each(function(i) {
  169. var gameTypeNum = parseInt(this.value);
  170. if (gameTypeNum >= 0 && gameTypeNum <= QL_GTS.MAX_GAMETYPE_NUM) {
  171. // Set 1 on the bit number equal to game type number
  172. filtersBitmap |= 1 << gameTypeNum;
  173. }
  174. });
  175. $("#prompt").jqmHide();
  176.  
  177. // Save bitmap in local storage for future
  178. localStorage.setItem("ql_gts_filters_bitmap", filtersBitmap);
  179.  
  180. // Refresh game modes list on the quick filter bar
  181. QL_GTS.fillModesBar();
  182. }
  183. });
  184.  
  185. // Attach actions to Select All and Select None links on the customization pop-up
  186. $('#ql_gts_selectAll').click(function () {
  187. $('#prompt input:checkbox').attr('checked', 'checked');
  188. });
  189. $('#ql_gts_selectNone').click(function () {
  190. $('#prompt input:checkbox').attr('checked', '');
  191. });
  192.  
  193. return false;
  194. },
  195.  
  196. // Function that is called to initialize QL_GTS
  197. initialize: function() {
  198. // Attach action to customize button that opens settings pop-up
  199. $('#qfCustomize').click(QL_GTS.configureFilters);
  200.  
  201. // Fill game modes list on the quick filter bar
  202. QL_GTS.fillModesBar();
  203. }
  204. }
  205.  
  206. // Add private and public matches quick switch (both are not displayed by default)
  207. $('#matchlist_header').prepend('<a id="qfSvShowPrivate" class="qfSvTypeLink" href="javascript:;" >Private Matches</a>'+
  208. '<a id="qfSvShowPublic" class="qfSvTypeLink" href="javascript:;" >Public Matches</a>');
  209. // Add space with quick filter above the server list
  210. $('#matchlist_header').append('<div class="quickFilter"> \
  211. <b>Mode:</b> <span id="quickFilterGTList"></span>'+
  212. '<span style="float: right; margin: 0 2px;"><a href="javascript:;" id="qfCustomize"><img title="Customize Quick Filter" src='+quakelive.resource('/images/modules/clans/ranks/rank_1.png')+' /></a></span>'+
  213. '</div>');
  214. $('#matchlist_header').css('height', 30);
  215. var divHeight = 24;
  216. $('#qlv_postlogin_matches').css('margin-top', divHeight);
  217. $('#qlv_postlogin_matches').css('height', $('#qlv_postlogin_matches').height() - divHeight + 'px');
  218.  
  219. // Attach action to link that changes server type to private
  220. $('#qfSvShowPrivate').click(function() {
  221. $("input:radio[name=private][value=1]").trigger('click');
  222. $("input:radio[name=private][value=1]").trigger('click'); // need two clicks to trigger action, dunno why
  223. quakelive.mod_home.SaveBrowserFilter();
  224. });
  225. // Attach action to link that changes server type to public
  226. $('#qfSvShowPublic').click(function() {
  227. $("input:radio[name=private][value=0]").trigger('click');
  228. $("input:radio[name=private][value=0]").trigger('click'); // need two clicks to trigger action, dunno why
  229. quakelive.mod_home.SaveBrowserFilter();
  230. });
  231.  
  232. QL_GTS.initialize();
  233. };
  234.  
  235. // Wrap around save browser settings success function to disable toggling of customize bar
  236. var oldSaveSuccess = quakelive.mod_home.SaveBrowserFilter_Success;
  237. quakelive.mod_home.SaveBrowserFilter_Success = function() {
  238. // Save handle to old toggle function and replace with empty one
  239. var tmpToggleFunc = quakelive.mod_home.ToggleFilterBar;
  240. quakelive.mod_home.ToggleFilterBar = function () {};
  241. oldSaveSuccess();
  242. // Restore normal toggle function
  243. quakelive.mod_home.ToggleFilterBar = tmpToggleFunc;
  244. };
  245.  
  246. // Wrap around refresh filter UI function to show proper action of switching between server types
  247. var oldRefreshFilter = quakelive.mod_home.UI_RefreshFilter;
  248. quakelive.mod_home.UI_RefreshFilter = function() {
  249. // This function is called internally by QL so we can listen to match type changes
  250. oldRefreshFilter();
  251. var mlHeaderClassName = $("#matchlist_header").attr("class");
  252. if ("matchlist_header_public" == mlHeaderClassName) {
  253. $('#qfSvShowPublic').css('display', 'none');
  254. $('#qfSvShowPrivate').css('display', 'inline');
  255. } else {
  256. $('#qfSvShowPublic').css('display', 'inline');
  257. $('#qfSvShowPrivate').css('display', 'none');
  258. }
  259. };
  260. }
  261.  
  262. /**
  263. * Use an auto-update script if integrated updating isn't enabled
  264. * Based on http://userscripts.org/scripts/show/38017
  265. * and http://userscripts.org/scripts/show/114449
  266. * NOTE from 2nd author: Modified to remove some checks, since we do that above.
  267. * Also added the new version number to the upgrade prompt
  268. * and custom messages for Chrome users (requires a manual update).
  269. */
  270. if (!GM_updatingEnabled && "undefined" != typeof(GM_xmlhttpRequest)) {
  271. var AutoUpdater_120117={id:120117,days:1,name:SCRIPT_NAME,version:SCRIPT_VER,time:new Date().getTime(),call:function(response,secure){GM_xmlhttpRequest({method:"GET",url:"http"+(secure?"s":"")+"://userscripts.org/scripts/source/"+this.id+".meta.js",onload:function(xpr){AutoUpdater_120117.compare(xpr,response)},onerror:function(xpr){if(secure){AutoUpdater_120117.call(response,false)}}})},enable:function(){GM_registerMenuCommand(this.name+": Enable updates",function(){GM_setValue("updated_120117",new Date().getTime()+"");AutoUpdater_120117.call(true,true)})},compareVersion:function(r_version,l_version){var r_parts=r_version.split("."),l_parts=l_version.split("."),r_len=r_parts.length,l_len=l_parts.length,r=l=0;for(var i=0,len=(r_len>l_len?r_len:l_len);i<len&&r==l;++i){r=+(r_parts[i]||"0");l=+(l_parts[i]||"0")}return(r!==l)?r>l:false},compare:function(xpr,response){this.xversion=/\/\/\s*@version\s+(.+)\s*\n/i.exec(xpr.responseText);this.xname=/\/\/\s*@name\s+(.+)\s*\n/i.exec(xpr.responseText);if((this.xversion)&&(this.xname[1]==this.name)){this.xversion=this.xversion[1];this.xname=this.xname[1]}else{if((xpr.responseText.match("the page you requested doesn't exist"))||(this.xname[1]!=this.name)){GM_setValue("updated_120117","off")}return false}var updated=this.compareVersion(this.xversion,this.version);if(updated&&confirm("A new version of "+this.xname+" is available.\nDo you wish to install the latest version ("+this.xversion+")?")){var path="http://userscripts.org/scripts/source/"+this.id+".user.js";if(window.chrome){prompt("This script can't be updated automatically in Chrome.\nPlease uninstall the old version, and navigate to the URL provided below.",path)}else{try{window.parent.location.href=path}catch(e){}}}else{if(this.xversion&&updated){if(confirm("Do you want to turn off auto updating for this script?")){GM_setValue("updated_120117","off");this.enable();if(window.chrome){alert("You will need to reinstall this script to enable auto-updating.")}else{alert("Automatic updates can be re-enabled for this script from the User Script Commands submenu.")}}}else{if(response){alert("No updates available for "+this.name)}}}},check:function(){if(GM_getValue("updated_120117",0)=="off"){this.enable()}else{if(+this.time>(+GM_getValue("updated_120117",0)+1000*60*60*24*this.days)){GM_setValue("updated_120117",this.time+"");this.call(false,true)}GM_registerMenuCommand(this.name+": Check for updates",function(){GM_setValue("updated_120117",new Date().getTime()+"");AutoUpdater_120117.call(true,true)})}}};AutoUpdater_120117.check();
  272. }
  273.  
  274. //if (new RegExp('Firefox/\\d', 'i').test(navigator.userAgent)) { // this detects QL Prism like Chrome, should rather like Firefox
  275. if (!window.chrome) {
  276. //Firefox
  277. QL_GTS_Init(unsafeWindow);
  278. } else {
  279. //Chrome
  280. var scriptNode = document.createElement("script");
  281. scriptNode.setAttribute("type", "text/javascript");
  282. scriptNode.text = "(" + QL_GTS_Init.toString() + ")(window);";
  283. document.body.appendChild(scriptNode);
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement