AFK-command *OLD*

Adds an /afk-command

  1. // ==UserScript==
  2. // @name AFK-command *OLD*
  3. // @namespace tag://kongregate
  4. // @description Adds an /afk-command
  5. // @include http://www.kongregate.com/games/*
  6. // @author Ventero
  7. // @version 1.9.11
  8. // @date 06.04.2013
  9. // require https://greasyfork.org/scripts/18207-afk-command-library/code/AFK-command%20Library.js?version=114954
  10. // ==/UserScript==
  11.  
  12. // Written by Ventero (http://www.kongregate.com/accounts/Ventero) 05/12/09
  13. // Copyright (c) 2009-2013 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_afk(){
  19. var AUTOAFK = "kongregate_autoAFKTimeout";
  20.  
  21. var holodeck = dom.holodeck,
  22. CDialogue = dom.ChatDialogue,
  23. CRoom = dom.ChatRoom,
  24. CWindow = dom.ChatWindow,
  25. Base64 = dom.Base64;
  26.  
  27. if(holodeck && CDialogue){
  28. CDialogue.prototype = dom.CDprototype||dom.ChatDialogue.prototype;
  29. CRoom.prototype = dom.CRprototype||dom.ChatRoom.prototype;
  30. CWindow.prototype = dom.CWprototype||dom.ChatWindow.prototype;
  31.  
  32. if(!holodeck.__afk){
  33. holodeck.__afk = true;
  34. if(!holodeck.setPresenceAwayOld){
  35. holodeck.setPresenceAwayOld = holodeck.setPresenceAway;
  36. holodeck.setPresenceAway = function(){
  37. this._afk = 1;
  38. this.setPresenceAwayOld();
  39. }
  40. }
  41. if(!holodeck.setPresenceChatOld){
  42. holodeck.setPresenceChatOld = holodeck.setPresenceChat;
  43. holodeck.setPresenceChat = function(){
  44. this._afk = 0;
  45. this.setPresenceChatOld();
  46. }
  47. }
  48.  
  49. if(!CRoom.prototype.updateUserOld_AFK){
  50. CRoom.prototype.updateUserOld_AFK = CRoom.prototype.updateUser;
  51. CRoom.prototype.updateUser= function(user){
  52. this.updateUserOld_AFK.apply(this, arguments);
  53. if(user.username == this._chat_window.username()){
  54. if(this._presence != user.variables.presence){
  55. switch(user.variables.presence){
  56. case "chat":
  57. if(!this._chat_window._holodeck._afktoggle)
  58. this._chat_window._holodeck._afk = 0;
  59. break;
  60. case "away":
  61. this._chat_window._holodeck._afk = 1;
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. }
  68.  
  69. holodeck._chat_commands.afk[0] = function (l, n) { if (l._afk == 0) {l.setPresenceAway()} else {l.setPresenceChat()} return false; }
  70. holodeck._chat_commands.back[0] = function(l,n){l.setPresenceChat(); return false};
  71.  
  72. holodeck.addChatCommand("afkmessage", function (l, n){ var z = n.match(/^\/\S+\s+(.+)/); if (z){a = z[1]}else{a="I am currently AFK"} l._afkmessage = a; l.activeDialogue().kongBotMessage("AFK-message set to: "+a); return false});
  73.  
  74. holodeck.addChatCommand("afktoggle", function(l, n){ if (l._afktoggle == 0) {l._afktoggle = 1; l.activeDialogue().kongBotMessage("Your AFK-flag won't get removed automatically")} else {l._afktoggle = 0; l.activeDialogue().kongBotMessage("Your AFK-flag will be removed automatically")} return false; });
  75.  
  76. holodeck.addChatCommand("autoafk", function(l, n){
  77. var match = n.match(/^\/autoafk\s+(\d+)/),
  78. timeout = 15;
  79.  
  80. if(match && match[1]){
  81. timeout = parseInt(match[1], 10);
  82. }
  83.  
  84. l._autoAFK = timeout*60*1000;
  85. window.setTimeout(function(){GM_setValue(AUTOAFK, timeout)}, 0);
  86.  
  87. if(l._autoAFKTimeout){
  88. clearTimeout(l._autoAFKTimeout);
  89. }
  90.  
  91. if(timeout){
  92. l.activeDialogue().kongBotMessage("Set auto-AFK timeout to " + timeout + " minute" + (timeout > 1?"s":""));
  93. l._autoAFKTimeout = setTimeout(function(a){a.setPresenceAway();}, l._autoAFK, l);
  94. } else {
  95. l.activeDialogue().kongBotMessage("Disabled auto-AFK");
  96. }
  97.  
  98. return false;
  99. });
  100.  
  101. holodeck.checkAFK = function(){
  102. if(!this._afktoggle){
  103. this._afk = 0;
  104. }
  105. if(this._autoAFKTimeout){
  106. clearTimeout(this._autoAFKTimeout);
  107. }
  108. if(this._autoAFK){
  109. this._autoAFKTimeout = setTimeout(function(a){a.setPresenceAway();}, this._autoAFK, this);
  110. }
  111. }
  112.  
  113. holodeck.addOutgoingMessageFilter(function(message, nextFunction){
  114. holodeck.checkAFK();
  115. nextFunction(message, nextFunction);
  116. });
  117.  
  118. // Outgoing whispers aren't filtered (yet), so check them manually...
  119. if(!CWindow.prototype.oldSendPrivateMessageAFK){
  120. CWindow.prototype.oldSendPrivateMessageAFK = CWindow.prototype.sendPrivateMessage;
  121. CWindow.prototype.sendPrivateMessage = function(user, msg){
  122. if(msg.indexOf(this._holodeck._afkprefix)!=0){
  123. this._holodeck.checkAFK();
  124. }
  125. this.oldSendPrivateMessageAFK(user, msg);
  126. }
  127. }
  128.  
  129. // Create setTimeout on session reconnect
  130. if(!CWindow.prototype.onLoginOldAFK){
  131. CWindow.prototype.onLoginOldAFK = CWindow.prototype.onLogin;
  132. CWindow.prototype.onLogin = function(){
  133. this.onLoginOldAFK();
  134. if(this._holodeck._afk) {
  135. this._holodeck.setPresenceAway();
  136. } else {
  137. this._holodeck.setPresenceChat();
  138. this._holodeck.checkAFK();
  139. }
  140. }
  141. }
  142.  
  143. if(!CDialogue.prototype.reply){
  144. CDialogue.prototype.reply = function(a){}
  145. }
  146.  
  147. if(!CDialogue.prototype.showReceivedPM){
  148. CDialogue.prototype.showReceivedPM = CDialogue.prototype.receivedPrivateMessage;
  149. }
  150.  
  151. CDialogue.prototype.receivedPrivateMessage = function(a){
  152. if (a.data.success){
  153. this.reply(a.data.from);
  154. if(this._holodeck._afk && Base64.decode(a.data.message).indexOf(this._holodeck._afkprefix)!=0){this.sendPrivateMessage(a.data.from, this._holodeck._afkprefix+this._holodeck._afkmessage)}
  155. }
  156. this.showReceivedPM(a);
  157. }
  158.  
  159. holodeck._afk = 0;
  160.  
  161. holodeck._afktoggle = 0;
  162.  
  163. holodeck._afkmessage = "I am currently AFK";
  164.  
  165. holodeck._afkprefix = "[AFK] ";
  166.  
  167. var autoAFK = 15;
  168.  
  169. try{
  170. if(GM_setValue){
  171. autoAFK = GM_getValue(AUTOAFK, 15);
  172. }else{
  173. GM_setValue = function(a,b){};
  174. }
  175. }catch(e){
  176. GM_setValue = function(a,b){};
  177. }
  178.  
  179. holodeck._autoAFK = autoAFK*60*1000;
  180. if(holodeck._autoAFK > 0){
  181. holodeck._autoAFKTimeout = setTimeout(function(a){a.setPresenceAway();}, holodeck._autoAFK, holodeck);
  182. }
  183. }
  184. }
  185. }
  186.  
  187. function check(){
  188. var injectScript = dom.injectScript||(document.getElementById("injectScriptDiv")?document.getElementById("injectScriptDiv").onclick():0);
  189. if(injectScript){
  190. injectScript.call(dom, init_afk, 0);
  191. } else if(!dom._promptedFramework && !/Chrome/i.test(navigator.appVersion)){
  192. if(confirm("You don't have the latest version of the framework-script!\n" +
  193. "Please install it, otherwise the scripts won't work.\n" +
  194. "Clicking ok will open a new tab where you can install the script"))
  195. window.open("http://userscripts.org/scripts/show/54245", "_blank");
  196. dom._promptedFramework = true;
  197. }
  198. }
  199.  
  200. setTimeout(check, 0);