Chat Resizer

Automatically resizes the chat to a specified value.

  1. // ==UserScript==
  2. // @name Chat Resizer
  3. // @namespace tag://kongregate
  4. // @description Automatically resizes the chat to a specified value.
  5. // @author Ventero
  6. // @include http://www.kongregate.com/games/*
  7. // @date 15.09.2012
  8. // require http://kong.ventero.de/updates/70040.js
  9. // @version 1.2.4
  10. // @license MIT license
  11. // ==/UserScript==
  12.  
  13. // Written by Ventero (http://www.kongregate.com/accounts/Ventero) 05/12/09
  14. // Copyright (c) 2010-2012 Ventero, licensed under MIT/X11 license
  15. // http://www.opensource.org/licenses/mit-license.php
  16.  
  17. // Default dimensions of chat
  18. var defaultWidth = 500;
  19. var defaultHeight = 600;
  20.  
  21. // How much space should be left on the left/right border
  22. var spaceLeft = 200;
  23.  
  24. // Set to false if game should line up with bottom of chat, not center
  25. var center = true;
  26.  
  27. // Sets the height of the user list
  28. var userListHeight = 100;
  29.  
  30. var dom = (typeof unsafeWindow === "undefined"?window:unsafeWindow);
  31. if(!document.getElementById("game")) return;
  32. if(!dom.holodeck){
  33. var s = document.createElement("div");
  34. s.setAttribute("onclick","return window");
  35. dom = s.onclick();
  36. }
  37.  
  38. function d(c){
  39. return document.getElementById(c);
  40. }
  41.  
  42. function c(c){
  43. return document.getElementsByClassName(c);
  44. }
  45.  
  46. function $(c){
  47. return document.querySelector(c);
  48. }
  49.  
  50. function $A(c){
  51. return [].slice.call(c);
  52. }
  53.  
  54. function setWidth(width){
  55. window._currentChatWidth = width;
  56. var gameWidth = parseInt(d("game").style.width, 10);
  57. d("maingame").style.width = (gameWidth + 3 + width) + "px";
  58. d("maingamecontent").style.width = (gameWidth + 3 + width) + "px";
  59. d("flashframecontent").style.width = (gameWidth + 3 + width) + "px";
  60. d("chat_container").style.width = width + "px";
  61. d('chat_window_spinner').style.right = width/2 - 38 + "px";
  62. if(d('high_scores_spinner'))
  63. d('high_scores_spinner').style.right = width/2 - 38 + "px";
  64. var ui = d("kong_game_ui");
  65. z = ui.childNodes
  66. for(i=0;i<z.length;i++){
  67. if(z[i].tagName == "DIV")
  68. z[i].style.width = (width - 17) + "px";
  69. }
  70. $A(ui.querySelectorAll("textarea.chat_input")).forEach(function(el){
  71. el.style.width = (width - 21) + "px";
  72. });
  73. }
  74.  
  75. function setHeight(height, userListHeight, center){
  76. if(!userListHeight) userListHeight = 100;
  77.  
  78. window._currentChatHeight = height;
  79. window._currentChatUserlistHeight = userListHeight;
  80.  
  81. var quicklinksHeight = d('quicklinks') ? d('quicklinks').parentNode.clientHeight : 26;
  82. var maintabHeight = d('main_tab_set').clientHeight;
  83.  
  84. var tabPaneHeight = height - 16;
  85. var mainHeight = height + quicklinksHeight + maintabHeight;
  86. var gameHeight = parseInt(d('game').style.height, 10);
  87.  
  88. d("maingame").style.height = mainHeight + "px";
  89. d("maingamecontent").style.height = mainHeight + "px";
  90. d("flashframecontent").style.height = mainHeight + "px";
  91. d("chat_container").style.height = (height + maintabHeight) + "px";
  92. d("user_mini_profile_container").style.height = (height - 65) + "px";
  93. d("user_mini_profile").style.height = (height - 65) + "px";
  94.  
  95. var messageWindows = c("chat_message_window");
  96. for(var i = 0; i < messageWindows.length; i++){
  97. messageWindows[i].style.height = (tabPaneHeight - userListHeight - 93)+"px"; // 93 = roomname, users in room etc.
  98. }
  99.  
  100. var usersInRoom = c("chat_tabpane users_in_room");
  101. for(i = 0; i < usersInRoom.length; i++){
  102. usersInRoom[i].style.height = userListHeight + "px";
  103. }
  104.  
  105. var roomsList = c("rooms_list");
  106. for(i = 0; i < roomsList.length; i++){
  107. roomsList[i].style.height = (height - 79)+"px";
  108. }
  109.  
  110. z = d("kong_game_ui").childNodes;
  111. for(i=0;i<z.length;i++){
  112. if(z[i].nodeName=="DIV"){
  113. z[i].style.height = tabPaneHeight + "px";
  114. }
  115. }
  116. if(center != -1 && center !== undefined)
  117. centerGame(center);
  118. }
  119.  
  120. function centerGame(center){
  121. window._currentGameCentered = center;
  122. if(center){
  123. var gameHeight = parseInt(d('game').style.height, 10);
  124. var mainHeight = parseInt(d("maingame").style.height, 10);
  125. d('game').style.top = (mainHeight - gameHeight)/2+"px"
  126. d('game').style.position = "relative";
  127. }else{
  128. d('game').style.bottom = "0px";
  129. d('game').style.top = "";
  130. d('game').style.position = "absolute";
  131. }
  132. }
  133.  
  134. function initalize_chat_resize(){
  135. if(!d('maingamecontent')) return;
  136. var initialOffsetTop = d('maingamecontent').offsetTop + d('chat_tab_pane').offsetTop;
  137. var initialOffsetLeft = d('maingamecontent').offsetLeft + d('chat_tab_pane').offsetLeft;
  138. var minimumHeight = parseInt(d("game").style.height, 10) - parseInt(d('main_tab_set').clientHeight, 10) - 16;
  139. var minimumWidth = 300;
  140.  
  141. if(dom.holodeck){
  142. var holodeck = dom.holodeck;
  143. holodeck.addChatCommand("size", function(l, n){
  144. var m = n.match(/^\/\S+\s+(\S+)/);
  145. var o = n.match(/^\/\S+\s+(\d+)\s+(\d+)(?:\s+(\d+))?/)
  146.  
  147. if(m && m[1] == "reset"){
  148. l.activeDialogue().kongBotMessage("Resetting size for this game to defaults.");
  149. window.setTimeout(function(){GM_deleteValue("kong_resize_"+location.pathname)}, 0);
  150. setWidth(window._defaultChatWidth);
  151. setHeight(window._defaultChatHeight, window._defaultUserlistHeight, window._currentGameCentered);
  152.  
  153. return false;
  154. } else if(m && m[1] == "show"){
  155. l.activeDialogue().kongBotMessage("Current chat size: width: " + window._currentChatWidth + "px, height: " + window._currentChatHeight + "px, userlist-height: " + window._currentChatUserlistHeight + "px.");
  156. return false;
  157. } else if(!o){
  158. l.activeDialogue().kongBotMessage("Please specify a width and a height: /size width height. Example: /size 500 500");
  159. return false;
  160. }
  161.  
  162. var width = parseInt(o[1], 10);
  163. var height = parseInt(o[2], 10);
  164. var listHeight = parseInt(o[3]||100, 10);
  165. var gameHeight = parseInt(d('game').style.height, 10);
  166. if(width < 300){
  167. l.activeDialogue().kongBotMessage("Minimum width is 300. Setting width to 300px.");
  168. width = 300;
  169. }
  170.  
  171. if(height < gameHeight){
  172. l.activeDialogue().kongBotMessage("Minimum height is the game's height. Setting height to " + gameHeight + "px.");
  173. height = gameHeight;
  174. }
  175.  
  176. if(listHeight > height - 200){
  177. l.activeDialogue().kongBotMessage("Userlist height is too large. Setting it to 100px");
  178. listHeight = 100;
  179. }
  180.  
  181. window.setTimeout(function(){GM_setValue("kong_resize_"+location.pathname, width+"/"+height+"/"+listHeight)}, 0);
  182. l.activeDialogue().kongBotMessage("Resizing chat to " + width + "px/" + height + "px/" + listHeight + "px");
  183. setWidth(width);
  184. setHeight(height, listHeight, window._currentGameCentered);
  185.  
  186. return false;
  187. });
  188.  
  189. holodeck.addChatCommand("defaultsize", function(l, n){
  190. var m = n.match(/^\/\S+\s+(\S+)/);
  191. var o = n.match(/^\/\S+\s+(\d+)\s+(\d+)(?:\s+(\d+))?/)
  192. if(m && m[1] == "reset"){
  193. l.activeDialogue().kongBotMessage("Resetting default size to 500/600/100");
  194. window.setTimeout(function(){GM_deleteValue("kong_resize_default")}, 0);
  195.  
  196. return false;
  197. } else if(m && m[1] == "show"){
  198. l.activeDialogue().kongBotMessage("Current chat size: width: " + window._defaultChatWidth + "px, height: " + window._defaultChatHeight + "px, userlist-height: " + window._defaultUserlistHeight + "px.");
  199. return false;
  200. } else if(!o){
  201. l.activeDialogue().kongBotMessage("Syntax /defaultsize width height userlist-height. userlist-height is optional. Example: /defaultsize 500 500 100");
  202. }
  203.  
  204. var width = parseInt(o[1], 10);
  205. var height = parseInt(o[2], 10);
  206. var listHeight = parseInt(o[3]||100, 10);
  207. if(width < 300){
  208. l.activeDialogue().kongBotMessage("Minimum width is 300. Setting width to 300px.");
  209. width = 300;
  210. }
  211.  
  212. if(listHeight > height){
  213. l.activeDialogue().kongBotMessage("Userlist height is too large. Setting it to 100px");
  214. listHeight = 100;
  215. }
  216.  
  217. window.setTimeout(function(){GM_setValue("kong_resize_default", width+"/"+height+"/"+listHeight)}, 0);
  218. l.activeDialogue().kongBotMessage("Set default values to width: " + width + "px, height: " + height + "px, userlist-height: " + listHeight + "px.");
  219.  
  220. return false;
  221. });
  222.  
  223. holodeck.addChatCommand("centergame", function(l, n){
  224. var center = !window._currentGameCentered;
  225. if(center){
  226. l.activeDialogue().kongBotMessage("Now centering the game");
  227. } else {
  228. l.activeDialogue().kongBotMessage("Now aligning the game to the chat's bottom");
  229. }
  230. window.setTimeout(function(){GM_setValue("kong_resize_center", center?1:0)}, 0);
  231.  
  232. centerGame(center);
  233.  
  234. return false;
  235. });
  236.  
  237. }
  238.  
  239. var getString = "", centerVal = -1, defaults = "";
  240. getString = GM_getValue("kong_resize_"+location.pathname, "");
  241. centerVal = GM_getValue("kong_resize_center", -1);
  242. defaults = GM_getValue("kong_resize_default", "");
  243.  
  244. if(defaults){
  245. var splitArr = defaults.split("/");
  246. defaultWidth = parseInt(splitArr[0], 10)||defaultWidth;
  247. defaultHeight = parseInt(splitArr[1], 10)||defaultHeight;
  248. userListHeight = parseInt(splitArr[2], 10)||userListHeight;
  249. }
  250.  
  251. window._defaultChatWidth = defaultWidth;
  252. window._defaultChatHeight = defaultHeight;
  253. window._defaultUserlistHeight = userListHeight;
  254.  
  255. var x = defaultWidth, y = defaultHeight, l = userListHeight, cg = center, override = false;
  256.  
  257. if(centerVal != -1){
  258. cg = (centerVal == 1);
  259. }
  260.  
  261. if(getString){
  262. var splitArr = getString.split("/");
  263. x = parseInt(splitArr[0], 10)||defaultWidth;
  264. y = parseInt(splitArr[1], 10)||defaultHeight;
  265. l = parseInt(splitArr[2], 10)||userListHeight;
  266. override = true;
  267. }
  268.  
  269. var gameWidth = parseInt(d('game').style.width, 10);
  270. var gameHeight = parseInt(d('game').style.height, 10);
  271.  
  272. if(x > minimumWidth){
  273. if(override || gameWidth + x < screen.width - spaceLeft){ // enough place to resize to specified width
  274. setWidth(x);
  275. }else{ // resize as far as possible
  276. var chatWidth = screen.width - gameWidth - spaceLeft;
  277. if(chatWidth > minimumWidth) setWidth(chatWidth);
  278. }
  279. }
  280.  
  281. if(y > minimumHeight && y > gameHeight){
  282. setHeight(y, l, cg);
  283. } else {
  284. setHeight(gameHeight, l, cg);
  285. }
  286. }
  287. initalize_chat_resize();