Agar Minimap

Minimap for Agar

  1. // ==UserScript==
  2. // @name Agar Minimap
  3. // @namespace http://your.homepage/
  4. // @version 0.1
  5. // @description Minimap for Agar
  6. // @author agar_cheats
  7. // @match http://agar.io/
  8. // @require https://code.jquery.com/jquery-latest.js
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. var observer = new MutationObserver(function(mutations) {
  14. for (var i = 0; i < mutations.length; i++) {
  15. if (/^http:\/\/agar\.io\/main_out\.js/.test(mutations[i].addedNodes[0].src)) {
  16. var scriptNode = mutations[i].addedNodes[0];
  17. httpRequest(scriptNode.src, handleResponse);
  18. try{
  19. document.head.removeChild(scriptNode);
  20. }catch(e){}
  21. observer.disconnect();
  22. break;
  23. }
  24. }
  25. });
  26. observer.observe(document.head, {childList: true});
  27.  
  28. function httpRequest(source, callBack) {
  29. var request = new XMLHttpRequest();
  30. request.onload = function() {
  31. callBack(this.responseText);
  32. };
  33. request.onerror = function() {
  34. console.log("Response was null");
  35. };
  36. request.open("get", source, true);
  37. request.send();
  38. }
  39.  
  40. function insertScript(script) {
  41. if (typeof jQuery === "undefined") {
  42. return setTimeout(insertScript, 0, script);
  43. }
  44. var scriptNode = document.createElement("script");
  45. scriptNode.innerHTML = script;
  46. document.head.appendChild(scriptNode);
  47. }
  48.  
  49. function handleResponse(script) {
  50. script = script.replace(/(\r\n|\n|\r)/gm,"");
  51. script = addHooks(script);
  52. insertScript(script);
  53. }
  54.  
  55. function addHooks(script) {
  56. var match = script.match(/1==(\w+)\.length&&\(/);
  57. var cells = match[1];
  58. match = script.match(/\w+\.width&&(\w+)\.drawImage\(\w+,\w+-\w+\.width-10,10\);/);
  59. split = script.split(match[0]);
  60. return split[0]+match[0]+'Draw('+match[1]+','+cells+');'+split[1];
  61. }
  62.  
  63. function CenterOfMass(cells, prop){
  64. var n = 0;
  65. var d = 0;
  66. for (var i in cells){
  67. n += cells[i].size*cells[i].size * cells[i][prop];
  68. d += cells[i].size*cells[i].size;
  69. }
  70. return n/d;
  71. }
  72.  
  73. var map = null;
  74. var last = {'x':0, 'y':0, 'color':'#000000', 'size':200};
  75.  
  76. window.Draw = function(oc, cells) {
  77. var client_width = window.innerWidth, client_height = window.innerHeight;
  78. var board_size = 11180;
  79. map && oc.drawImage(map, client_width-map.width-10, client_height-map.height-10);
  80. map || (map = document.createElement("canvas"));
  81. var c = Math.min(150, .3 * client_height, .3 * client_width) / board_size;
  82. map.width = board_size * c;
  83. map.height = board_size * c;
  84.  
  85. mc = map.getContext("2d");
  86. mc.scale(c, c);
  87. mc.globalAlpha = .2;
  88. mc.fillStyle = "#000000";
  89. mc.fillRect(0, 0, board_size, board_size);
  90. mc.globalAlpha = .4;
  91. mc.lineWidth = 200;
  92. mc.strokeStyle = "#000000";
  93. mc.strokeRect(0, 0, board_size, board_size);
  94. if (cells && cells[0]){
  95. last.x = CenterOfMass(cells,'x');
  96. last.y = CenterOfMass(cells,'y');
  97. last.size = 200;
  98. last.color = cells[0].color;
  99. }
  100. mc.beginPath();
  101. mc.arc(last.x, last.y, last.size, 0, 2 * Math.PI, false);
  102. mc.globalAlpha = .8;
  103. mc.fillStyle = last.color;
  104. mc.fill();
  105. mc.lineWidth = 70;
  106. mc.stroke();
  107. }