concatenated raw chat log stumblechat

Automatic Moderation

  1. // ==UserScript==
  2. // @name concatenated raw chat log stumblechat
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.13
  5. // @description Automatic Moderation
  6. // @author MeKLiN
  7. // @match https://stumblechat.com/room/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=stumblechat.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12. let webSocket;
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. let selfHandleSet = false; // Variable to track if the 'self' handle has been set
  18. let selfHandle; // Variable to store the 'self' handle
  19.  
  20. const handleUserMap = {}; // Initialize handleUserMap as an empty object
  21. // Define yourHandle variable
  22. const yourHandle = "myHandle"; // Replace "myHandle" with the actual handle
  23.  
  24. function parseWebSocketMessage(message) {
  25. const parsedMessage = JSON.parse(message);
  26. if (parsedMessage.stumble === "join") {
  27. const { handle, username } = parsedMessage;
  28. if (!selfHandleSet) {
  29. // If 'self' handle hasn't been set yet, set it now
  30. selfHandle = handle;
  31. handleUserMap[handle] = "self"; // Set the first entry as 'self'
  32. selfHandleSet = true;
  33. } else {
  34. handleUserMap[handle] = username;
  35. }
  36. addUserToUserList({ handle, username });
  37. const joinMessage = `${username} joined the chat.`;
  38. displayWebSocketMessage(joinMessage);
  39. } else if (parsedMessage.stumble === "quit") {
  40. const handle = parsedMessage.handle;
  41. const username = handleUserMap[handle];
  42. if (username) {
  43. delete handleUserMap[handle];
  44. removeUserFromUserList(handle);
  45. const quitMessage = `${username} left the chat.`;
  46. displayWebSocketMessage(quitMessage);
  47. }
  48. } else if (parsedMessage.stumble === "msg") {
  49. // Handle additional changes to the user list for specific "msg" messages
  50. }
  51. }
  52.  
  53. let webSocketURL; // Global variable to store the WebSocket URL!
  54.  
  55. // Function to set up WebSocket listener
  56. function setupWebSocketListener() {
  57. // Override WebSocket constructor to intercept WebSocket creation
  58. const originalWebSocket = window.WebSocket;
  59. window.WebSocket = function(url, protocols) {
  60. webSocket = new originalWebSocket(url, protocols); // Assign to global webSocket variable
  61. console.log('WebSocket URL:', webSocketURL);
  62.  
  63. // Call original WebSocket constructor
  64. const ws = new originalWebSocket(url, protocols);
  65.  
  66. // Event listener for receiving messages
  67. webSocket.addEventListener('message', event => {
  68. handleWebSocketMessage(event.data);
  69. });
  70.  
  71. return webSocket;
  72. };
  73. }
  74.  
  75. // WebSocket Listener: Override WebSocket constructor to intercept WebSocket creation
  76. const originalWebSocket = window.WebSocket;
  77. window.WebSocket = function(url, protocols) {
  78.  
  79. // Call original WebSocket constructor
  80. const ws = new originalWebSocket(url, protocols);
  81.  
  82. // Event listener for receiving messages
  83. ws.addEventListener('message', event => {
  84. const parsedMessage = JSON.parse(event.data);
  85.  
  86. // Check if the message is a "joined" message
  87. if (parsedMessage.stumble === "joined") {
  88. // Extracting our own handle from the "self" object in the message
  89. const selfHandle = parsedMessage.self.handle;
  90. // Update handleUserMap and custom user list when a user joins
  91. const userList = parsedMessage.userlist;
  92. if (userList && userList.length > 0) {
  93. userList.forEach(user => {
  94. // Check if the user being added is ourselves
  95. const isSelf = user.handle === selfHandle;
  96. // If it's our own user, update handleUserMap with our own handle
  97. if (isSelf) {
  98. handleUserMap[user.handle] = user.username || user.nick;
  99. console.log('Found self, handleUserMap updated with self handle');
  100. } else {
  101. // If it's not ourselves, update the map with the new user
  102. handleUserMap[user.handle] = user.username || user.nick;
  103. }
  104. });
  105. }
  106. } else if (parsedMessage.stumble === "join") {
  107. // Handle join messages
  108. const { handle, username } = parsedMessage;
  109. if (handle !== yourHandle) {
  110. handleUserMap[handle] = username;
  111. }
  112. } else if (parsedMessage.stumble === "quit") {
  113. // Handle quit messages
  114. const handle = parsedMessage.handle;
  115. const username = handleUserMap[handle];
  116. if (username) {
  117. delete handleUserMap[handle];
  118. console.log('User left:', username);
  119. }
  120. } else if (parsedMessage.stumble === "msg") {
  121. // Handle message messages
  122. const { handle, text } = parsedMessage;
  123. const username = handleUserMap[handle] || handle;
  124. displayWebSocketMessage(event.data);
  125. }
  126.  
  127. // Check if the message is a system message
  128. if (parsedMessage.stumble === "system") {
  129. const systemMessage = parsedMessage.message;
  130. if (systemMessage.startsWith('"Client Version:')) {
  131. console.log("System message detected, client version info received.");
  132. }
  133. }
  134. });
  135.  
  136. return ws;
  137. };
  138.  
  139.  
  140.  
  141. // Function to update handleUserMap with user data from the loaded file
  142. function updateHandleUserMap(fileContent) {
  143. // Split the file content into lines
  144. const lines = fileContent.split('\n');
  145.  
  146. // Iterate over each line to parse user data
  147. lines.forEach(line => {
  148. const userData = line.trim().split(' '); // Splitting by space to separate username and handle
  149. if (userData.length === 2) {
  150. const username = userData[0].trim();
  151. const handle = userData[1].trim();
  152.  
  153. // Check if the handle already exists in the handleUserMap
  154. if (!handleUserMap.hasOwnProperty(handle)) {
  155. // Update user map with the new username and handle
  156. handleUserMap[handle] = username;
  157. }
  158. }
  159. });
  160. }
  161.  
  162. // Function to get the user's own handle number (implement your own logic)
  163. function getOwnHandle() {
  164. // Implement your logic to retrieve the user's own handle number
  165. // For demonstration purposes, return a hardcoded value
  166. return "123456"; // Replace with your actual handle number
  167. }
  168.  
  169. // WebSocket listener for handling messages
  170. function handleWebSocketMessage(message) {
  171. const parsedMessage = JSON.parse(message);
  172. const ownHandle = getOwnHandle(); // Function to get the user's own handle number
  173.  
  174. if (parsedMessage.stumble === "msg" && ownHandle === parsedMessage.handle) {
  175. const text = parsedMessage.text;
  176. // No ban logic, so just checking if it's a message
  177. // You can handle other message types here if necessary
  178. }
  179. }
  180.  
  181. // Call functions to set up WebSocket listener
  182. setupWebSocketListener();
  183.  
  184.  
  185. // List of blocked usernames
  186. const blockedUsers = ['AccountNameHere', 'hell']; // Modify this list as needed
  187.  
  188. // Track if blocking is enabled
  189. let isBlockingEnabled = true;
  190.  
  191. // Flag to control whether messages should be concatenated or displayed individually
  192. let isMessagesConcatenated = true;
  193.  
  194. // Function to toggle between concatenated and individual message displays
  195. function toggleConcatenation() {
  196. isMessagesConcatenated = !isMessagesConcatenated;
  197. console.log(`Message display mode: ${isMessagesConcatenated ? "Concatenated" : "Individual"}`);
  198.  
  199. // If we want to clear all previous concatenated messages when switching modes
  200. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  201. if (webSocketMessagesDiv) {
  202. webSocketMessagesDiv.innerHTML = ''; // Clear all messages
  203. }
  204.  
  205. // You can decide whether to clear the messages completely or preserve the chat history
  206. // based on your specific requirements.
  207. }
  208. // Modify the `displayWebSocketMessage` function to handle both concatenated and individual modes
  209. function displayWebSocketMessage(message) {
  210. const parsedMessage = JSON.parse(message);
  211. if (parsedMessage.stumble === "join") {
  212. const { handle, username } = parsedMessage;
  213. handleUserMap[handle] = username;
  214. addUserToUserList({ handle, username }, "join");
  215. } else if (parsedMessage.stumble === "msg") {
  216. const { handle, text } = parsedMessage;
  217. const username = handleUserMap[handle] || handle;
  218.  
  219. if (isMessagesConcatenated) {
  220. // Handle concatenated messages
  221. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  222. if (webSocketMessagesDiv) {
  223. let existingMessage = webSocketMessagesDiv.querySelector(`#message-${handle}`);
  224. if (existingMessage) {
  225. existingMessage.textContent += ` . ${text}`;
  226. } else {
  227. const messageDiv = document.createElement("div");
  228. messageDiv.id = `message-${handle}`;
  229. messageDiv.textContent = `${username}: ${text}`;
  230. webSocketMessagesDiv.appendChild(messageDiv);
  231. }
  232. webSocketMessagesDiv.scrollTop = webSocketMessagesDiv.scrollHeight;
  233. }
  234. } else {
  235. // Handle individual messages (unconcatenated)
  236. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  237. if (webSocketMessagesDiv) {
  238. const messageDiv = document.createElement("div");
  239. messageDiv.id = `message-${handle}`;
  240. messageDiv.textContent = `${username}: ${text}`;
  241. webSocketMessagesDiv.appendChild(messageDiv);
  242. webSocketMessagesDiv.scrollTop = webSocketMessagesDiv.scrollHeight;
  243. }
  244. }
  245.  
  246. // Additional logic for handling commands
  247. if (text === "#join") {
  248. console.log("join");
  249. } else if (text === "#icon") {
  250. console.log("icon");
  251. } else if (text === "#tokes") {
  252. console.log("tokes");
  253. TokesSendEnter();
  254. } else if (text.startsWith("#ai ")) {
  255. console.log("ai");
  256. const word = text.substring(4);
  257. console.log("Word after '#ai':", word);
  258. }
  259. } else if (parsedMessage.stumble === "joined") {
  260. const userList = parsedMessage.userlist;
  261. if (userList && userList.length > 0) {
  262. userList.forEach(user => {
  263. const username = user.username || user.nick;
  264. handleUserMap[user.handle] = username;
  265. addUserToUserList({ handle: user.handle, username }, "joined");
  266. });
  267. }
  268. } else if (parsedMessage.stumble === "quit") {
  269. const handle = parsedMessage.handle;
  270. const username = handleUserMap[handle];
  271. if (username) {
  272. delete handleUserMap[handle];
  273. removeUserFromUserList(handle);
  274. console.log('line 323 user departed')
  275. }
  276. }
  277. }
  278.  
  279. // Create a button to toggle concatenation mode
  280. const toggleConcatenateButton = document.createElement("button");
  281. toggleConcatenateButton.textContent = "T";
  282. toggleConcatenateButton.style.position = "fixed";
  283. toggleConcatenateButton.style.top = "40px"; // Below the checkbox
  284. toggleConcatenateButton.style.left = "10px";
  285. toggleConcatenateButton.style.zIndex = "9999"; // Ensure the button is on top of other elements
  286.  
  287. // Add event listener to toggle the concatenation mode
  288. toggleConcatenateButton.addEventListener("click", toggleConcatenation);
  289.  
  290. // Append the button to the body
  291. document.body.appendChild(toggleConcatenateButton);
  292.  
  293. // Create a checkbox to toggle blocking functionality
  294. const blockMessagesCheckbox = document.createElement("input");
  295. blockMessagesCheckbox.type = "checkbox";
  296. blockMessagesCheckbox.checked = isBlockingEnabled; // Initially checked
  297. blockMessagesCheckbox.id = "blockMessagesCheckbox";
  298.  
  299. // Create a label for the checkbox
  300. const checkboxLabel = document.createElement("label");
  301. checkboxLabel.setAttribute("for", "blockMessagesCheckbox");
  302. checkboxLabel.textContent = "Block messages from ignored users";
  303.  
  304. // Style the checkbox and label
  305. blockMessagesCheckbox.style.position = "fixed";
  306. blockMessagesCheckbox.style.top = "10px";
  307. blockMessagesCheckbox.style.left = "10px";
  308. blockMessagesCheckbox.style.zIndex = "9999"; // Ensure the checkbox is on top of other elements
  309.  
  310. checkboxLabel.style.position = "fixed";
  311. checkboxLabel.style.top = "10px";
  312. checkboxLabel.style.left = "40px";
  313. checkboxLabel.style.zIndex = "9999"; // Ensure the label is on top of other elements
  314. checkboxLabel.style.fontSize = "14px";
  315. checkboxLabel.style.color = "white";
  316.  
  317. // Add event listener to the checkbox to toggle the blocking functionality
  318. blockMessagesCheckbox.addEventListener("change", function () {
  319. isBlockingEnabled = blockMessagesCheckbox.checked;
  320. console.log(`Message blocking ${isBlockingEnabled ? "enabled" : "disabled"}`);
  321. });
  322.  
  323. // Append the checkbox and label to the body
  324. document.body.appendChild(blockMessagesCheckbox);
  325. document.body.appendChild(checkboxLabel);
  326.  
  327.  
  328. // Function to add user to user list with appropriate icon based on user type
  329. function addUserToUserList(user, userType) {
  330. const userList = document.getElementById("userList");
  331. if (!userList) return;
  332.  
  333. const userItem = document.createElement("div");
  334. userItem.textContent = `${user.username}`;
  335.  
  336. // Define the default dot color and icon
  337. let dotColor; // Default dot color
  338. let icon; // Default icon
  339.  
  340. // Set dot color and icon based on user type
  341. if (userType === "self") {
  342. console.log('[function addUserToList] found "self" for purple dot. Applying!')
  343. dotColor = "purple";
  344. icon = "🟣";
  345.  
  346. } else if (userType === "join") {
  347. console.log('[function addUserToList] found "join" for green dot. Applying!')
  348. dotColor = "green";
  349. icon = "🟢";
  350.  
  351. } else if (userType === "joined") {
  352. console.log('[function addUserToList] found "joined" for blue dot. Applying!')
  353. dotColor = "blue";
  354. icon = "🔵";
  355.  
  356. } else {
  357. // For other user types, default to red dot
  358. console.log('[function addUserToList] found "userList" for green dot. Applying!')
  359. dotColor = "green";
  360. icon = "🟢";
  361. //dotColor = "red"; // FOR QUIT MSG
  362. //icon = "🔴";
  363. }
  364.  
  365. // Add colored dot based on user status
  366. const dot = document.createElement("span");
  367. dot.textContent = icon;
  368. dot.style.color = dotColor;
  369. userItem.appendChild(dot);
  370.  
  371. // Add custom icon for the user
  372. if (user.icon) {
  373. const customIcon = document.createElement("span");
  374. customIcon.textContent = user.icon;
  375. customIcon.style.marginLeft = "5px"; // Adjust margin as needed
  376. userItem.appendChild(customIcon);
  377. }
  378.  
  379. userList.appendChild(userItem); // Append user item to the user list
  380.  
  381. // If user is not active and not yourself, show popup for 5 seconds
  382. //if (!user.active && user.handle !== yourHandle) {
  383. //const popup = document.createElement("div");
  384. //popup.textContent = "WELCOME TO STUMBLECHAT YEOPARDY!";
  385. //popup.style.fontSize = "48px";
  386. //popup.style.position = "fixed";
  387. //popup.style.top = "50%";
  388. //popup.style.left = "50%";
  389. //popup.style.transform = "translate(-50%, -50%)";
  390. //popup.style.backgroundColor = "rgba(0, 0, 0, 0.5)";
  391. //popup.style.color = "white";
  392. //popup.style.padding = "10px";
  393. //popup.style.borderRadius = "5px";
  394. //document.body.appendChild(popup);
  395.  
  396. }
  397. // Function to update handleUserMap without affecting the user list div
  398. function updateUserListAndMapOnJoin(user) {
  399. // Update handleUserMap with the new user
  400. handleUserMap[user.handle] = user.username || user.nick; // Derive username from handle or nick
  401. }
  402.  
  403. // Call the function to update the user list
  404. function updateUserListOnMessage(userList) {
  405. return function(message) {
  406. const parsedMessage = JSON.parse(message);
  407. if (parsedMessage.stumble === "join" || parsedMessage.stumble === "msg" || parsedMessage.stumble === "joined") {
  408. // Add user to user list
  409. addUserToUserList(parsedMessage);
  410. }
  411. };
  412. }
  413.  
  414. // Function to create WebSocket messages div
  415. function createWebSocketMessagesDiv() {
  416. const div = document.createElement("div");
  417. div.id = "webSocketMessages";
  418. div.style.position = "fixed";
  419. div.style.height = "100%";
  420. div.style.paddingLeft = "2px";
  421. div.style.visibility = "visible"; // Ensure the div is visible
  422. div.style.willChange = "transform";
  423. div.style.boxSizing = "border-box";
  424. div.style.overflowX = "hidden";
  425. div.style.overflowY = "auto";
  426. div.style.color = "#ffffff"; // Set font color to white
  427. div.style.padding = "10px"; // Example padding
  428. div.style.zIndex = "2"; // Set a higher z-index value for the WebSocket messages div
  429.  
  430. // Additional styles for specific scenarios
  431. div.style.display = "flex";
  432. div.style.flexDirection = "column";
  433. div.style.justifyContent = "flex-end";
  434. div.style.fontSize = "18px";
  435.  
  436. div.style.whiteSpace = "pre-wrap"; // Allow text to wrap within the container
  437. div.style.wordWrap = "break-word"; // Allow long words to break and wrap
  438.  
  439. // Locate the chat-position div
  440. const chatPositionDiv = document.getElementById("chat-position");
  441. if (chatPositionDiv) {
  442. // Append custom div to the chat-position div
  443. chatPositionDiv.appendChild(div);
  444. } else {
  445. // If chat-position div not found, append to document body as fallback
  446. document.body.appendChild(div);
  447. }
  448. }
  449.  
  450. // Call the function to create the WebSocket messages div
  451. createWebSocketMessagesDiv();
  452.  
  453. function toggleWebSocketMessagesDiv() {
  454. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  455. const chatContentDiv = document.getElementById("chat-content");
  456. if (webSocketMessagesDiv && chatContentDiv) {
  457. // Check the current display style of the WebSocket messages div
  458. const webSocketMessagesDisplayStyle = webSocketMessagesDiv.style.display;
  459.  
  460. // Toggle the display of both divs based on the current display style of the WebSocket messages div
  461. if (webSocketMessagesDisplayStyle === "none") {
  462. // If WebSocket messages div is hidden, show it and hide chat content div
  463. webSocketMessagesDiv.style.display = "block";
  464. chatContentDiv.style.display = "none";
  465. } else {
  466. // If WebSocket messages div is visible, hide it and show chat content div
  467. webSocketMessagesDiv.style.display = "none";
  468. chatContentDiv.style.display = "block";
  469. }
  470. }
  471. }
  472. // This line keeps the default style and hides the socket log by default remove for default wss log
  473. toggleWebSocketMessagesDiv()
  474.  
  475. // Add a button to toggle visibility of WebSocket messages div
  476. const toggleMessagesButton = document.createElement("button");
  477. toggleMessagesButton.textContent = "M";
  478. toggleMessagesButton.style.position = "fixed";
  479. toggleMessagesButton.style.right = "0px";
  480. toggleMessagesButton.style.bottom = "0px";
  481. toggleMessagesButton.addEventListener("click", toggleWebSocketMessagesDiv);
  482. document.body.appendChild(toggleMessagesButton);
  483.  
  484. // Call the function to create the user list div
  485.  
  486.  
  487. // Function to remove user from custom user list
  488. function removeUserFromUserList(handle) {
  489. const userList = document.getElementById("userList");
  490. if (userList) {
  491. const userElements = userList.querySelectorAll("div");
  492. userElements.forEach(userElement => {
  493. if (userElement.textContent.includes(`(${handle})`)) {
  494. userElement.remove();
  495. console.log('line 490 user remove/depart')
  496. }
  497. });
  498. }
  499. }
  500.  
  501.  
  502.  
  503. // Function to clear messages
  504. function clr() {
  505. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  506. if (webSocketMessagesDiv) {
  507. webSocketMessagesDiv.innerHTML = "";
  508. }
  509. }
  510.  
  511. // Function to apply font size to WebSocket messages
  512. function applyFontSize(fontSize) {
  513. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  514. if (webSocketMessagesDiv) {
  515. webSocketMessagesDiv.style.fontSize = `${fontSize}px`;
  516. }
  517. }
  518.  
  519. /* Additional compacting styles */
  520. /*@-moz-document url-prefix("https://stumblechat.com/room/") {*/
  521. // Compact message styles
  522. const compactStyles = `
  523. .message .nickname ~ .content {
  524. display: inline-block;
  525. top: -7px;
  526. position: relative;
  527. margin-left: 2px;
  528. margin-right: 1em;
  529. }
  530. .content + .content {
  531. display: inline-block!important;
  532. margin-right: 1em;
  533. }
  534. .message .nickname ~ .content span {
  535. line-height: 1.5em;
  536. }
  537. `;
  538.  
  539. // Apply compact styles to the document
  540. const style = document.createElement('style');
  541. style.textContent = compactStyles;
  542. document.head.appendChild(style);
  543. /*}*/
  544.  
  545. // Function to send the "Tokes in 20 seconds" message and simulate Enter key press
  546. function TokesSendEnter() {
  547. // Insert predefined text
  548. const textArea = document.getElementById("textarea");
  549. textArea.value += 'Tokes in 20 seconds\n';
  550.  
  551. // Simulate Enter key press
  552. const event = new KeyboardEvent('keypress', {
  553. key: 'Enter',
  554. code: 'Enter',
  555. keyCode: 13,
  556. which: 13,
  557. bubbles: true
  558. });
  559. textArea.dispatchEvent(event);
  560.  
  561. // Set a timeout to display "tokes started" message after 20 seconds
  562. setTimeout(function() {
  563. textArea.value += 'tokes started\n'; // Send message indicating tokes has started
  564.  
  565. // Simulate Enter key press again
  566. textArea.dispatchEvent(event);
  567. }, 20000); // 20 seconds delay
  568. }
  569.  
  570.  
  571. // Function to create input element
  572. function createInputBox() {
  573. const input = document.createElement('input');
  574. input.type = 'text';
  575. input.placeholder = 'Type your message...';
  576. input.style.position = 'fixed'; // Set position to fixed
  577. input.style.bottom = '50px'; // Adjust bottom position as needed
  578. input.style.left = '10px'; // Adjust left position as needed
  579. input.style.zIndex = '9999'; // Set a high z-index value to ensure it's above other elements
  580.  
  581. // Add event listener to detect when user presses Enter key
  582. input.addEventListener('keypress', function(event) {
  583. if (event.key === 'Enter') {
  584. const msg = input.value.trim();
  585. // Check if the message starts with '/kick'
  586. if (msg.startsWith('/kick')) {
  587. // Extract the username from the input text
  588. const username = msg.substring(6).trim(); // Assuming '/kick ' is 6 characters long
  589. // Check if the username exists in the handleUserMap
  590. const handle = Object.keys(handleUserMap).find(key => handleUserMap[key] === username);
  591. if (handle) {
  592. // Send kick message to WebSocket server
  593. webSocket.send(JSON.stringify({
  594. "stumble": "kick",
  595. "handle": handle
  596. }));
  597. } else {
  598. console.error('Username not found');
  599. }
  600. // Clear the input field after sending the message
  601. input.value = "";
  602. }
  603. // Check if the message starts with '/ban'
  604. else if (msg.startsWith('/ban')) {
  605. // Extract the username from the input text
  606. const username = msg.substring(5).trim(); // Assuming '/ban ' is 5 characters long
  607. // Check if the username exists in the handleUserMap
  608. const handle = Object.keys(handleUserMap).find(key => handleUserMap[key] === username);
  609. if (handle) {
  610. // Send ban message to WebSocket server
  611. webSocket.send(JSON.stringify({
  612. "stumble": "ban",
  613. "handle": handle
  614. }));
  615. } else {
  616. console.error('Username not found');
  617. }
  618. // Clear the input field after sending the message
  619. input.value = "";
  620. }
  621. // If the message is not a kick or ban command, send it as a regular message
  622. else {
  623. // Send normal message via sendMessage function
  624. sendMessage();
  625. }
  626. }
  627. });
  628.  
  629. return input;
  630. }
  631.  
  632. // Call the function to create input box
  633. const input = createInputBox();
  634.  
  635. // Append input to the document body or wherever you want it to appear
  636. document.body.appendChild(input);
  637.  
  638. // Function to handle sending message
  639. function sendMessage() {
  640. // Get the value of the input
  641. const msg = input.value.trim();
  642.  
  643. // Check if the WebSocket is not null and the message is not empty
  644. if (webSocket !== undefined && msg !== "") { // Check if webSocket is defined
  645. // Send the message via WebSocket
  646. webSocket.send(JSON.stringify({
  647. "stumble": "msg",
  648. "text": msg
  649. }));
  650.  
  651. // Clear the input field after sending the message
  652. input.value = "";
  653. }
  654. }
  655.  
  656. // Add event listener to input for the 'Enter' key press to send message
  657. input.addEventListener('keydown', function(event) {
  658. if (event.key === 'Enter') {
  659. sendMessage();
  660. }
  661. });
  662.  
  663.  
  664.  
  665.  
  666.  
  667. })();