Advertisement
AgarB13

Agar.ioZoomOut

Feb 27th, 2016
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.38 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Agar.io Zoom Out
  3. // @namespace Agar.io with Unlimited Zoom
  4. // @version 1.0
  5. // @description Enjoy! ^-^
  6. // @author Helinho
  7. // @license MIT
  8. // @match http://agar.io/*
  9. // @require https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.4/socket.io.min.js
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. //http://agar.io/img/background.png
  15.  
  16. setTimeout(function() {
  17.  
  18. var socket = io.connect('');
  19. var canMove = true;
  20. var movetoMouse = true;
  21. var moveEvent = new Array(2);
  22. var canvas = document.getElementById("canvas");
  23. last_transmited_game_server = null;
  24.  
  25. socket.on('force-login', function (data) {
  26. socket.emit("login", {"uuid":client_uuid, "type":"client"});
  27. transmit_game_server();
  28. });
  29.  
  30. socket.on('spawn-count', function (data) {
  31. console.log("Bot Count: " + data);
  32. });
  33.  
  34. var client_uuid = localStorage.getItem('client_uuid');
  35.  
  36. if(client_uuid == null){
  37. console.log("generating a uuid for this user");
  38. client_uuid = Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  39. localStorage.setItem('client_uuid', client_uuid);
  40. }
  41.  
  42. socket.emit("login", client_uuid);
  43.  
  44. // values in --> window.agar
  45.  
  46. function isMe(cell){
  47. for (var i = 0; i < window.agar.myCells.length; i++){
  48. if (window.agar.myCells[i] == cell.id){
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54.  
  55. function getCell(){
  56. var me = [];
  57. for (var key in window.agar.allCells){
  58. var cell = window.agar.allCells[key];
  59. if (isMe(cell)){
  60. me.push(cell);
  61. }
  62. }
  63. return me[0];
  64. }
  65.  
  66. function emitPosition(){
  67. x = (mouseX - window.innerWidth / 2) / window.agar.drawScale + window.agar.rawViewport.x;
  68. y = (mouseY - window.innerHeight / 2) / window.agar.drawScale + window.agar.rawViewport.y;
  69.  
  70. if(!movetoMouse)
  71. {
  72. x = getCell().x;
  73. y = getCell().y;
  74. }
  75.  
  76. socket.emit("pos", {"x": x, "y": y} );
  77. }
  78.  
  79. function emitSplit(){
  80. socket.emit("cmd", {"name":"split"} );
  81. }
  82.  
  83. function emitMassEject(){
  84. socket.emit("cmd", {"name":"eject"} );
  85. }
  86.  
  87. function toggleMovement(){
  88. canMove = !canMove;
  89.  
  90. switch(canMove)
  91. {
  92. case true:
  93. canvas.onmousemove = moveEvent[0];
  94. moveEvent[0] = null;
  95.  
  96. canvas.onmousedown = moveEvent[1];
  97. moveEvent[1] = null;
  98. break;
  99.  
  100. case false:
  101. canvas.onmousemove({clientX: innerWidth / 2, clientY: innerHeight / 2});
  102.  
  103. moveEvent[0] = canvas.onmousemove;
  104. canvas.onmousemove = null;
  105.  
  106. moveEvent[1] = canvas.onmousedown;
  107. canvas.onmousedown = null;
  108. break;
  109. }
  110. }
  111.  
  112. interval_id = setInterval(function() {
  113. emitPosition();
  114. }, 100);
  115.  
  116. interval_id2 = setInterval(function() {
  117. transmit_game_server_if_changed();
  118. }, 5000);
  119.  
  120. document.addEventListener('keydown',function(e){
  121. var key = e.keyCode || e.which;
  122. switch(key)
  123. {
  124. case 65://a has been pressed. (Toggle Position)
  125. movetoMouse = !movetoMouse;
  126. break;
  127.  
  128. case 68://d has been pressed. (Toggle Movement)
  129. toggleMovement();
  130. break;
  131.  
  132. case 69://e has been pressed. (Split Bots)
  133. emitSplit();
  134. break;
  135.  
  136. case 82://r has been pressed. (Eject Mass from Bots)
  137. emitMassEject();
  138. break;
  139. }
  140. });
  141.  
  142. function transmit_game_server_if_changed(){
  143. if(last_transmited_game_server != window.agar.ws){
  144. transmit_game_server();
  145. }
  146. }
  147.  
  148. function transmit_game_server(){
  149. last_transmited_game_server = window.agar.ws;
  150. socket.emit("cmd", {"name":"connect_server", "ip": last_transmited_game_server } );
  151. }
  152.  
  153. var mouseX = 0;
  154. var mouseY = 0;
  155.  
  156. $("body").mousemove(function( event ) {
  157. mouseX = event.clientX;
  158. mouseY = event.clientY;
  159. });
  160.  
  161. window.agar.minScale = -30;
  162. }, 5000);
  163.  
  164. //EXPOSED CODE BELOW
  165.  
  166. var allRules = [
  167. { hostname: ["agar.io"],
  168. scriptUriRe: /^http:\/\/agar\.io\/main_out\.js/,
  169. replace: function (m) {
  170. m.removeNewlines()
  171.  
  172. m.replace("var:allCells",
  173. /(=null;)(\w+)(.hasOwnProperty\(\w+\)?)/,
  174. "$1" + "$v=$2;" + "$2$3",
  175. "$v = {}")
  176.  
  177. m.replace("var:myCells",
  178. /(case 32:)(\w+)(\.push)/,
  179. "$1" + "$v=$2;" + "$2$3",
  180. "$v = []")
  181.  
  182. m.replace("var:top",
  183. /case 49:[^:]+?(\w+)=\[];/,
  184. "$&" + "$v=$1;",
  185. "$v = []")
  186.  
  187. m.replace("var:ws",
  188. /new WebSocket\((\w+)[^;]+?;/,
  189. "$&" + "$v=$1;",
  190. "$v = ''")
  191.  
  192. m.replace("var:topTeams",
  193. /case 50:(\w+)=\[];/,
  194. "$&" + "$v=$1;",
  195. "$v = []")
  196.  
  197. var dr = "(\\w+)=\\w+\\.getFloat64\\(\\w+,!0\\);\\w+\\+=8;\\n?"
  198. var dd = 7071.067811865476
  199. m.replace("var:dimensions",
  200. RegExp("case 64:"+dr+dr+dr+dr),
  201. "$&" + "$v = [$1,$2,$3,$4],",
  202. "$v = " + JSON.stringify([-dd,-dd,dd,dd]))
  203.  
  204. var vr = "(\\w+)=\\w+\\.getFloat32\\(\\w+,!0\\);\\w+\\+=4;"
  205. m.save() &&
  206. m.replace("var:rawViewport:x,y var:disableRendering:1",
  207. /else \w+=\(29\*\w+\+(\w+)\)\/30,\w+=\(29\*\w+\+(\w+)\)\/30,.*?;/,
  208. "$&" + "$v0.x=$1; $v0.y=$2; if($v1)return;") &&
  209. m.replace("var:disableRendering:2 hook:skipCellDraw",
  210. /(\w+:function\(\w+\){)(if\(this\.\w+\(\)\){\+\+this\.[\w$]+;)/,
  211. "$1" + "if($v || $H(this))return;" + "$2") &&
  212. m.replace("var:rawViewport:scale",
  213. /Math\.pow\(Math\.min\(64\/\w+,1\),\.4\)/,
  214. "($v.scale=$&)") &&
  215. m.replace("var:rawViewport:x,y,scale",
  216. RegExp("case 17:"+vr+vr+vr),
  217. "$&" + "$v.x=$1; $v.y=$2; $v.scale=$3;") &&
  218. m.reset_("window.agar.rawViewport = {x:0,y:0,scale:1};" +
  219. "window.agar.disableRendering = false;") ||
  220. m.restore()
  221.  
  222.  
  223.  
  224. m.replace("reset",
  225. /new WebSocket\(\w+[^;]+?;/,
  226. "$&" + m.reset)
  227.  
  228. m.replace("property:scale",
  229. /function \w+\(\w+\){\w+\.preventDefault\(\);[^;]+;1>(\w+)&&\(\1=1\)/,
  230. `;${makeProperty("scale", "$1")};$&`)
  231.  
  232. m.replace("var:minScale",
  233. /;1>(\w+)&&\(\1=1\)/,
  234. ";$v>$1 && ($1=$v)",
  235. "$v = 1")
  236.  
  237. m.replace("var:region",
  238. /console\.log\("Find "\+(\w+\+\w+)\);/,
  239. "$&" + "$v=$1;",
  240. "$v = ''")
  241.  
  242. m.replace("cellProperty:isVirus",
  243. /((\w+)=!!\(\w+&1\)[\s\S]{0,400})((\w+).(\w+)=\2;)/,
  244. "$1$4.isVirus=$3")
  245.  
  246. m.replace("var:dommousescroll",
  247. /("DOMMouseScroll",)(\w+),/,
  248. "$1($v=$2),")
  249.  
  250. m.replace("var:skinF hook:cellSkin",
  251. /(\w+.fill\(\))(;null!=(\w+))/,
  252. "$1;" +
  253. "if($v)$3 = $v(this,$3);" +
  254. "if($h)$3 = $h(this,$3);" +
  255. "$2");
  256.  
  257. /*m.replace("bigSkin",
  258. /(null!=(\w+)&&\((\w+)\.save\(\),)(\3\.clip\(\),\w+=)(Math\.max\(this\.size,this\.\w+\))/,
  259. "$1" + "$2.big||" + "$4" + "($2.big?2:1)*" + "$5")*/
  260.  
  261. m.replace("hook:afterCellStroke",
  262. /\((\w+)\.strokeStyle="#000000",\1\.globalAlpha\*=\.1,\1\.stroke\(\)\);\1\.globalAlpha=1;/,
  263. "$&" + "$H(this);")
  264.  
  265. m.replace("var:showStartupBg",
  266. /\w+\?\(\w\.globalAlpha=\w+,/,
  267. "$v && $&",
  268. "$v = true")
  269.  
  270.  
  271. var vAlive = /\((\w+)\[(\w+)\]==this\){\1\.splice\(\2,1\);/.exec(m.text)
  272. var vEaten = /0<this\.[$\w]+&&(\w+)\.push\(this\)}/.exec(m.text)
  273. !vAlive && console.error("Expose: can't find vAlive")
  274. !vEaten && console.error("Expose: can't find vEaten")
  275. if (vAlive && vEaten)
  276. m.replace("var:aliveCellsList var:eatenCellsList",
  277. RegExp(vAlive[1] + "=\\[\\];" + vEaten[1] + "=\\[\\];"),
  278. "$v0=" + vAlive[1] + "=[];" + "$v1=" + vEaten[1] + "=[];",
  279. "$v0 = []; $v1 = []")
  280.  
  281. m.replace("hook:drawScore",
  282. /(;(\w+)=Math\.max\(\2,(\w+\(\))\);)0!=\2&&/,
  283. "$1($H($3))||0!=$2&&")
  284.  
  285. m.replace("hook:beforeTransform hook:beforeDraw var:drawScale",
  286. /(\w+)\.save\(\);\1\.translate\((\w+\/2,\w+\/2)\);\1\.scale\((\w+),\3\);\1\.translate\((-\w+,-\w+)\);/,
  287. "$v = $3;$H0($1,$2,$3,$4);" + "$&" + "$H1($1,$2,$3,$4);",
  288. "$v = 1")
  289.  
  290. m.replace("hook:afterDraw",
  291. /(\w+)\.restore\(\);(\w+)&&\2\.width&&\1\.drawImage/,
  292. "$H();" + "$&")
  293.  
  294.  
  295. m.replace("hook:cellColor",
  296. /(\w+=)this\.color;/,
  297. "$1 ($h && $h(this, this.color) || this.color);")
  298.  
  299. m.replace("var:drawGrid",
  300. /(\w+)\.globalAlpha=(\.2\*\w+);/,
  301. "if(!$v)return;" + "$&",
  302. "$v = true")
  303.  
  304. m.replace("hook:drawCellMass",
  305. /&&\((\w+\|\|0==\w+\.length&&\(!this\.\w+\|\|this\.\w+\)&&20<this\.size)\)&&/,
  306. "&&( $h ? $h(this,$1) : ($1) )&&")
  307.  
  308. m.replace("hook:cellMassText",
  309. /(\.\w+)(\(~~\(this\.size\*this\.size\/100\)\))/,
  310. "$1( $h ? $h(this,$2) : $2 )")
  311.  
  312. m.replace("hook:cellMassTextScale",
  313. /(\.\w+)\((this\.\w+\(\))\)([\s\S]{0,1000})\1\(\2\/2\)/,
  314. "$1($2)$3$1( $h ? $h(this,$2/2) : ($2/2) )")
  315.  
  316. var template = (key,n) =>
  317. `this\\.${key}=\\w+\\*\\(this\\.(\\w+)-this\\.(\\w+)\\)\\+this\\.\\${n};`
  318. var re = new RegExp(template('x', 2) + template('y', 4) + template('size', 6))
  319. var match = re.exec(m.text)
  320. if (match) {
  321. m.cellProp.nx = match[1]
  322. m.cellProp.ny = match[3]
  323. m.cellProp.nSize = match[5]
  324. } else
  325. console.error("Expose: cellProp:x,y,size search failed!")
  326.  
  327. }},
  328. ]
  329.  
  330. function makeProperty(name, varname) {
  331. return "'" + name + "' in window.agar || " +
  332. "Object.defineProperty( window.agar, '"+name+"', " +
  333. "{get:function(){return "+varname+"},set:function(){"+varname+"=arguments[0]},enumerable:true})"
  334. }
  335.  
  336. if (window.top != window.self)
  337. return
  338.  
  339. if (document.readyState !== 'loading')
  340. return console.error("Expose: this script should run at document-start")
  341.  
  342. var isFirefox = /Firefox/.test(navigator.userAgent)
  343.  
  344. // Stage 1: Find corresponding rule
  345. var rules
  346. for (var i = 0; i < allRules.length; i++)
  347. if (allRules[i].hostname.indexOf(window.location.hostname) !== -1) {
  348. rules = allRules[i]
  349. break
  350. }
  351. if (!rules)
  352. return console.error("Expose: cant find corresponding rule")
  353.  
  354.  
  355. // Stage 2: Search for `main_out.js`
  356. if (isFirefox) {
  357. function bse_listener(e) { tryReplace(e.target, e) }
  358. window.addEventListener('beforescriptexecute', bse_listener, true)
  359. } else {
  360. // Iterate over document.head child elements and look for `main_out.js`
  361. for (var i = 0; i < document.head.childNodes.length; i++)
  362. if (tryReplace(document.head.childNodes[i]))
  363. return
  364. // If there are no desired element in document.head, then wait until it appears
  365. function observerFunc(mutations) {
  366. for (var i = 0; i < mutations.length; i++) {
  367. var addedNodes = mutations[i].addedNodes
  368. for (var j = 0; j < addedNodes.length; j++)
  369. if (tryReplace(addedNodes[j]))
  370. return observer.disconnect()
  371. }
  372. }
  373. var observer = new MutationObserver(observerFunc)
  374. observer.observe(document.head, {childList: true})
  375. }
  376.  
  377. // Stage 3: Replace found element using rules
  378. function tryReplace(node, event) {
  379. var scriptLinked = rules.scriptUriRe && rules.scriptUriRe.test(node.src)
  380. var scriptEmbedded = rules.scriptTextRe && rules.scriptTextRe.test(node.textContent)
  381. if (node.tagName != "SCRIPT" || (!scriptLinked && !scriptEmbedded))
  382. return false // this is not desired element; get back to stage 2
  383.  
  384. if (isFirefox) {
  385. event.preventDefault()
  386. window.removeEventListener('beforescriptexecute', bse_listener, true)
  387. }
  388.  
  389. var mod = {
  390. reset: "",
  391. text: null,
  392. history: [],
  393. cellProp: {},
  394. save() {
  395. this.history.push({reset:this.reset, text:this.text})
  396. return true
  397. },
  398. restore() {
  399. var state = this.history.pop()
  400. this.reset = state.reset
  401. this.text = state.text
  402. return true
  403. },
  404. reset_(reset) {
  405. this.reset += reset
  406. return true
  407. },
  408. replace(what, from, to, reset) {
  409. var vars = [], hooks = []
  410. what.split(" ").forEach((x) => {
  411. x = x.split(":")
  412. x[0] === "var" && vars.push(x[1])
  413. x[0] === "hook" && hooks.push(x[1])
  414. })
  415. function replaceShorthands(str) {
  416. function nope(letter, array, fun) {
  417. str = str
  418. .split(new RegExp('\\$' + letter + '([0-9]?)'))
  419. .map((v,n) => n%2 ? fun(array[v||0]) : v)
  420. .join("")
  421. }
  422. nope('v', vars, (name) => "window.agar." + name)
  423. nope('h', hooks, (name) => "window.agar.hooks." + name)
  424. nope('H', hooks, (name) =>
  425. "window.agar.hooks." + name + "&&" +
  426. "window.agar.hooks." + name)
  427. return str
  428. }
  429. var newText = this.text.replace(from, replaceShorthands(to))
  430. if(newText === this.text) {
  431. console.error("Expose: `" + what + "` replacement failed!")
  432. return false
  433. } else {
  434. this.text = newText
  435. if (reset)
  436. this.reset += replaceShorthands(reset) + ";"
  437. return true
  438. }
  439. },
  440. removeNewlines() {
  441. this.text = this.text.replace(/([,\/])\n/mg, "$1")
  442. },
  443. get: function() {
  444. var cellProp = JSON.stringify(this.cellProp)
  445. return `window.agar={hooks:{},cellProp:${cellProp}};` +
  446. this.reset + this.text
  447. }
  448. }
  449.  
  450. if (scriptEmbedded) {
  451. mod.text = node.textContent
  452. rules.replace(mod)
  453. if (isFirefox) {
  454. document.head.removeChild(node)
  455. var script = document.createElement("script")
  456. script.textContent = mod.get()
  457. document.head.appendChild(script)
  458. } else {
  459. node.textContent = mod.get()
  460. }
  461. console.log("Expose: replacement done")
  462. } else {
  463. document.head.removeChild(node)
  464. var request = new XMLHttpRequest()
  465. request.onload = function() {
  466. var script = document.createElement("script")
  467. mod.text = this.responseText
  468. rules.replace(mod)
  469. script.textContent = mod.get()
  470. // `main_out.js` should not executed before jQuery was loaded, so we need to wait jQuery
  471. function insertScript(script) {
  472. if (typeof jQuery === "undefined")
  473. return setTimeout(insertScript, 0, script)
  474. document.head.appendChild(script)
  475. console.log("Expose: replacement done")
  476. }
  477. insertScript(script)
  478. }
  479. request.onerror = function() { console.error("Expose: response was null") }
  480. request.open("get", node.src, true)
  481. request.send()
  482. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement