Starve.io autoAtack and autoCraftItems

try to take over the world!

当前为 2020-09-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Starve.io autoAtack and autoCraftItems
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://starve.io/
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. //polyfills
  13. function Event(e,t){this.script=e,this.target=t,this._cancel=!1,this._replace=null,this._stop=!1}Event.prototype.preventDefault=function(){this._cancel=!0},Event.prototype.stopPropagation=function(){this._stop=!0},Event.prototype.replacePayload=function(e){this._replace=e};var callbacks=[],addBeforeScriptExecuteListener=function(e){if(!e instanceof Function)throw new Error("Event handler must be a function.");callbacks.push(e)},removeBeforeScriptExecuteListener=function(e){for(var t=callbacks.length;t--;)callbacks[t]===e&&callbacks.splice(t,1)},addev=window.addEventListener.bind(window),rmev=window.removeEventListener.bind(window);window.addEventListener=function(){"beforescriptexecute"===arguments[0].toLowerCase()?addBeforeScriptExecuteListener(arguments[1]):addev.apply(null,arguments)},window.removeEventListener=function(){"beforescriptexecute"===arguments[0].toLowerCase()?removeBeforeScriptExecuteListener(arguments[1]):rmev.apply(null,arguments)};var dispatch=function(e,t){var r=new Event(e,t);if(window.onbeforescriptexecute instanceof Function)try{window.onbeforescriptexecute(r)}catch(e){console.error(e)}for(var n=0;n<callbacks.length&&!r._stop;n++)try{callbacks[n](r)}catch(e){console.error(e)}return r},observer=new MutationObserver(e=>{for(var t=0;t<e.length;t++)for(var r=0;r<e[t].addedNodes.length;r++){var n=e[t].addedNodes[r];if("SCRIPT"===n.tagName){var o=dispatch(n,e[t].target);o._cancel?n.remove():"string"==typeof o._replace&&(n.textContent=o._replace)}}});observer.observe(document,{childList:!0,subtree:!0});
  14. //polyfills
  15.  
  16. //options
  17. let options={
  18. gameFuncName:"wi",
  19. fillRectFuncName:"fh"
  20. };
  21. let ws;
  22. let circleTime;
  23. let commands={lastCraftCommand:{keyCode:"KeyE",wsSend:"[7,49]"},
  24. meatCraft:{keyCode:"KeyZ",wsSend:"[7,49]"},
  25. bandageCraft:{keyCode:"KeyX",wsSend:"[7,54]"},
  26. fillBottle:{keyCode:"KeyC",wsSend:"[7,111]"},
  27. autoClick:{keyCode:"KeyQ",active:false,wsSend:"0"}
  28. }
  29. let oldfunc={};
  30. let newfunc={};
  31. //options
  32. //hooks
  33. /* //If there is an update, bring the new function name.
  34. oldfunc['String.indexOf']=String.prototype.indexOf;
  35. String.prototype.indexOf=newfunc['String.indexOf']=new Proxy(String.prototype.indexOf,{
  36. apply:function(target, thisArg, argArray){
  37. if(argArray[0]=="http://starve.io/beta"){ console.log(arguments.callee.caller.name)
  38. String.prototype.indexOf=oldfunc['String.indexOf'];
  39. }
  40. return target.apply(thisArg, argArray);
  41. }});
  42. */
  43. oldfunc.webSocket=window.WebSocket;
  44. window.WebSocket=newfunc.webSocket=new Proxy(window.WebSocket,{
  45. construct:function(target,args){
  46. ws = new target(...args);
  47.  
  48. const messageHandler = (e) => {
  49. if ("string" == typeof e.data){
  50.  
  51. }else{
  52. var d = new Uint8Array(e.data);
  53. switch (d[0]) {
  54. case 16:
  55. circleTime=Date.now();
  56. break;
  57. }
  58. }
  59. };
  60.  
  61. const closeHandler = (event) => {
  62. console.log('Close', event);
  63. ws.removeEventListener('message', messageHandler);
  64. ws.removeEventListener('close', closeHandler);
  65. };
  66. ws.addEventListener('message', messageHandler);
  67. ws.addEventListener('close', closeHandler);
  68.  
  69. oldfunc['ws.send']=ws.send;
  70. newfunc['ws.send']= ws.send= new Proxy(ws.send, {
  71. apply: function(target, thisArg, args) {
  72. if(typeof args[0]==='string' ){
  73. let arr=JSON.parse(args[0]);
  74. if(arr[0]===7){
  75. commands.lastCraftCommand.wsSend=args[0];
  76. }else if(arr[0]===3){
  77. if(commands.autoClick.active){
  78. arr[0]=4;
  79. args[0]=JSON.stringify(arr);
  80. }else{
  81. commands.autoClick.wsSend=arr[1];
  82. }
  83. }
  84. }
  85. target.apply(thisArg, args);
  86. }
  87. });
  88. return ws;
  89. }
  90. });
  91. oldfunc['canvas.fillRect'] = CanvasRenderingContext2D.prototype.fillRect;
  92. newfunc['canvas.fillRect'] = CanvasRenderingContext2D.prototype.fillRect= new Proxy(CanvasRenderingContext2D.prototype.fillRect, {
  93. apply: function(target, _this, _arguments) {
  94. if(arguments.callee.caller.name===options.fillRectFuncName && _this.fillStyle==="#669bb1"){
  95. _this.fillStyle = "red";
  96. _this.font = "30px Arial";
  97. _this.fillText(`${circleTime? (5-(Date.now()-circleTime)/1000).toFixed(1):'5'}`,_arguments[0]+180,_arguments[1]+20);
  98. _this.fillStyle="#669bb1"
  99. }
  100. return Function.prototype.apply.apply(target, [_this, _arguments]);;
  101. }
  102. });
  103. //hooks
  104. //handler
  105. /* window.addEventListener('beforescriptexecute',e => {
  106. if (e.script.src.includes('c9')) {
  107. e.preventDefault();
  108. fetch('https://starve.io/js/c9.js').then(response=>response.text()).then((dataStr) => {
  109. let sc=document.createElement('script');
  110. sc.textContent=dataStr.replace(new RegExp('function '+options.gameFuncName+'\\(\\)\\{',"gi"),"function "+options.gameFuncName+"(){'object' === typeof this && window!==this && ((obj)=>{window.GAME=obj})(this);");
  111. e.target.append(sc);
  112. })
  113. }
  114. });*/
  115. document.addEventListener('keydown', (event)=>{
  116. if(event.code==="KeyQ"){
  117. commands.autoClick.active && ws.send("[14]");
  118. commands.autoClick.active=!commands.autoClick.active;
  119. commands.autoClick.active && ws.send(`[4,${commands.autoClick.wsSend}]`);
  120. }else{
  121. for (const [key, value] of Object.entries(commands)) {
  122. if(value.keyCode===event.code){
  123. ws.send(value.wsSend);
  124. }
  125. }
  126. }
  127. });
  128. document.addEventListener("DOMContentLoaded", function(event) {
  129. //style
  130. document.getElementById("game_canvas") && document.getElementById("game_canvas").style && (function(){document.getElementById("game_canvas").style.filter = "brightness(1.1)"})();
  131. //style
  132. });
  133. //handler
  134.  
  135.  
  136. })();