Bot for typeracer.com

A simple bot for typeracer.

  1. // ==UserScript==
  2. // @name Bot for typeracer.com
  3. // @namespace type_bot
  4. // @include http://play.typeracer.com/
  5. // @version 1.03
  6. // @grant none
  7. // @description:en A simple bot for typeracer.
  8. // @description A simple bot for typeracer.
  9. // ==/UserScript==
  10.  
  11.  
  12. var CURRENT_WORD_ID = "nhwMiddlegwt-uid-6";
  13. var ADDITIONAL_WORD_ID = "nhwMiddleCommagwt-uid-7";
  14. var INPUT_CLASS = "txtInput";
  15. var INPUT_CLASS_UNFOCUSED = "txtInput txtInput-unfocused";
  16. var CLASS_OF_START = "mainMenuItem mainMenuItem-highlighted";
  17. var CLASS_OF_START_PRACTICE = "mainMenuItem mainMenuItem-secondary";
  18. var CONTAINER_ELEM_ID = "dUI";
  19. var RACE_AGAIN_CLASS = "raceAgainLink";
  20. var CONTAINER_SINGLE_PLAYER_TO_NORMAL_RACE = "roomSection";
  21. var DEFAULT_SPEED = 700;
  22. var tb_container = document.getElementsByClassName("themeContent").item(0);
  23. var button_text_active = "Bot activated";
  24. var button_text_deactivated = "Bot deactivated";
  25. var INFO_TXT = "Reload page after every race.";
  26. var bot_input;
  27. var words;
  28. var tb_input;
  29. var tb_thread;
  30. var tb_info;
  31. var ev;
  32. var space;
  33. ev = document.createEvent("KeyboardEvent");
  34. ev.initKeyEvent("keypress", true, false, window, 0, 0, 0, 0, 13, 13);
  35. space = document.createEvent("KeyboardEvent");
  36. space.initKeyEvent("keypress",true,false, window, 0,0,0,0,32,32);
  37.  
  38.  
  39. if(localStorage.type_bot === undefined)
  40. localStorage.type_bot = "active";
  41.  
  42. var waitforElem = setInterval(function(){
  43. if(document.getElementsByClassName(CLASS_OF_START).item(0) != null &&
  44. document.getElementsByClassName(CLASS_OF_START_PRACTICE).item(0) != null){
  45. console.log("waiting...");
  46. var RACE_ELEM = document.getElementsByClassName(CLASS_OF_START).item(0);//.item(NUMBER_RACE);
  47. var PRACTICE_ELEM = document.getElementsByClassName(CLASS_OF_START_PRACTICE).item(0);//.item(NUMBER_PRACTICE);
  48. RACE_ELEM.addEventListener("click",tb_init);
  49. PRACTICE_ELEM.addEventListener("click",tb_init);
  50. clearInterval(waitforElem);
  51. }
  52. },100);
  53.  
  54.  
  55. function tb_init(){
  56. var bot_button = document.createElement("input");
  57. bot_button.id = "bot_button";
  58. bot_button.type = "button";
  59. if(localStorage.type_bot == "active")
  60. bot_button.value = button_text_active;
  61. else bot_button.value = button_text_deactivated;
  62. bot_button.style.position = "absolute";
  63. bot_button.style.top = "100px";
  64. bot_button.style.left = "50px";
  65. bot_button.addEventListener("click",button_pressed);
  66. bot_input = document.createElement("input");
  67. bot_input.id = "bot_input";
  68. bot_input.type = "text";
  69. bot_input.value = DEFAULT_SPEED;
  70. bot_input.style.top = "150px";
  71. bot_input.style.left = "50px";
  72. bot_input.style.position = "absolute";
  73. bot_input.title = "Milliseconds between each word";
  74. tb_info = document.createTextNode(INFO_TXT);
  75. var tb_div = document.createElement("div");
  76. tb_div.style.position = "absolute";
  77. tb_div.style.top = "200px";
  78. tb_div.style.left = "50px";
  79. tb_div.appendChild(tb_info);
  80. tb_container.appendChild(tb_div);
  81. tb_container.appendChild(bot_input);
  82. tb_container.appendChild(bot_button);
  83. setTimeout(tb_wait,2000);
  84. }
  85.  
  86. function button_pressed(event){
  87. if(localStorage.type_bot == "active"){
  88. localStorage.type_bot = "unactive";
  89. event.target.value = button_text_deactivated;
  90. bot_input.disabled = true;
  91. }
  92. else{
  93. localStorage.type_bot = "active";
  94. event.target.value = button_text_active;
  95. bot_input.disabled = false;
  96. }
  97. }
  98.  
  99. function tb_wait(){
  100. tb_input = document.getElementsByClassName(INPUT_CLASS_UNFOCUSED).item(0);
  101. tb_thread = window.setInterval(function(){
  102. if(tb_input.className == INPUT_CLASS)
  103. tb_type(tb_input);
  104. },100);
  105. }
  106.  
  107. function tb_type(input){
  108. clearInterval(tb_thread);
  109. if(localStorage.type_bot == "active"){
  110. var WAIT_BETWEEN_WORDS = parseInt(bot_input.value);
  111. if(isNaN(WAIT_BETWEEN_WORDS) || !WAIT_BETWEEN_WORDS)
  112. WAIT_BETWEEN_WORDS = DEFAULT_SPEED;
  113. words = document.getElementById(CURRENT_WORD_ID);
  114. additional = document.getElementById(ADDITIONAL_WORD_ID);
  115. var word = words.innerHTML;
  116. var add = additional.innerHTML;
  117. tb_thread = window.setInterval(function(){
  118. if(word == null)
  119. clearInterval(tb_thread);
  120.  
  121. tb_input.value += word + add;
  122. tb_input.dispatchEvent(space);
  123.  
  124. word = words.innerHTML;
  125. add = additional.innerHTML;
  126.  
  127. },WAIT_BETWEEN_WORDS);
  128. }
  129. }