In-Chat Timestamp

Adds a timestamp to every message

  1. // ==UserScript==
  2. // @name In-Chat Timestamp
  3. // @namespace tag://kongregate
  4. // @description Adds a timestamp to every message
  5. // @include http://www.kongregate.com/games/*
  6. // @author Ventero
  7. // @version 1.2.2
  8. // @date 29.12.10
  9. // require http://kong.ventero.de/updates/55571.js
  10. // @license MIT license
  11. // ==/UserScript==
  12.  
  13. // Written by Ventero (http://www.kongregate.com/accounts/Ventero) 06/04/09
  14. // Copyright (c) 2009-2012 Ventero, licensed under MIT/X11 license
  15. // http://www.opensource.org/licenses/mit-license.php
  16.  
  17. var dom = (typeof unsafeWindow === "undefined"?window:unsafeWindow);
  18.  
  19. function init_chatTimestamp(){
  20.  
  21. var holodeck = dom.holodeck,
  22. ChatDialogue = dom.ChatDialogue;
  23.  
  24. if(holodeck && ChatDialogue){
  25.  
  26. ChatDialogue.prototype = dom.CDprototype||dom.ChatDialogue.prototype;
  27.  
  28. if(!holodeck.__timestamp){
  29. holodeck.__timestamp = true;
  30.  
  31. holodeck.addChatCommand("timeformat", function(l,n){
  32. var k = n.match(/^\/\S+\s+(\d+)/),
  33. m = "",
  34. q = l.activeDialogue();
  35. k && (m=k[1]);
  36. if(m==12 || m==24){
  37. l._timeFormat = m;
  38. window.setTimeout(function(){GM_setValue("kong_timeformat", m);}, 0);
  39. q.displayMessage("Timeformat", "Set to "+m+"-hour clock (hh:mm:ss"+(m==12?" AM/PM)":")"), { "class": "whisper received_whisper"}, {non_user: true});
  40. } else {
  41. q.displayMessage("Timeformat", "Allowed values: 12 and 24", { "class": "whisper received_whisper"}, {non_user: true});
  42. }
  43. return false;
  44. });
  45.  
  46. holodeck.addChatCommand("tscolor", function(l,n){
  47. var k = n.match(/^\/\S+\s+([0-9a-f]{6})/i),
  48. z = "";
  49. k&&(z = "#"+k[1]);
  50. if (z){
  51. updateColor(z);
  52. window.setTimeout(function(){GM_setValue("kong_timestampcolor", z);}, 0);
  53. l.activeDialogue().displayMessage("Timestamp", "Set font-color to "+z, { "class": "whisper received_whisper"}, {non_user: true});
  54. } else {
  55. l.activeDialogue().displayMessage("Timestamp", "No valid color! Format is /hlcolor ###### (# = hex character)", {"class":"whisper received_whisper"}, {non_user: true})
  56. }
  57. return false;
  58. });
  59.  
  60. holodeck.addChatCommand("toggleseconds", function(l,n){
  61. if(l._showSeconds){
  62. l._showSeconds = 0;
  63. l.activeDialogue().displayMessage("Timestamp", "Now hiding seconds", { "class": "whisper received_whisper"}, {non_user: true});
  64. }else{
  65. l._showSeconds = 1;
  66. l.activeDialogue().displayMessage("Timestamp", "Now showing seconds", { "class": "whisper received_whisper"}, {non_user: true})
  67. }
  68. window.setTimeout(function(){GM_setValue("kong_timeshowseconds", l._showSeconds);}, 0);
  69. return false;
  70. });
  71.  
  72. var timeformat = 12, fontcolor = "#999999", seconds = 0;
  73. if(typeof GM_setValue !== "function"){
  74. GM_getValue = GM_setValue = function(){};
  75. } else {
  76. timeformat = GM_getValue("kong_timeformat", 12)||12;
  77. fontcolor = GM_getValue("kong_timestampcolor", "#999999")||"#999999";
  78. seconds = GM_getValue("kong_timeshowseconds", 0)||0;
  79. }
  80.  
  81. holodeck._timeFormat = timeformat;
  82. holodeck._showSeconds = seconds;
  83.  
  84. var updateColor = (function(c){
  85. var style = document.createElement("style");
  86. style.setAttribute("type", "text/css");
  87. function _updateColor(color){
  88. style.innerHTML = "span.inline_timestamp { color: " + color + " !important; }";
  89. };
  90.  
  91. _updateColor(c);
  92. document.body.appendChild(style);
  93.  
  94. return _updateColor;
  95. })(fontcolor);
  96.  
  97. ChatDialogue.MESSAGE_TEMPLATE.template = '<p class="#{classNames}"><span style="float: left;" class="inline_timestamp">[#{time}]&nbsp;</span><span username="#{username}" class="username #{userClassNames}">#{prefix}#{username}</span><span class="separator">: </span><span class="message">#{message}</span><span class="clear"></span></p>'
  98. ChatDialogue.MESSAGE_TEMPLATE.old_evaluate_inline = ChatDialogue.MESSAGE_TEMPLATE.evaluate;
  99. ChatDialogue.MESSAGE_TEMPLATE.evaluate = function(args){
  100. var date = new Date();
  101. var hours = date.getHours();
  102. var minutes = date.getMinutes();
  103. var seconds = date.getSeconds();
  104. var time;
  105. if (holodeck._timeFormat == 12){
  106. time = (hours<10?(hours==0?"12":"0"+hours):(hours>12?(hours>21?hours-12:"0"+(hours-12)):hours))+":"+(minutes<10?"0":"")+minutes+(holodeck._showSeconds?(":"+(seconds<10?"0":"")+seconds):"")+(hours>11?" PM":" AM");
  107. } else {
  108. time = (hours<10?"0":"")+hours+":"+(minutes<10?"0":"")+minutes+(holodeck._showSeconds?(":"+(seconds<10?"0":"")+seconds):"");
  109. }
  110. args.time = time;
  111. return this.old_evaluate_inline(args);
  112. };
  113.  
  114. }
  115. }
  116. };
  117.  
  118. function check(){
  119. dom.injectScript = dom.injectScript||(document.getElementById("injectScriptDiv")?document.getElementById("injectScriptDiv").onclick():0);
  120. if(dom.injectScript){
  121. dom.injectScript(init_chatTimestamp, 0);
  122. } else if(!dom._promptedFramework && !/Chrome/i.test(navigator.appVersion)){
  123. if(confirm("You don't have the latest version of the framework-script!\n" +
  124. "Please install it, otherwise the scripts won't work.\n" +
  125. "Clicking ok will open a new tab where you can install the script"))
  126. window.open("http://userscripts.org/scripts/show/54245", "_blank");
  127. dom._promptedFramework = true;
  128. }
  129. }
  130.  
  131. setTimeout(check, 0);