Fullscreen - Bonk.io

Adds a button to enter fullscreen

当前为 2021-11-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Fullscreen - Bonk.io
  3. // @description Adds a button to enter fullscreen
  4. // @author Excigma
  5. // @namespace https://greasyfork.org/users/416480
  6. // @license GPL-3.0
  7. // @version 0.0.2
  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 scale = Math.min(unsafeWindow.innerWidth / 730, unsafeWindow.innerHeight / 500);
  32. map_editor_style.maxHeight = `${scale * 500 * 0.9}px`;
  33. map_editor_style.maxWidth = `${scale * 730 * 0.9}px`;
  34. lobby_style.maxHeight = `${scale * 500 * 0.9}px`;
  35. lobby_style.maxWidth = `${scale * 730 * 0.9}px`;
  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_size();
  45. } else {
  46. document.body.classList.remove("fullscreen");
  47. parent_document.body.classList.remove("fullscreen");
  48.  
  49. // Stop limiting the size
  50. lobby_style.maxHeight = "100%";
  51. lobby_style.maxWidth = "100%";
  52. map_editor_style.maxHeight = "100%";
  53. map_editor_style.maxWidth = "100%";
  54. }
  55. });
  56. unsafeWindow.addEventListener("resize", () => {
  57. if (fullscreen) limit_size();
  58. });
  59.  
  60. // eslint-disable-next-line no-undef
  61. GM_addStyle(`
  62. #pretty_top_fullscreen {
  63. width: 58px;
  64. height: 34px;
  65. 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");
  66. background-size: 24px 24px;
  67. background-repeat: no-repeat;
  68. background-position: center;
  69. position: absolute;
  70. right: ${(button_count * 58) + 1}px;
  71. top: 0;
  72. }
  73.  
  74. .fullscreen #pretty_top_fullscreen {
  75. 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;
  76. }
  77.  
  78. /* Move the game frame to the top, so the adverts do not interfere with clicking*/
  79. .fullscreen #maingameframe {
  80. z-index: 10000;
  81. }
  82.  
  83. /* 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 */
  84. .fullscreen #bonkiocontainer {
  85. height: 100vh !important;
  86. width: 100vw !important;
  87. border: none !important;
  88. }
  89.  
  90. /* Makes the countdown partially transparent, as some maps spawn you there */
  91. .fullscreen #ingamecountdown {
  92. opacity: 75%;
  93. }
  94.  
  95. /* The actual canvas where the game is drawn; this does NOT include buttons, menus etc */
  96. /* 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 */
  97. /* also hides the cursor while you're hovering over the game canvas */
  98. .fullscreen #gamerenderer {
  99. text-align: center !important;
  100. }
  101.  
  102. .fullscreen #gamerenderer>canvas {
  103. display: inline-block !important
  104. }
  105.  
  106. /* Center the Bonk.io main menu replays */
  107. .fullscreen #bgreplay {
  108. text-align: center !important;
  109. }
  110.  
  111. .fullscreen #bgreplay>canvas {
  112. display: inline-block !important
  113. }
  114.  
  115. /* Moves the XP bar down a bit, so it's visible when in fullscreen*/
  116. .fullscreen #xpbarcontainer {
  117. transform: translateY(5px);
  118. }
  119. `);
  120. } else {
  121. // eslint-disable-next-line no-undef
  122. GM_addStyle(`
  123. .fullscreen #maingameframe {
  124. position: fixed !important;
  125. margin-top: 0px !important;
  126. z-index: 99999 !important;
  127. }
  128. .fullscreen #bonkioheader {
  129. display:none;
  130. }
  131. body.fullscreen {
  132. overflow-y: hidden;
  133. }
  134. `);
  135. }
  136. })();