index.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="description" content="A basic Hello World HTML page with metadata and OpenGraph headers">
  7. <meta name="author" content="Your Name">
  8. <title>Hello World</title>
  9. <!-- OpenGraph Metadata -->
  10. <meta property="og:title" content="Hello World">
  11. <meta property="og:description" content="A basic Hello World HTML page with metadata and OpenGraph headers">
  12. <meta property="og:type" content="website">
  13. <meta property="og:url" content="http://example.com">
  14. <meta property="og:image" content="http://example.com/image.jpg">
  15. <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
  16. <style>
  17. body{
  18. margin: 0;
  19. }
  20. #remoteCapture{
  21. width: 100%;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <img id="remoteCapture" src="/stream" oncontextmenu="return false;"></img>
  27. <script>
  28. let socket;
  29. let protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
  30. let port = window.location.port ? window.location.port : (protocol === 'wss' ? 443 : 80);
  31. let socketURL = `${protocol}://${window.location.hostname}:${port}/hid`;
  32. let mouseMoveAbsolte = true; // Set to true for absolute mouse coordinates, false for relativeZ
  33. let mouseIsOutside = false;
  34. /* Mouse events */
  35. function handleMouseMove(event) {
  36. const hidCommand = {
  37. event: 2,
  38. mouse_x: event.clientX,
  39. mouse_y: event.clientY
  40. };
  41. const rect = event.target.getBoundingClientRect();
  42. const relativeX = event.clientX - rect.left;
  43. const relativeY = event.clientY - rect.top;
  44. if (relativeX < 0 || relativeY < 0 || relativeX > rect.width || relativeY > rect.height) {
  45. mouseIsOutside = true;
  46. return; // Mouse is outside the client rect
  47. }
  48. mouseIsOutside = false;
  49. const percentageX = (relativeX / rect.width) * 4096;
  50. const percentageY = (relativeY / rect.height) * 4096;
  51. hidCommand.mouse_x = Math.round(percentageX);
  52. hidCommand.mouse_y = Math.round(percentageY);
  53. console.log(`Mouse move: (${event.clientX}, ${event.clientY})`);
  54. console.log(`Mouse move relative: (${relativeX}, ${relativeY})`);
  55. console.log(`Mouse move percentage: (${hidCommand.mouse_x}, ${hidCommand.mouse_y})`);
  56. if (socket && socket.readyState === WebSocket.OPEN) {
  57. socket.send(JSON.stringify(hidCommand));
  58. } else {
  59. console.error("WebSocket is not open.");
  60. }
  61. }
  62. function handleMousePress(event) {
  63. event.preventDefault();
  64. event.stopImmediatePropagation();
  65. if (mouseIsOutside) {
  66. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  67. return;
  68. }
  69. const buttonMap = {
  70. 0: 1,
  71. 1: 3,
  72. 2: 2
  73. }; //Map javascript mouse buttons to HID buttons
  74. const hidCommand = {
  75. event: 3,
  76. mouse_button: buttonMap[event.button] || 0
  77. };
  78. console.log(`Mouse down: ${hidCommand.mouse_button}`);
  79. if (socket && socket.readyState === WebSocket.OPEN) {
  80. socket.send(JSON.stringify(hidCommand));
  81. } else {
  82. console.error("WebSocket is not open.");
  83. }
  84. }
  85. function handleMouseRelease(event) {
  86. event.preventDefault();
  87. event.stopImmediatePropagation();
  88. if (mouseIsOutside) {
  89. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  90. return;
  91. }
  92. const buttonMap = {
  93. 0: 1,
  94. 1: 3,
  95. 2: 2
  96. }; //Map javascript mouse buttons to HID buttons
  97. const hidCommand = {
  98. event: 4,
  99. mouse_button: buttonMap[event.button] || 0
  100. };
  101. console.log(`Mouse release: ${hidCommand.mouse_button}`);
  102. if (socket && socket.readyState === WebSocket.OPEN) {
  103. socket.send(JSON.stringify(hidCommand));
  104. } else {
  105. console.error("WebSocket is not open.");
  106. }
  107. }
  108. function handleMouseScroll(event) {
  109. const hidCommand = {
  110. event: 5,
  111. mouse_scroll: event.deltaY
  112. };
  113. if (mouseIsOutside) {
  114. console.warn("Mouse is outside the capture area, ignoring mouse press.");
  115. return;
  116. }
  117. console.log(`Mouse scroll: mouse_scroll=${event.deltaY}`);
  118. if (socket && socket.readyState === WebSocket.OPEN) {
  119. socket.send(JSON.stringify(hidCommand));
  120. } else {
  121. console.error("WebSocket is not open.");
  122. }
  123. }
  124. // Attach mouse event listeners
  125. document.addEventListener('mousemove', handleMouseMove);
  126. document.addEventListener('mousedown', handleMousePress);
  127. document.addEventListener('mouseup', handleMouseRelease);
  128. document.addEventListener('wheel', handleMouseScroll);
  129. /* Keyboard */
  130. function isNumpadEvent(event) {
  131. return event.location === 3;
  132. }
  133. function handleKeyDown(event) {
  134. event.preventDefault();
  135. event.stopImmediatePropagation();
  136. const key = event.key;
  137. let hidCommand = {
  138. event: 0,
  139. keycode: event.keyCode
  140. };
  141. console.log(`Key down: ${key} (code: ${event.keyCode})`);
  142. // Check if the key is a modkey on the right side of the keyboard
  143. const rightModKeys = ['Control', 'Alt', 'Shift', 'Meta'];
  144. if (rightModKeys.includes(key) && event.location === 2) {
  145. hidCommand.is_right_modifier_key = true;
  146. }else if (key === 'Enter' && isNumpadEvent(event)) {
  147. //Special case for Numpad Enter
  148. hidCommand.is_right_modifier_key = true;
  149. }else{
  150. hidCommand.is_right_modifier_key = false;
  151. }
  152. if (socket && socket.readyState === WebSocket.OPEN) {
  153. socket.send(JSON.stringify(hidCommand));
  154. } else {
  155. console.error("WebSocket is not open.");
  156. }
  157. }
  158. function handleKeyUp(event) {
  159. event.preventDefault();
  160. event.stopImmediatePropagation();
  161. const key = event.key;
  162. let hidCommand = {
  163. event: 1,
  164. keycode: event.keyCode
  165. };
  166. console.log(`Key up: ${key} (code: ${event.keyCode})`);
  167. // Check if the key is a modkey on the right side of the keyboard
  168. const rightModKeys = ['Control', 'Alt', 'Shift', 'Meta'];
  169. if (rightModKeys.includes(key) && event.location === 2) {
  170. hidCommand.is_right_modifier_key = true;
  171. } else if (key === 'Enter' && isNumpadEvent(event)) {
  172. //Special case for Numpad Enter
  173. hidCommand.is_right_modifier_key = true;
  174. }else{
  175. hidCommand.is_right_modifier_key = false;
  176. }
  177. if (socket && socket.readyState === WebSocket.OPEN) {
  178. socket.send(JSON.stringify(hidCommand));
  179. } else {
  180. console.error("WebSocket is not open.");
  181. }
  182. }
  183. /* Start and Stop events */
  184. function startWebSocket(){
  185. if (socket){
  186. //Already started
  187. alert("Websocket already started");
  188. return;
  189. }
  190. const socketUrl = socketURL;
  191. socket = new WebSocket(socketUrl);
  192. socket.addEventListener('open', function(event) {
  193. console.log('WebSocket is connected.');
  194. });
  195. socket.addEventListener('message', function(event) {
  196. console.log('Message from server ', event.data);
  197. });
  198. document.addEventListener('keydown', handleKeyDown);
  199. document.addEventListener('keyup', handleKeyUp);
  200. }
  201. function stopWebSocket(){
  202. if (!socket){
  203. alert("No ws connection to stop");
  204. return;
  205. }
  206. socket.close();
  207. console.log('WebSocket is disconnected.');
  208. document.removeEventListener('keydown', handleKeyDown);
  209. document.removeEventListener('keyup', handleKeyUp);
  210. }
  211. $(document).ready(function(){
  212. startWebSocket();
  213. });
  214. </script>
  215. </body>
  216. </html>