Advertisement
xosski

“Hey boss I’m working!” Automation

Jan 2nd, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. const robot = require("robotjs");
  2. const clip = require('clipboardy');
  3. const words = [
  4. // Your predefined list of words...
  5. "React", "JavaScript", "Frontend", "React Hooks", //... etc
  6. ];
  7.  
  8. let running = true; // Flag to control the infinite loop
  9.  
  10. // Start the bot
  11. function startBot() {
  12. if (!running) return; // Stop the bot if the flag is false
  13.  
  14. setTimeout(() => {
  15. try {
  16. shiftApp();
  17. searchInGoogle(() => {
  18. setTimeout(() => {
  19. shiftTab();
  20. setTimeout(() => {
  21. shiftApp();
  22. setTimeout(() => {
  23. shiftTab();
  24. robot.keyTap("enter");
  25. keyTabRepeat("down", 100, () => {
  26. startBot(); // Recursively restart bot
  27. });
  28. }, 15000);
  29. }, 25000);
  30. }, 30000);
  31. });
  32. } catch (error) {
  33. console.error('Error in startBot:', error);
  34. }
  35. }, 5000);
  36. }
  37.  
  38. // Close current browser tab
  39. function closeTab() {
  40. try {
  41. robot.keyTap("w", ["control"]);
  42. } catch (error) {
  43. console.error('Error closing tab:', error);
  44. }
  45. }
  46.  
  47. // Switch application using Alt + Tab
  48. function shiftApp() {
  49. try {
  50. robot.keyTap("tab", ["alt"]);
  51. robot.keyTap("enter");
  52. } catch (error) {
  53. console.error('Error shifting application:', error);
  54. }
  55. }
  56.  
  57. // Switch tabs using Control + Tab
  58. function shiftTab() {
  59. try {
  60. robot.keyTap("tab", ["control"]);
  61. } catch (error) {
  62. console.error('Error shifting tab:', error);
  63. }
  64. }
  65.  
  66. // Perform a Google search using a random word from the array
  67. function searchInGoogle(callback) {
  68. setTimeout(() => {
  69. const len = words.length - 1;
  70. const randomIndex = Math.floor(Math.random() * len);
  71. const word = words[randomIndex];
  72.  
  73. try {
  74. setTimeout(() => {
  75. robot.keyTap("t", ["control"]); // Open new tab
  76. }, 1000);
  77.  
  78. setTimeout(() => {
  79. if (word.indexOf("http") >= 0) {
  80. clip.writeSync(word);
  81. robot.keyTap("v", ["control"]); // Paste from clipboard
  82. } else {
  83. typeString(word); // Type the word
  84. }
  85.  
  86. robot.keyTap("enter"); // Press enter
  87.  
  88. setTimeout(() => {
  89. keyTabRepeat("down", 30, () => {
  90. // Add any additional logic you want to repeat
  91. robot.mouseClick();
  92. callback(); // Execute callback after interaction
  93. });
  94. }, 2000);
  95. }, 1000);
  96. } catch (error) {
  97. console.error('Error in searchInGoogle:', error);
  98. }
  99. }, 5000);
  100. }
  101.  
  102. // Simulate pressing a key multiple times
  103. function keyTabRepeat(key, times, callback) {
  104. try {
  105. for (let i = 0; i < times; i++) {
  106. robot.keyTap(key);
  107. }
  108. callback(); // Execute callback after the loop
  109. } catch (error) {
  110. console.error(`Error in keyTabRepeat (key: ${key}, times: ${times}):`, error);
  111. }
  112. }
  113.  
  114. // Type a string using robotjs
  115. function typeString(str) {
  116. try {
  117. for (let i = 0; i < str.length; i++) {
  118. robot.keyTap(str[i]);
  119. }
  120. } catch (error) {
  121. console.error('Error typing string:', error);
  122. }
  123. }
  124.  
  125. // Stop the bot from running
  126. function stopBot() {
  127. running = false; // Set the flag to stop the bot
  128. console.log("Bot has been stopped.");
  129. }
  130.  
  131. // Optional: Introduce a key press event to stop the bot manually (for example: pressing 'Esc')
  132. function listenForStop() {
  133. setInterval(() => {
  134. // Check for a specific key press to stop the bot (Esc key in this case)
  135. const key = robot.keyTap('esc');
  136. if (key) {
  137. stopBot(); // Stop the bot if 'Esc' is pressed
  138. }
  139. }, 1000); // Check every second for key press
  140. }
  141.  
  142. // Start listening for stop event
  143. listenForStop();
  144.  
  145. // Start the bot when the script is run
  146. startBot();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement