Mousehunt Helper - XHRIntercept

XHR Intercept for MHH

  1. // ==UserScript==
  2. // @name Mousehunt Helper - XHRIntercept
  3. // @namespace DSXC
  4. // @description XHR Intercept for MHH
  5. // @include http://www.mousehuntgame.com/*
  6. // @include https://www.mousehuntgame.com/*
  7. // @include http://apps.facebook.com/mousehunt/*
  8. // @include https://apps.facebook.com/mousehunt/*
  9. // @version 1.0
  10. // ==/UserScript==
  11. //===============================================================================
  12.  
  13. var _is_xhri_setup = false;
  14. var _trap_update_user_array = new Array();
  15. var _timeout_called = false;
  16. var _timeout_hidden_called = false;
  17. var _last_user;
  18.  
  19. setupCommunications();
  20.  
  21. function setupCommunications()
  22. {
  23. document.addEventListener("xhri_trap_change", xhri_trap_change, false);
  24. document.addEventListener("xhri_hidden_item", xhri_hidden_item, false);
  25. _is_xhri_setup = true;
  26. }
  27.  
  28. function xhri_trap_change(aEvent)
  29. {
  30. if (typeof user == 'undefined')
  31. {
  32. timeout = setTimeout(function(){xhri_trap_change(aEvent)}, 2000);
  33. return;
  34. }
  35.  
  36. _trap_update_user_array.push(user);
  37. if (_timeout_called)
  38. return;
  39.  
  40. _timeout_called = true;
  41. timeout = setTimeout(function(){call_trap_check()}, 2000);
  42. }
  43.  
  44. function xhri_hidden_item(aEvent)
  45. {
  46. if (typeof hg == 'undefined' ||
  47. $(".inventoryitemview[data-view-id="+$($("#tabbarContent_page_4 .inventoryitemview")[0]).data("viewId")+"]").length <= 0)
  48. {
  49. timeout = setTimeout(function(){xhri_hidden_item(aEvent)}, 2000);
  50. return;
  51. }
  52.  
  53. if (_timeout_hidden_called)
  54. return;
  55.  
  56. _timeout_hidden_called = true;
  57. timeout = setTimeout(function(){call_modify_hidden_items()}, 2000);
  58. }
  59.  
  60. function call_trap_check()
  61. {
  62. var _trap_update_user = JSON.stringify(_trap_update_user_array.pop());
  63.  
  64. //Don't send another update if the last user pushed is the same as the user we're currently on.
  65. if (_trap_update_user == _last_user)
  66. {
  67. _trap_update_user_array.length = 0;
  68. _timeout_called = false;
  69. return;
  70. }
  71.  
  72. var evt = document.createEvent("MutationEvents");
  73. evt.initMutationEvent("ht_trap_change", true, true, this, _trap_update_user, _trap_update_user, 'ht_stuff', 1);
  74.  
  75. document.dispatchEvent(evt);
  76.  
  77. _last_user = _trap_update_user;
  78. _trap_update_user_array.length = 0;
  79. _timeout_called = false;
  80. }
  81.  
  82. function call_modify_hidden_items()
  83. {
  84. var inv = hg.utils.UserInventory.getAllItems();
  85. for (var category in inv) if (inv.hasOwnProperty(category) && category != "length")
  86. {
  87. for (var item in inv[category]) if (inv[category].hasOwnProperty(item) && item != "length")
  88. {
  89. hg.utils.UserInventory.getAllItems()[category][item].is_hidden = false;
  90. }
  91. }
  92.  
  93. //Empty the existing items:
  94. $(".inventoryitemview[data-view-id="+$($("#tabbarContent_page_4 .inventoryitemview")[0]).data("viewId")+"]").empty();
  95.  
  96. //Render all the items again:
  97. app.views.InventoryItemView[$($("#tabbarContent_page_4 .inventoryitemview")[0]).data("viewId")].render();
  98. }
  99.  
  100. function getQueryParams(qs)
  101. {
  102. var urlParams = {},
  103. e,
  104. d = function (s) { return decodeURIComponent(s).replace(/\+/g, " "); },
  105. r = /([^&=]+)=?([^&]*)/g;
  106.  
  107. while (e = r.exec(d(qs))) {
  108. if (e[1].indexOf("[") == "-1")
  109. urlParams[d(e[1])] = d(e[2]);
  110. else {
  111. var b1 = e[1].indexOf("["),
  112. aN = e[1].slice(b1+1, e[1].indexOf("]", b1)),
  113. pN = d(e[1].slice(0, b1));
  114. if (typeof urlParams[pN] != "object")
  115. urlParams[d(pN)] = {},
  116. urlParams[d(pN)].length = 0;
  117. if (aN)
  118. urlParams[d(pN)][d(aN)] = d(e[2]);
  119. else
  120. Array.prototype.push.call(urlParams[d(pN)], d(e[2]));
  121. }
  122. }
  123.  
  124. return urlParams;
  125. }
  126.  
  127. (function(open)
  128. {
  129. XMLHttpRequest.prototype.open = function(method, url, async, user, pass)
  130. {
  131. if (typeof url == 'string')
  132. {
  133. if (url.search('mousehuntgame') != -1)
  134. {
  135. if (!_is_xhri_setup)
  136. setupCommunications();
  137. /*
  138. window.unsafeWindow || (
  139. unsafeWindow = (function()
  140. {
  141. var el = document.createElement('p');
  142. el.setAttribute('onclick', 'return window;');
  143. return el.onclick();
  144. })()
  145. );*/
  146.  
  147. var mh_user = window.user; //unsafeWindow.user;
  148. if (mh_user != null)
  149. {
  150. this.mhUserJSON = mh_user;
  151.  
  152. //TODO: Make a call to grab the user variable from HG with the least amount of data. Use this as a direct before snapshot of event firing.
  153. // In conjunction with the earlier variable grab we can determine 3 points of user values for hunt tracking.
  154.  
  155. this.calledURL = url;
  156. this.addEventListener("readystatechange", function()
  157. {
  158. if (this.readyState == 4)
  159. {
  160. if (this.responseText.substring(0, 1) == "{")
  161. {
  162. //TODO: Make this all generic, have one event thrown, and one submit.
  163. // Specific instructions can be made in the response packet so message information can be gathered from there.
  164.  
  165. var respJSON = JSON.parse(this.responseText);
  166. respJSON.ht_submit_URL = this.calledURL;
  167. respJSON.ht_submit_params = this.submit_params;
  168.  
  169. //Hunt submission
  170. if (this.calledURL.search('activeturn.php') != -1)
  171. {
  172. var evt = document.createEvent("MutationEvents");
  173.  
  174. evt.initMutationEvent("ht_submit", true, true, this, JSON.stringify(this.mhUserJSON), JSON.stringify(respJSON), 'ht_stuff', 1);
  175. document.dispatchEvent(evt);
  176. }
  177.  
  178. //NOTE: Currently disabled.
  179. //Crafting attempts.
  180. else if (this.calledURL.search('crafting.php') != -1)
  181. {
  182. /*
  183. var crafts = $('#selectedComponents div.craftThumb:has(input)');
  184. var t_array = Array();
  185. for (var i in respJSON.ht_submit_params.parts)
  186. {
  187. for (var j = 0; j < crafts.length; j++)
  188. {
  189. if (crafts[j].classList.contains(i))
  190. {
  191. var t_obj = new Object();
  192. t_obj.name = $('span', crafts[j]).html();
  193. t_obj.val = respJSON.ht_submit_params.parts[i];
  194. t_array.push(t_obj);
  195. break;
  196. }
  197. }
  198. }
  199.  
  200. respJSON.ht_submit_params.parts.translated = t_array;
  201.  
  202. var evt = document.createEvent("MutationEvents");
  203.  
  204. evt.initMutationEvent("ht_crafting", true, true, this, JSON.stringify(this.mhUserJSON), JSON.stringify(respJSON), 'ht_stuff', 1);
  205. document.dispatchEvent(evt);
  206. */
  207. }
  208.  
  209. //TEM effectiveness selection
  210. else if (this.calledURL.search('getmiceeffectiveness.php') != -1)
  211. {
  212. var evt = document.createEvent("MutationEvents");
  213.  
  214. evt.initMutationEvent("ht_effectiveness", true, true, this, JSON.stringify(this.mhUserJSON), JSON.stringify(respJSON), 'ht_stuff', 1);
  215. document.dispatchEvent(evt);
  216. }
  217.  
  218. //Adversaries page tab click
  219. else if (this.calledURL.search('getmousegroup.php') != -1 ||
  220. this.calledURL.search('getregionmice.php') != -1)
  221. {
  222. var evt = document.createEvent("MutationEvents");
  223.  
  224. evt.initMutationEvent("ht_group_effectiveness", true, true, this, JSON.stringify(this.mhUserJSON), JSON.stringify(respJSON), 'ht_stuff', 1);
  225. document.dispatchEvent(evt);
  226. }
  227. //Convertible
  228. else if (this.calledURL.search('useconvertible.php') != -1)
  229. {
  230. var evt = document.createEvent("MutationEvents");
  231. evt.initMutationEvent("ht_convertible", true, true, this, JSON.stringify(this.mhUserJSON), JSON.stringify(respJSON), 'ht_stuff', 1);
  232.  
  233. document.dispatchEvent(evt);
  234. }
  235.  
  236. //Inventory
  237. else if (this.calledURL.search('userInventory.php') != -1)
  238. {
  239. var evt = document.createEvent("MutationEvents");
  240. evt.initMutationEvent("ht_inventory", true, true, this, JSON.stringify(this.mhUserJSON), JSON.stringify(respJSON), 'ht_stuff', 1);
  241.  
  242. document.dispatchEvent(evt);
  243. }
  244. }
  245. }
  246. },
  247. false);
  248. }
  249. }
  250. }
  251.  
  252. //Call regardless. Use async if not defined to avoid hanging browser due to extension needing waiting on response.
  253. if (is_set(async))
  254. open.call(this, method, url, async, user, pass);
  255. else
  256. open.call(this, method, url, true, user, pass);
  257. };
  258.  
  259. })(XMLHttpRequest.prototype.open);
  260.  
  261.  
  262. (function(send) {
  263. XMLHttpRequest.prototype.send = function(params)
  264. {
  265. this.submit_params = getQueryParams(params);
  266. send.call(this, params);
  267. }
  268. })(XMLHttpRequest.prototype.send);
  269.  
  270.  
  271. function is_set(variable)
  272. {
  273. if (typeof variable === "undefined")
  274. return false;
  275. else
  276. return true;
  277. }