Fullscreen - Bonk.io

Adds a button to enter fullscreen

  1. // ==UserScript==
  2. // @name Fullscreen - Bonk.io
  3. // @description Adds a button to enter fullscreen
  4. // @author Excigma
  5. // @namespace https://github.com/Excigma
  6. // @license GPL-3.0
  7. // @version 0.0.5
  8. // @match https://bonk.io/*
  9. // @grant GM_addStyle
  10. // @grant unsafeWindow
  11. // @run-at document-idle
  12. // ==/UserScript==
  13.  
  14. (() => {
  15. if (unsafeWindow.parent !== unsafeWindow) {
  16. let fullscreen = false;
  17.  
  18. const parent_document = unsafeWindow.parent.document;
  19. const fullscreen_button = document.createElement("div");
  20. const pretty_top_bar = document.getElementById("pretty_top_bar");
  21. const button_count = document.querySelectorAll("#pretty_top_bar>.pretty_top_button.niceborderleft").length;
  22. const map_editor_style = document.getElementById("mapeditor").style;
  23. const lobby_style = document.getElementById("newbonklobby").style;
  24.  
  25. fullscreen_button.id = "pretty_top_fullscreen";
  26. fullscreen_button.classList.add("pretty_top_button", "niceborderleft");
  27. pretty_top_bar.appendChild(fullscreen_button);
  28. // Thanks to kklkkj for this
  29. // https://github.com/kklkkj/kklee/blob/3d93fb10134bfc6c0b9bd98b413edc511a53ae21/src/injector.js#L283
  30. const limit_size = () => {
  31. const max_width = `${Math.min(unsafeWindow.innerWidth / 730, unsafeWindow.innerHeight / 500) * 730 * 0.9}px`;
  32. map_editor_style.maxHeight = "calc(100vh - 70px)";
  33. lobby_style.maxHeight = "calc(100vh - 70px)";
  34. map_editor_style.maxWidth = max_width;
  35. lobby_style.maxWidth = max_width;
  36. };
  37.  
  38. fullscreen_button.addEventListener("click", () => {
  39. fullscreen = !fullscreen;
  40.  
  41. if (fullscreen) {
  42. document.body.classList.add("fullscreen");
  43. parent_document.body.classList.add("fullscreen");
  44. // Limit the size of the chat and map editor
  45. limit_size();
  46. } else {
  47. document.body.classList.remove("fullscreen");
  48. parent_document.body.classList.remove("fullscreen");
  49.  
  50. // Stop limiting the size
  51. map_editor_style.maxHeight = "calc(100vh - 70px)";
  52. lobby_style.maxHeight = "100%";
  53. map_editor_style.maxWidth = "90vw";
  54. lobby_style.maxWidth = "100%";
  55. }
  56. });
  57. unsafeWindow.addEventListener("resize", () => {
  58. if (fullscreen) setTimeout(limit_size, 50);
  59. });
  60.  
  61. // eslint-disable-next-line no-undef
  62. GM_addStyle(`
  63. #pretty_top_fullscreen {
  64. width: 58px;
  65. height: 34px;
  66. background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 24 24'%3E%3Cpath d='M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z'/%3E%3C/svg%3E");
  67. background-size: 24px 24px;
  68. background-repeat: no-repeat;
  69. background-position: center;
  70. position: absolute;
  71. right: ${(button_count * 58) + 1}px;
  72. top: 0;
  73. }
  74.  
  75. .fullscreen #pretty_top_fullscreen {
  76. background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 24 24'%3E%3Cpath d='M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z'/%3E%3C/svg%3E") !important;
  77. }
  78.  
  79. /* Move the game frame to the top, so the adverts do not interfere with clicking*/
  80. .fullscreen #maingameframe {
  81. z-index: 10000;
  82. }
  83.  
  84. /* Makes the main game section have no border, have 100% of it's parent's height, and 100% of it's parent's width (in this case the size of your screen */
  85. .fullscreen #bonkiocontainer {
  86. height: 100vh !important;
  87. width: 100vw !important;
  88. border: none !important;
  89. }
  90.  
  91. /* Makes the countdown partially transparent, as some maps spawn you there */
  92. .fullscreen #ingamecountdown {
  93. opacity: 75%;
  94. }
  95.  
  96. /* The actual canvas where the game is drawn; this does NOT include buttons, menus etc */
  97. /* Tells it to make the position absolute on the screen (doesn't care about parent) and move it to the right? idk what i did here lol i wrote it ages ago but it works */
  98. /* also hides the cursor while you're hovering over the game canvas */
  99. .fullscreen #gamerenderer {
  100. text-align: center !important;
  101. }
  102.  
  103. .fullscreen #gamerenderer>canvas {
  104. display: inline-block !important;
  105. }
  106.  
  107. /* Center the Bonk.io main menu replays */
  108. .fullscreen #bgreplay {
  109. text-align: center !important;
  110. }
  111.  
  112. .fullscreen #bgreplay>canvas {
  113. display: inline-block !important;
  114. }
  115.  
  116. /* Moves the XP bar down a bit, so it's visible when in fullscreen*/
  117. .fullscreen #xpbarcontainer {
  118. top: -3px !important;
  119. }
  120. `);
  121. } else {
  122. // eslint-disable-next-line no-undef
  123. GM_addStyle(`
  124. .fullscreen #maingameframe {
  125. position: fixed !important;
  126. margin-top: 0px !important;
  127. z-index: 99999 !important;
  128. }
  129. .fullscreen #bonkioheader {
  130. display:none;
  131. }
  132. body.fullscreen {
  133. overflow: hidden;
  134. }
  135. `);
  136. }
  137. })();