Arras.io Chat

Use Right Shift button to enable/disable chat

当前为 2023-01-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Arras.io Chat
  3. // @match *://arras.io/#*
  4. // @match *://arras.netlify.app/#*
  5. // @match *://arrax.io/#*
  6. // @match *://arras.cx/#*
  7. // @match *://arras.io/
  8. // @match *://arras.netlify.app/
  9. // @match *://arrax.io/
  10. // @match *://arras.cx/
  11. // @author Inellok
  12. // @description Use Right Shift button to enable/disable chat
  13. // @namespace Inellok Labs.
  14. // @version 2.1
  15. // @run-at document-end
  16. // @license MIT
  17. // ==/UserScript==
  18.  
  19.  
  20. function getServerFromLoc()
  21. {
  22. let rlockhash = "";
  23. let started = false;
  24. for (let i = 0; i < window.location.href.length; i++) {
  25. if (window.location.href[i] === '#') {
  26. started = true;
  27. continue
  28. }
  29. if (started) {
  30. rlockhash += window.location.href[i];
  31. }
  32. }
  33. return rlockhash;
  34. }
  35.  
  36.  
  37. function getName()
  38. {
  39. let rname = "unnamed";
  40. let item = window.localStorage.getItem("arras.io");
  41.  
  42. if (item != null) {
  43. rname = JSON.parse(window.localStorage.getItem("arras.io")).name;
  44. }
  45. return rname;
  46. }
  47.  
  48.  
  49. function sendEverything() {
  50. let xhr = new XMLHttpRequest();
  51. xhr.open("POST", "https://arrasio-chat.glitch.me/");
  52.  
  53. //We need this to send http requests from arras.io page to the server with other URL
  54. xhr.setRequestHeader("Accept", "plain/text");
  55. xhr.setRequestHeader("Content-Type", "plain/text");
  56. //------
  57.  
  58. //when we got server's response
  59. xhr.onreadystatechange = function() {
  60. let srv = getServerFromLoc();
  61. if (xhr.readyState === 4) {
  62. chat_container.innerText = "";
  63. let msgs = JSON.parse(xhr.responseText);
  64. for (let i = 0; i < msgs.length; ++i) {
  65. if (srv === msgs[i].srv) {
  66. let nick = document.createElement("font");
  67. nick.color = "yellow";
  68. nick.innerText = msgs[i].nick + ": ";
  69. chat_container.appendChild(nick);
  70.  
  71. let msg = document.createElement("font");
  72. msg.color = "red";
  73. msg.innerText = msgs[i].message;
  74. chat_container.appendChild(msg);
  75.  
  76. chat_container.appendChild(document.createElement("br"));
  77. }
  78. }
  79. }
  80. };
  81.  
  82. xhr.send(JSON.stringify({
  83. srv: getServerFromLoc(),
  84. nick: getName(),
  85. message: msg_container.value
  86. }));
  87. }
  88.  
  89. //We need this to draw chat over arras drawing canvas
  90. let canvas = document.getElementById("canvas");
  91. canvas.style.zIndex = 1;
  92. canvas.style.position = "absolute";
  93.  
  94.  
  95. let lockhash = "";
  96.  
  97.  
  98. let info_container = document.createElement("div");
  99. info_container.innerText = "Arras.io Chat (by Inellok)";
  100. info_container.style.marginLeft = "40%";
  101. info_container.style.color = "white";
  102. info_container.style.opacity = "1";
  103.  
  104.  
  105. let chat_container = document.createElement("div");
  106. chat_container.style.marginLeft = "2%";
  107. chat_container.style.color = "red";
  108. chat_container.style.opacity = "1";
  109. chat_container.style.height = "88%";
  110. chat_container.fontSize = "100%";
  111.  
  112. let msg_container = document.createElement("input");
  113.  
  114. msg_container.placeholder = "message";
  115. msg_container.style.marginLeft = "2%";
  116. msg_container.style.color = "black";
  117. msg_container.style.opacity = "1";
  118. msg_container.style.height = "5%";
  119. msg_container.style.width = "65%";
  120. msg_container.style.fontSize = "110%";
  121.  
  122.  
  123. let send_container = document.createElement("button");
  124.  
  125. send_container.innerText = "SEND / UPDATE";
  126. send_container.style.marginLeft = "2%";
  127. send_container.style.color = "black";
  128. send_container.style.opacity = "1";
  129. send_container.style.height = "5%";
  130. send_container.style.width = "21%";
  131. send_container.style.fontSize = "60%";
  132.  
  133. send_container.onclick = function() {
  134. sendEverything();
  135. msg_container.value = "";
  136. return false;
  137. }
  138.  
  139. let mainContainer = document.createElement("div");
  140. mainContainer.style = `
  141. width:50%;
  142. height:85%;
  143. background:#000000;
  144. opacity: 90%;
  145. margin: auto;
  146. font-size: 150%;
  147. visibility:hidden;
  148. z-index:2;
  149. position:absolute;
  150. margin-left: 20%`;
  151.  
  152. msg_container.addEventListener("keydown", (e) => {
  153. //We need this to not move our character while writing message
  154. e.stopPropagation();
  155. if (e.code === "Enter" && document.activeElement === msg_container) {
  156. sendEverything();
  157. msg_container.value = "";
  158. }
  159.  
  160. if (e.code === "ShiftRight") {
  161. if (mainContainer.style.visibility === "hidden") {
  162. mainContainer.style.visibility = "visible";
  163. } else {
  164. mainContainer.style.visibility = "hidden";
  165. }
  166. }
  167. });
  168.  
  169. window.addEventListener("keydown", (e) => {
  170. //show/hide our chat when press right shift
  171. if (e.code === "ShiftRight") {
  172. console.log("SHIFT PRESSED");
  173. if (mainContainer.style.visibility === "hidden") {
  174. mainContainer.style.visibility = "visible";
  175. } else {
  176. mainContainer.style.visibility = "hidden";
  177. }
  178. }
  179.  
  180.  
  181. });
  182.  
  183. mainContainer.appendChild(info_container);
  184. mainContainer.appendChild(chat_container);
  185. mainContainer.appendChild(msg_container);
  186. mainContainer.appendChild(send_container);
  187. document.body.appendChild(mainContainer);