Join Random Game Mode

Join a new game on your region with the highest player count for a random game mode that is not full when you press a certain key

  1. // ==UserScript==
  2. // @name Join Random Game Mode
  3. // @namespace violentmonkey
  4. // @version 3.1.3.3.7
  5. // @description Join a new game on your region with the highest player count for a random game mode that is not full when you press a certain key
  6. // @author hitthemoney, moongazer07
  7. // @match *://krunker.io/*
  8. // @exclude *://krunker.io/social.html*
  9. // @exclude *://krunker.io/editor.html*
  10. // @grant GM_xmlhttpRequest
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. const joinKey = "F4"; // Join game shortcut key, case sensitive
  15. const gameModes = [0, 1, 2, 3, 10, 13, 14, 19, 21, 25, 27, 28, 29, 33, 34]; // Game mode IDs
  16.  
  17. const getRandomGameMode = async () => {
  18. return new Promise((resolve, reject) => {
  19. GM_xmlhttpRequest({
  20. method: "GET",
  21. url: "https://www.random.org/integers/?num=1&min=0&max=14&col=1&base=10&format=plain&rnd=new",
  22. onload: function(response) {
  23. if (response.status === 200) {
  24. resolve(gameModes[parseInt(response.responseText.trim())]);
  25. } else {
  26. reject(new Error("Failed to fetch random number from Random.org"));
  27. }
  28. },
  29. onerror: function(error) {
  30. reject(new Error("Failed to fetch random number from Random.org"));
  31. }
  32. });
  33. });
  34. };
  35.  
  36. const getTopGame = async () => {
  37. const region = window.localStorage.pingRegion7 || (await fetch(`https://matchmaker.krunker.io/game-info?game=${encodeURIComponent(window.getGameActivity().id)}`).then(res => res.json()));
  38. const gameData = (await fetch(`https://matchmaker.krunker.io/game-list?hostname=${window.location.hostname}`).then(res => res.json())).games;
  39. const targetGameMode = await getRandomGameMode();
  40.  
  41. const filData = gameData.filter(game =>
  42. game[1] === region &&
  43. game[4].g === targetGameMode &&
  44. game[4].c === 0 &&
  45. game[2] < game[3]
  46. ).sort((x, y) => y[2] - x[2]);
  47. return filData[0];
  48. }
  49.  
  50. const joinTopGame = async () => {
  51. const targetGame = await getTopGame();
  52. window.location.href = `/?game=${targetGame[0]}`;
  53. }
  54.  
  55. window.addEventListener("keydown", async (event) => {
  56. switch (event.key) {
  57. case joinKey: {
  58. await joinTopGame();
  59. break;
  60. }
  61. }
  62. });