Kogama Keyboard Injector

Injects an Apple-like keyboard script into Kogama game pages automatically, including within iframes.

  1. // ==UserScript==
  2. // @name Kogama Keyboard Injector
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Injects an Apple-like keyboard script into Kogama game pages automatically, including within iframes.
  6. // @author YourName
  7. // @match *://www.kogama.com/*
  8. // @match *://friends.kogama.com/*
  9. // @match *://kogama.com.br/*
  10. // @match *://www.kogama.com.br/*
  11. // @match *://kogama.game/*
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // Function to create and inject the keyboard script
  19. function injectKeyboardScript() {
  20. const script = document.createElement('script');
  21. script.textContent = `
  22. // Your custom keyboard script here
  23. // Create keyboard container
  24. const keyboardContainer = document.createElement('div');
  25. keyboardContainer.style.position = 'fixed';
  26. keyboardContainer.style.bottom = '20px';
  27. keyboardContainer.style.left = '50%';
  28. keyboardContainer.style.transform = 'translateX(-50%)';
  29. keyboardContainer.style.width = '95%';
  30. keyboardContainer.style.maxWidth = '1200px';
  31. keyboardContainer.style.height = '300px';
  32. keyboardContainer.style.backgroundColor = '#d4d4d4'; // Light grey background
  33. keyboardContainer.style.border = '1px solid #ccc';
  34. keyboardContainer.style.borderRadius = '15px';
  35. keyboardContainer.style.zIndex = '10000';
  36. keyboardContainer.style.display = 'none'; // Initially hidden
  37. keyboardContainer.style.padding = '10px';
  38. keyboardContainer.style.boxShadow = '0px 4px 15px rgba(0,0,0,0.2)';
  39. keyboardContainer.style.userSelect = 'none';
  40. keyboardContainer.style.fontFamily = 'sans-serif'; // Apple-like font
  41. document.body.appendChild(keyboardContainer);
  42.  
  43. // Function to toggle keyboard visibility
  44. function toggleKeyboardVisibility() {
  45. if (keyboardContainer.style.display === 'none') {
  46. keyboardContainer.style.display = 'block';
  47. } else {
  48. keyboardContainer.style.display = 'none';
  49. }
  50. }
  51.  
  52. // Detect if the user is on mobile or PC
  53. const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
  54.  
  55. if (isMobile) {
  56. // Mobile: Create open/close button
  57. const toggleButton = document.createElement('button');
  58. toggleButton.textContent = '⌨️ Open Keyboard';
  59. toggleButton.style.position = 'fixed';
  60. toggleButton.style.bottom = '330px';
  61. toggleButton.style.left = '10px';
  62. toggleButton.style.padding = '10px 20px';
  63. toggleButton.style.zIndex = '10001';
  64. toggleButton.style.backgroundColor = '#007aff'; // Apple blue color
  65. toggleButton.style.color = '#fff';
  66. toggleButton.style.border = 'none';
  67. toggleButton.style.borderRadius = '5px';
  68. toggleButton.style.fontSize = '16px';
  69. toggleButton.style.boxShadow = '0px 2px 5px rgba(0,0,0,0.2)';
  70. document.body.appendChild(toggleButton);
  71.  
  72. // Toggle keyboard visibility when button is clicked
  73. toggleButton.addEventListener('click', () => {
  74. toggleKeyboardVisibility();
  75. toggleButton.textContent = keyboardContainer.style.display === 'none' ? '⌨️ Open Keyboard' : '⌨️ Close Keyboard';
  76. });
  77. } else {
  78. // PC: Listen for 'T' key press to toggle keyboard visibility
  79. document.addEventListener('keydown', (e) => {
  80. if (e.key.toLowerCase() === 't') {
  81. toggleKeyboardVisibility();
  82. }
  83. });
  84. }
  85.  
  86. // Draggable functionality for both cursor and touch
  87. let isDragging = false;
  88. let offsetX, offsetY;
  89.  
  90. function startDrag(e) {
  91. isDragging = true;
  92. offsetX = e.clientX ? e.clientX - keyboardContainer.getBoundingClientRect().left : e.touches[0].clientX - keyboardContainer.getBoundingClientRect().left;
  93. offsetY = e.clientY ? e.clientY - keyboardContainer.getBoundingClientRect().top : e.touches[0].clientY - keyboardContainer.getBoundingClientRect().top;
  94. }
  95.  
  96. function drag(e) {
  97. if (isDragging) {
  98. const x = e.clientX ? e.clientX : e.touches[0].clientX;
  99. const y = e.clientY ? e.clientY : e.touches[0].clientY;
  100. keyboardContainer.style.left = \`\${x - offsetX}px\`;
  101. keyboardContainer.style.top = \`\${y - offsetY}px\`;
  102. }
  103. }
  104.  
  105. function stopDrag() {
  106. isDragging = false;
  107. }
  108.  
  109. keyboardContainer.addEventListener('mousedown', startDrag);
  110. document.addEventListener('mousemove', drag);
  111. document.addEventListener('mouseup', stopDrag);
  112.  
  113. keyboardContainer.addEventListener('touchstart', startDrag);
  114. document.addEventListener('touchmove', drag);
  115. document.addEventListener('touchend', stopDrag);
  116.  
  117. // Keyboard keys array (including arrow keys and spacebar)
  118. const keys = [
  119. ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'],
  120. ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'],
  121. ['Z', 'X', 'C', 'V', 'B', 'N', 'M'],
  122. ['ArrowUp'],
  123. ['ArrowLeft', 'Space', 'ArrowRight'],
  124. ['ArrowDown']
  125. ];
  126.  
  127. // Create key buttons
  128. keys.forEach(row => {
  129. const rowDiv = document.createElement('div');
  130. rowDiv.style.display = 'flex';
  131. rowDiv.style.justifyContent = 'center';
  132. rowDiv.style.marginBottom = '10px';
  133.  
  134. row.forEach(key => {
  135. const keyButton = document.createElement('button');
  136. keyButton.textContent = key === 'Space' ? '' : key;
  137. keyButton.style.flex = key === 'Space' ? '2' : 'none';
  138. keyButton.style.padding = '15px';
  139. keyButton.style.margin = '2px';
  140. keyButton.style.borderRadius = '8px';
  141. keyButton.style.fontSize = '18px';
  142. keyButton.style.backgroundColor = '#ffffff'; // White keys
  143. keyButton.style.border = '1px solid #ddd';
  144. keyButton.style.boxShadow = '0px 2px 4px rgba(0,0,0,0.1)';
  145. keyButton.style.fontFamily = 'Arial, sans-serif';
  146. rowDiv.appendChild(keyButton);
  147.  
  148. // Key press event listener
  149. keyButton.addEventListener('click', () => {
  150. handleKeyPress(key);
  151. });
  152. });
  153.  
  154. keyboardContainer.appendChild(rowDiv);
  155. });
  156.  
  157. // Handle key press (send key event to Kogama)
  158. function handleKeyPress(key) {
  159. const event = new KeyboardEvent('keydown', {
  160. key: key === 'Space' ? ' ' : key,
  161. code: \`Key\${key.toUpperCase()}\`,
  162. keyCode: key === 'Enter' ? 13 : key.charCodeAt(0),
  163. bubbles: true,
  164. cancelable: true
  165. });
  166.  
  167. document.dispatchEvent(event);
  168.  
  169. // Custom API interaction for Kogama
  170. if (typeof kogamaAPI !== 'undefined') {
  171. kogamaAPI.sendKey(key); // Example, replace with actual API function
  172. }
  173. }
  174.  
  175. // Kogama API integration (example, replace with actual API calls)
  176. const kogamaAPI = {
  177. sendKey: function(key) {
  178. console.log(\`Sending key: \${key} to Kogama API\`);
  179. // Implement actual Kogama API interaction here for movement and jumping
  180. }
  181. };
  182.  
  183. // Ensure the keyboard works on dynamically loaded pages
  184. function checkForGamePage() {
  185. // Check if the game or other specific page is loaded
  186. const gameFrame = document.querySelector('iframe[src*="game"]');
  187. if (gameFrame) {
  188. // Insert keyboard into game frame if it's loaded in an iframe
  189. gameFrame.contentWindow.document.body.appendChild(keyboardContainer);
  190. } else {
  191. // Attach to main document body if not in an iframe
  192. document.body.appendChild(keyboardContainer);
  193. }
  194. }
  195.  
  196. // Run check on page load
  197. checkForGamePage();
  198.  
  199. // Run check when navigating between pages (using MutationObserver)
  200. const observer = new MutationObserver(checkForGamePage);
  201. observer.observe(document.body, { childList: true, subtree: true });
  202. `;
  203. document.body.appendChild(script);
  204. }
  205.  
  206. // Inject the keyboard script when the page loads
  207. window.addEventListener('load', injectKeyboardScript);
  208.  
  209. // Optionally, re-inject on navigation within the site (e.g., when navigating to different game pages)
  210. const observer = new MutationObserver(() => {
  211. injectKeyboardScript();
  212. });
  213.  
  214. observer.observe(document.body, { childList: true, subtree: true });
  215. })();