Chat Line Highlighting

Highlights lines which include your name and other set words

  1. // ==UserScript==
  2. // @name Chat Line Highlighting
  3. // @include http://www.kongregate.com/games/*
  4. // @description Highlights lines which include your name and other set words
  5. // @author Ventero
  6. // @version 2.6
  7. // @date 2012-10-21
  8. // require http://kong.ventero.de/updates/49868.js
  9. // @namespace https://greasyfork.org/users/32649
  10. // ==/UserScript==
  11.  
  12. // Written by Ventero (http://www.kongregate.com/accounts/Ventero) 05/23/09
  13. // Copyright (c) 2009-2012 Ventero, licensed under MIT/X11 license
  14. // http://www.opensource.org/licenses/mit-license.php
  15.  
  16. var dom = (typeof unsafeWindow === "undefined"?window:unsafeWindow);
  17.  
  18. function init_highlighting(){
  19.  
  20. var holodeck = dom.holodeck,
  21. CDialogue = dom.ChatDialogue,
  22. CRoom = dom.ChatRoom,
  23. CWindow = dom.ChatWindow;
  24.  
  25. if(CRoom && CDialogue){
  26.  
  27. CDialogue.prototype = dom.CDprototype||dom.ChatDialogue.prototype;
  28. CRoom.prototype = dom.CRprototype||dom.ChatRoom.prototype;
  29. CWindow.prototype = dom.CWprototype||dom.ChatWindow.prototype;
  30. if(!CDialogue.prototype.searchWord){
  31.  
  32. if(!String.prototype.trim){
  33. String.prototype.trim = function(){
  34. return this.replace(/^\s+/, "").replace(/\s+$/, "");
  35. }
  36. }
  37.  
  38. CDialogue.prototype.searchWord = function(a, b){
  39. for (var i=0;i<b.length;i++){
  40. var r = b[i].replace(/(\/|\.|\*|\+|\?|\||\(|\)|\[|\]|\{|\}|\\)/g, '\\$1'),
  41. reg = new RegExp("\\b"+r+"\\b");
  42. if (reg.test(a)) return true;
  43. }
  44. return false;
  45. };
  46.  
  47. CDialogue.prototype.searchUser = function(a){
  48. return this.searchWord(a, this._holodeck._hluser);
  49. };
  50.  
  51. CDialogue.prototype.searchText = function(a){
  52. var l = this._holodeck,
  53. z = l._highlighting.concat([l._username.toLowerCase()]);
  54. return this.searchWord(a, z);
  55. }
  56.  
  57. CWindow.prototype.hlFriend = function(a){
  58. return this._holodeck._hl_friends && this.isFriend(a);
  59. }
  60.  
  61. CWindow.prototype.hlMod = function(a){
  62. if(!this._holodeck._hl_mods) return;
  63.  
  64. return this._rooms.any(function(roomArr){
  65. var room = roomArr[1];
  66. var user = room.user(a);
  67. return user && room.canUserModerate(user);
  68. });
  69. }
  70.  
  71. CWindow.prototype.friendOrMod = function(a){
  72. if(a.toLowerCase() == this._holodeck._username.toLowerCase()) return "";
  73.  
  74. var colors = [];
  75. if(this.hlMod(a)) colors.push(" hlmod");
  76. if(this.hlFriend(a)) colors.push(" hlfriend");
  77. if(colors.length > 1)
  78. return colors[this._holodeck._hl_priority]
  79.  
  80.  
  81. return (colors[0] || "");
  82. }
  83.  
  84. CDialogue.prototype.displayUnsanitizedMessageOldHighlight = CDialogue.prototype.displayUnsanitizedMessage;
  85.  
  86. CDialogue.prototype.displayUnsanitizedMessage = function(user, msg, attributes, options){
  87. if(!attributes) attributes = {};
  88. var classes = attributes["class"] || "";
  89. var isWhisper = (classes.indexOf("whisper") >= 0);
  90.  
  91. if(!(options && options["private"]))
  92. classes += this._user_manager.friendOrMod(user.toLowerCase());
  93.  
  94. if(!isWhisper &&
  95. !this._user_manager.isMuted(user.toLowerCase()) &&
  96. (this.searchUser(user.toLowerCase()) ||
  97. this.searchText(msg.toLowerCase()))) {
  98. classes += " highlight";
  99. if(typeof this.new_private_message === "function") {
  100. var oldChime = holodeck._pm_chime;
  101. holodeck._pm_chime = holodeck._hl_chime;
  102. this.new_private_message();
  103. holodeck._pm_chime = oldChime;
  104. }
  105. }
  106.  
  107. attributes["class"] = classes;
  108.  
  109. this.displayUnsanitizedMessageOldHighlight(user, msg, attributes, options);
  110. }
  111.  
  112. holodeck.addChatCommand("highlight", function(l,n){
  113. var k = n.match(/^\/\S+\s+(.+)/),
  114. z = "";
  115. k&&(z = k[1])
  116. if(z){
  117. z = z.replace(/\s+/g, ' ').trim();
  118. window.setTimeout(function(){GM_setValue("kong_highlighting", z.toLowerCase());}, 0);
  119. l.activeDialogue().kongBotMessage("Now highlighting: "+z+" "+l._username);
  120. l._highlighting = z.toLowerCase().split(' ');
  121. }
  122. return false;
  123. });
  124.  
  125. holodeck.addChatCommand("hluser", function(l,n){
  126. var k = n.match(/^\/\S+\s+(.+)/),
  127. z = "";
  128. k&&(z = k[1])
  129. if(z){
  130. z = z.replace(/\s+/g, ' ').trim();
  131. window.setTimeout(function(){GM_setValue("kong_highlightuser", z.toLowerCase());}, 0);
  132. l.activeDialogue().kongBotMessage("Now highlighting user(s): "+z);
  133. l._hluser = z.toLowerCase().split(' ');
  134. }
  135. return false;
  136. });
  137.  
  138. function generateCallback(name, stop, start){
  139. return function(l, n){
  140. if(l["_hl_" + name]){
  141. l["_hl_" + name] = 0;
  142. l.activeDialogue().kongBotMessage(stop || "Stopped highlighting messages by " + name);
  143. }else{
  144. l["_hl_" + name] = 1;
  145. l.activeDialogue().kongBotMessage(start || "Now highlighting messages by " + name);
  146. }
  147. window.setTimeout(function(){GM_setValue("kong_highlight"+name, l["_hl_" + name]);}, 0);
  148. return false;
  149. }
  150. }
  151.  
  152. holodeck.addChatCommand("hlmods", generateCallback("mods"));
  153. holodeck.addChatCommand("hlfriends", generateCallback("friends"));
  154. holodeck.addChatCommand("hlchime", generateCallback("chime", "Stopped playing the chime for highlighted messages",
  155. "Now playing the chime for highlighted messages"));
  156. holodeck.addChatCommand("hlpriority", generateCallback("priority", "Now prioritizing mods over friends", "Now prioritizing friends over mods"));
  157.  
  158. function generateColorCallback(selector, rule, name, text, max){
  159. if(!max) max = 1;
  160. return function(l, n){
  161. var k = n.match(/^\/\S+\s+#?([0-9a-f]{6})/i),
  162. z = "",
  163. count = 0;
  164. if(k) z = "#" + k[1];
  165. if(z){
  166. for(var i = 0; i < sheet.cssRules.length; i++){
  167. if(sheet.cssRules[i].selectorText.indexOf(selector) == 0){
  168. sheet.cssRules[i].style.setProperty(rule, z, "important");
  169. if(++count == max){
  170. window.setTimeout(function(){GM_setValue("kong_" + name, z);}, 0);
  171. l.activeDialogue().kongBotMessage("New " + (text||name) + ": " + z);
  172. return false;
  173. }
  174. }
  175. }
  176. } else {
  177. l.activeDialogue().kongBotMessage("No valid color! Format is /" + name + " XXXXXX (X = hex character)");
  178. }
  179. return false;
  180. }
  181. };
  182.  
  183. holodeck.addChatCommand(
  184. "whispercolor",
  185. generateColorCallback("#kong_game_ui .chat_message_window .whisper",
  186. "background-color",
  187. "whispercolor")
  188. )
  189.  
  190. holodeck.addChatCommand(
  191. "friendcolor",
  192. generateColorCallback("#kong_game_ui .chat_message_window .hlfriend span.chat_message_window_username",
  193. "color",
  194. "friendcolor")
  195. )
  196. holodeck.addChatCommand(
  197. "hlcolor",
  198. generateColorCallback("#kong_game_ui .chat_message_window .highlight",
  199. "background-color",
  200. "hlcolor",
  201. "highlighting-color",
  202. 2)
  203. )
  204. holodeck.addChatCommand(
  205. "modcolor",
  206. generateColorCallback("#kong_game_ui .chat_message_window .hlmod span.chat_message_window_username",
  207. "color",
  208. "modcolor")
  209. )
  210.  
  211. holodeck.addChatCommand("hllist", function(l, n) {
  212. var diag = l.activeDialogue();
  213. function botMessage(msg) {
  214. diag.displayUnsanitizedMessage("Kong Bot", msg);
  215. }
  216.  
  217. botMessage("Current highlighting settings:");
  218.  
  219. if(holodeck._hluser.length > 0) {
  220. botMessage('Users:');
  221. botMessage('Users: ' + holodeck._hluser.map(function(user) {
  222. return ['<a href="#" onclick="holodeck.showMiniProfile(\'', user,
  223. '\'); return false;">', user, '</a>'].join("");
  224. }).join(" "));
  225. } else {
  226. botMessage("No users highlighted");
  227. }
  228.  
  229. if(holodeck._highlighting.length > 0) {
  230. botMessage('Words: ' + holodeck._highlighting.join(" "));
  231. } else {
  232. botMessage("No words highlighted");
  233. }
  234.  
  235. botMessage('Highlight color: <span style="color: ' + color + '">' +
  236. color + '</span>');
  237. botMessage('Whisper color: <span style="color: ' + wcolor + '">' +
  238. wcolor + '</span>');
  239.  
  240. botMessage("Highlighting friends: " +
  241. (holodeck._hl_friends ? "Yes" : "No") +
  242. ' (color: <span style="color: ' + fcolor + '">' +
  243. fcolor + '</span>)');
  244. botMessage("Highlighting mods: " +
  245. (holodeck._hl_mods ? "Yes" : "No") +
  246. ' (color: <span style="color: ' + mcolor + '">' +
  247. mcolor + '</span>)');
  248.  
  249. botMessage("Highlight priority: " +
  250. (holodeck._hl_priority ? "Friends over mods" : "Mods over friends"));
  251. botMessage("Playing chime: " +
  252. (holodeck._hl_chime ?
  253. (typeof holodeck._pm_chime !== "undefined" ? "Yes" :
  254. 'No, <a href="http://userscripts.org/scripts/show/65622">script</a> not installed') :
  255. "No"));
  256. return false;
  257. });
  258.  
  259. holodeck.addChatCommand("hlreset", function(l, n) {
  260. var diag = l.activeDialogue();
  261. diag.kongBotMessage("Resetting all highlighting preferences");
  262.  
  263. holodeck._chat_commands.hlcolor[0](holodeck, "/color #def6ea");
  264. holodeck._chat_commands.whispercolor[0](holodeck, "/color #deeaf6");
  265. holodeck._chat_commands.friendcolor[0](holodeck, "/color #006600");
  266. holodeck._chat_commands.modcolor[0](holodeck, "/color #ba6328");
  267. holodeck._hl_priority = 1;
  268. holodeck._hl_friends = 1;
  269. holodeck._hl_mods = 1;
  270. holodeck._hl_chime = 1;
  271. holodeck._highlighting = [];
  272. holodeck._hluser = [];
  273.  
  274. ["highlighting", "highlightuser", "hlcolor", "whispercolor", "friendcolor",
  275. "modcolor", "highlightfriends", "highlightpriority", "highlightmods",
  276. "highlightchime"].forEach(function(pref) {
  277. window.setTimeout(function() {
  278. GM_deleteValue("kong_" + pref);
  279. }, 0);
  280. });
  281.  
  282. return false;
  283. });
  284.  
  285. holodeck._chat_commands.hl = holodeck._chat_commands.highlight;
  286. holodeck._chat_commands.hlfriend = holodeck._chat_commands.hlfriends;
  287.  
  288. holodeck._highlighting = [];
  289. holodeck._hluser = [];
  290.  
  291.  
  292. var color = "#def6ea", wcolor = "#deeaf6", fcolor = "#006600", mcolor = "#ba6328", priority = 1, friends = 1, mods = 1, chime = 1;
  293.  
  294. if(typeof GM_setValue !== "function"){
  295. GM_setValue = GM_getValue = function(){};
  296. } else {
  297. // migrate old value
  298. var temp = GM_getValue("kong_highlightcolor", "");
  299. if(temp){
  300. GM_setValue("kong_hlcolor", temp);
  301. if(typeof GM_deleteValue === "undefined"){
  302. GM_setValue("kong_highlightcolor", "");
  303. } else {
  304. GM_deleteValue("kong_highlightcolor");
  305. }
  306. }
  307.  
  308. var list = GM_getValue("kong_highlighting"),
  309. user = GM_getValue("kong_highlightuser");
  310. color = GM_getValue("kong_hlcolor", "#def6ea")||"#def6ea";
  311. wcolor = GM_getValue("kong_whispercolor", "#deeaf6")||"#deeaf6";
  312. fcolor = GM_getValue("kong_friendcolor", "#006600")||"#006600";
  313. mcolor = GM_getValue("kong_modcolor", "#ba6328")||"#ba6328";
  314. friends = GM_getValue("kong_highlightfriends", 1);
  315. priority = GM_getValue("kong_highlightpriority", 1);
  316. mods = GM_getValue("kong_highlightmods", 1);
  317. chime = GM_getValue("kong_highlightchime", 1);
  318. if(list){holodeck._highlighting = list.trim().split(' ')};
  319. if(user){holodeck._hluser = user.trim().split(' ')}
  320. }
  321.  
  322. holodeck._hl_friends = friends;
  323. holodeck._hl_mods = mods;
  324. holodeck._hl_chime = chime;
  325. holodeck._hl_priority = priority;
  326.  
  327. // guarantee we have a non-crossdomain stylesheet
  328. var style = document.createElement("style");
  329. var head = document.getElementsByTagName("head")[0];
  330. (head || document.body).appendChild(style);
  331.  
  332. // now find it...
  333. var sheet = null;
  334. for(var s = document.styleSheets.length - 1; s >= 0; --s) {
  335. try{
  336. if(document.styleSheets[s].cssRules && document.styleSheets[s].cssRules.length) {
  337. sheet = document.styleSheets[s];
  338. break;
  339. }
  340. }catch(e){ /* no-op */ }
  341. }
  342.  
  343. if(!sheet) {
  344. alert("Kongregate Chat Line Highlighting could not find a style sheet!\nPlease send a message to Ventero about this problem.");
  345. return;
  346. }
  347.  
  348. sheet.insertRule('#kong_game_ui .chat_message_window .whisper { background-color: '+wcolor+' !important; }', sheet.cssRules.length);
  349. sheet.insertRule('#kong_game_ui .chat_message_window .highlight.even { background-color: '+color+' !important; }', sheet.cssRules.length);
  350. sheet.insertRule('#kong_game_ui .chat_message_window .highlight { background-color: '+color+' !important; }', sheet.cssRules.length);
  351. sheet.insertRule('#kong_game_ui .chat_message_window .hlfriend span.chat_message_window_username { color: '+fcolor+' !important; }', sheet.cssRules.length);
  352. sheet.insertRule('#kong_game_ui .chat_message_window .hlmod span.chat_message_window_username { color: '+mcolor+' !important; }', sheet.cssRules.length);
  353. }
  354. }
  355. }
  356.  
  357. function check(){
  358. dom.injectScript = dom.injectScript||(document.getElementById("injectScriptDiv")?document.getElementById("injectScriptDiv").onclick():0);
  359. if(dom.injectScript){
  360. dom.injectScript(init_highlighting, 300);
  361. } else if(!dom._promptedFramework && !/Chrome/i.test(navigator.appVersion)){
  362. if(confirm("You don't have the latest version of the framework-script!\n" +
  363. "Please install it, otherwise the scripts won't work.\n" +
  364. "Clicking ok will open a new tab where you can install the script"))
  365. window.open("http://userscripts.org/scripts/show/54245", "_blank");
  366. dom._promptedFramework = true;
  367. }
  368. }
  369.  
  370. setTimeout(check, 0);