You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

remote.html 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Mediator - Remote</title>
  7. <link rel="stylesheet" href="style.css">
  8. <link rel="icon" type="image/png" href="favicon.png">
  9. <style>
  10. body {
  11. margin: 0px;
  12. }
  13. #cursor {
  14. position: absolute;
  15. left: 0px;
  16. top: 0px;
  17. pointer-events: none;
  18. }
  19. #screencast-container {
  20. max-height: calc(100% - 200px);
  21. height: 100%;
  22. text-align: center;
  23. }
  24. #screencast-container {
  25. background: black;
  26. }
  27. #screencast {
  28. max-height: calc(100vh - 200px);
  29. max-width: 100%;
  30. }
  31. #input {
  32. padding: 12px;
  33. text-align: center;
  34. }
  35. #keyboard {
  36. margin-top: 12px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <img id="cursor" src="cursor.png" width=10>
  42. <div id="screencast-container">
  43. <img id="screencast" src="/api/remote/screencast">
  44. </div>
  45. <div id="input">
  46. <form id="text-form">
  47. <input id="text" name="text" type="text">
  48. </form>
  49. <div id="keyboard">
  50. <button onclick='sendKey("escape")'>Esc</button>
  51. <button onclick='sendKey("enter")'>Enter</button>
  52. <button onclick='sendKey("space")'>Space</button>
  53. </div>
  54. </div>
  55. <script src="util.js"></script>
  56. <script>
  57. let cursorEl = document.getElementById("cursor");
  58. let screencastEl = document.getElementById("screencast");
  59. let screencastContainerEl = document.getElementById("screencast-container");
  60. let textFormEl = document.getElementById("text-form");
  61. let textEl = document.getElementById("text");
  62. let screencastSrc = screencastEl.src;
  63. let screencastLoaded = false;
  64. let sendMousePos = debounce(pos => {
  65. return api("PUT", "remote/mouse-pos", pos);
  66. });
  67. let sendKey = key => {
  68. return api("POST", "remote/keyboard-key", {key});
  69. };
  70. window.addEventListener("blur", () => {
  71. if (!screencastLoaded) {
  72. screencastEl.src = "";
  73. return;
  74. }
  75. let canvas = document.createElement("canvas");
  76. canvas.width = screencastEl.width;
  77. canvas.height = screencastEl.height;
  78. let ctx = canvas.getContext("2d");
  79. ctx.drawImage(screencastEl, 0, 0, canvas.width, canvas.height);
  80. screencastEl.src = canvas.toDataURL();
  81. });
  82. window.addEventListener("focus", () => {
  83. screencastLoaded = false;
  84. screencastEl.src = screencastSrc;
  85. });
  86. screencastEl.addEventListener("load", () => {
  87. screencastLoaded = true;
  88. });
  89. screencastEl.addEventListener("error", evt => {
  90. screencastLoaded = false;
  91. // This is necessary because sometimes, right after Safari on iOS returns
  92. // from sleep, it will fail to load the screencast.
  93. // This line forces the browser to reload the image.
  94. screencastEl.src = screencastEl.src;
  95. });
  96. window.addEventListener("keydown", evt => {
  97. let handled = true;
  98. console.log("key:", evt.key);
  99. if (evt.key == " ") {
  100. sendKey("space");
  101. } else if (evt.key == "Enter" && document.activeElement.tagName != "INPUT") {
  102. sendKey("enter");
  103. } else if (evt.key == "ArrowUp") {
  104. sendKey("up");
  105. } else if (evt.key == "ArrowDown") {
  106. sendKey("down");
  107. } else if (evt.key == "ArrowLeft") {
  108. sendKey("left");
  109. } else if (evt.key == "ArrowRight") {
  110. sendKey("right");
  111. } else if (evt.key == "Escape") {
  112. sendKey("escape");
  113. } else {
  114. handled = false;
  115. if (evt.key.length == 1 && document.activeElement.tagName != "INPUT") {
  116. textEl.focus();
  117. }
  118. }
  119. if (handled) {
  120. evt.preventDefault();
  121. }
  122. });
  123. function updateCursor(mousePos, screenSize) {
  124. let fracX = mousePos.x / screenSize.width;
  125. let fracY = mousePos.y / screenSize.height;
  126. let left = fracX * screencastEl.offsetWidth + screencastEl.offsetLeft;
  127. let top = fracY * screencastEl.offsetHeight + screencastEl.offsetTop;
  128. cursorEl.style.left = left + "px";
  129. cursorEl.style.top = top + "px";
  130. }
  131. let scrollDist = {x: 0, y: 0};
  132. function onScroll(delta) {
  133. // Scroll is inverted
  134. delta.x = -delta.x;
  135. delta.y = -delta.y;
  136. if (Math.abs(delta.x) > Math.abs(delta.y)) {
  137. scrollDist.x += delta.x;
  138. let distX = roundToZero(scrollDist.x);
  139. if (distX != 0) {
  140. api("POST", "remote/scroll", {x: distX, y: 0});
  141. scrollDist.x -= distX;
  142. }
  143. } else {
  144. scrollDist.y += delta.y;
  145. let distY = roundToZero(scrollDist.y);
  146. if (distY != 0) {
  147. api("POST", "remote/scroll", {x: 0, y: distY});
  148. scrollDist.y -= distY;
  149. }
  150. }
  151. }
  152. function moveDelta(mousePos, screenSize, delta) {
  153. mousePos.x += delta.x;
  154. if (mousePos.x >= screenSize.width) {
  155. mousePos.x = screenSize.width - 1;
  156. } else if (mousePos.x < 0) {
  157. mousePos.x = 0;
  158. }
  159. mousePos.y += delta.y;
  160. if (mousePos.y >= screenSize.height) {
  161. mousePos.y = screenSize.height - 1;
  162. } else if (mousePos.y < 0) {
  163. mousePos.y = 0;
  164. }
  165. updateCursor(mousePos, screenSize);
  166. sendMousePos(mousePos);
  167. }
  168. function signPow(num, pow) {
  169. if (num >= 0) {
  170. return Math.pow(num, pow);
  171. } else {
  172. return -Math.pow(-num, pow);
  173. }
  174. }
  175. function roundToZero(num) {
  176. if (num >= 0) {
  177. return Math.floor(num);
  178. } else {
  179. return Math.ceil(num);
  180. }
  181. }
  182. async function main() {
  183. let screenSize = await api("GET", "remote/screen-size");
  184. let mousePos = await api("GET", "remote/mouse-pos");
  185. updateCursor(mousePos, screenSize);
  186. setInterval(async () => {
  187. if (!sendMousePos.waiting) {
  188. mousePos = await api("GET", "remote/mouse-pos");
  189. updateCursor(mousePos, screenSize);
  190. }
  191. }, 200);
  192. textFormEl.addEventListener("submit", async evt => {
  193. evt.preventDefault();
  194. let text = evt.target.elements.text.value;
  195. evt.target.elements.text.value = "";
  196. await api("POST", "remote/keyboard-type", {text});
  197. sendKey("enter");
  198. });
  199. screencastEl.addEventListener("click", evt => {
  200. evt.preventDefault();
  201. api("POST", "remote/mouse-click", {button: "left", doubleClick: false});
  202. });
  203. screencastEl.addEventListener("dblclick", evt => {
  204. evt.preventDefault();
  205. api("POST", "remote/mouse-click", {button: "left", doubleClick: true});
  206. });
  207. screencastEl.addEventListener("contextmenu", evt => {
  208. evt.preventDefault();
  209. api("POST", "remote/mouse-click", {button: "right", doubleClick: false});
  210. });
  211. screencastEl.addEventListener("wheel", evt => {
  212. evt.preventDefault();
  213. onScroll({x: evt.deltaX / 5, y: evt.deltaY / 5});
  214. });
  215. screencastEl.addEventListener("mousemove", evt => {
  216. if (evt.buttons != 0) {
  217. return;
  218. }
  219. evt.preventDefault();
  220. let fracX = evt.offsetX / evt.target.offsetWidth;
  221. let fracY = evt.offsetY / evt.target.offsetHeight;
  222. mousePos.x = Math.round(fracX * screenSize.width);
  223. mousePos.y = Math.round(fracY * screenSize.height);
  224. updateCursor(mousePos, screenSize);
  225. sendMousePos(mousePos);
  226. });
  227. let numTouches = 0;
  228. let touches = {};
  229. screencastContainerEl.addEventListener("touchstart", evt => {
  230. evt.preventDefault();
  231. numTouches += evt.changedTouches.length;
  232. for (let touch of evt.changedTouches) {
  233. touches[touch.identifier] = {x: touch.clientX, y: touch.clientY, moveDist: 0};
  234. }
  235. });
  236. screencastContainerEl.addEventListener("touchmove", evt => {
  237. evt.preventDefault();
  238. let delta = {x: 0, y: 0}
  239. for (let touch of evt.changedTouches) {
  240. let oldTouch = touches[touch.identifier];
  241. let d = {x: touch.clientX - oldTouch.x, y: touch.clientY - oldTouch.y};
  242. oldTouch.moveDist += Math.sqrt(d.x * d.x + d.y * d.y);
  243. oldTouch.x = touch.clientX;
  244. oldTouch.y = touch.clientY;
  245. delta.x += d.x;
  246. delta.y += d.y;
  247. }
  248. if (numTouches == 1) {
  249. delta.x = Math.round(signPow(delta.x, 1.5));
  250. delta.y = Math.round(signPow(delta.y, 1.5));
  251. moveDelta(mousePos, screenSize, delta);
  252. } else if (numTouches == 2) {
  253. delta.x = -delta.x / 5 / numTouches;
  254. delta.y = -delta.y / 5 / numTouches;
  255. onScroll(delta);
  256. }
  257. });
  258. screencastContainerEl.addEventListener("touchend", evt => {
  259. evt.preventDefault();
  260. numTouches -= evt.changedTouches.length;
  261. for (let touch of evt.changedTouches) {
  262. let oldTouch = touches[touch.identifier];
  263. touches[touch.identifier] = null;
  264. if (oldTouch.moveDist < 10) {
  265. api("POST", "remote/mouse-click", {button: "left", doubleClick: false});
  266. break;
  267. }
  268. }
  269. });
  270. }
  271. main();
  272. </script>
  273. </body>
  274. </html>