Advertisement
MeKLiN2

Untitled

Nov 22nd, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.08 KB | None | 0 0
  1. // ==UserScript==
  2. // @name stumblechat kick
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.11
  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. // Check if the message is a "joined" message
  86. if (parsedMessage.stumble === "joined") {
  87. // Extracting our own handle from the "self" object in the message
  88. const selfHandle = parsedMessage.self.handle;
  89. // Update handleUserMap and custom user list when a user joins
  90. const userList = parsedMessage.userlist;
  91. if (userList && userList.length > 0) {
  92. userList.forEach(user => {
  93. // Check if the user being added is ourselves
  94. const isSelf = user.handle === selfHandle;
  95. // If it's our own user, update handleUserMap and display our own handle in the user list
  96. if (isSelf) {
  97. // Update handleUserMap with our own handle and username
  98. handleUserMap[user.handle] = user.username || user.nick;
  99. // Add our own handle to the custom user list with purple icon
  100. addUserToUserList({
  101. username: user.username,
  102. handle: user.handle,
  103. active: true, // We just joined, so we're considered active
  104. icon: "⚪" // White icon for our own user entry
  105. }, "self");
  106. console.log('const originalWebSocket = window.WebSocket found self for white dot. Applying!')
  107. } else {
  108. // If it's not our own user, proceed as usual and add them to the user list
  109. updateUserListAndMapOnJoin(user);
  110. }
  111. });
  112. }
  113. } else if (parsedMessage.stumble === "join") {
  114. // Handle join messages
  115. const { handle, username } = parsedMessage;
  116. // Check if the user being added is not ourselves
  117. if (handle !== yourHandle) {
  118. handleUserMap[handle] = username;
  119. addUserToUserList({ handle, username }, "join");
  120. }
  121. } else if (parsedMessage.stumble === "quit") {
  122. // Handle quit messages
  123. const handle = parsedMessage.handle;
  124. const username = handleUserMap[handle];
  125. if (username) {
  126. // Remove the user from the handleUserMap
  127. delete handleUserMap[handle];
  128. // Remove the user from the user list
  129. removeUserFromUserList(handle);
  130. console.log('user departed')
  131. addUserToUserList({ handle, username }, "quit");
  132. }
  133. } else if (parsedMessage.stumble === "msg") {
  134. // Handle message messages
  135. const { handle, text } = parsedMessage;
  136. const username = handleUserMap[handle] || handle;
  137. displayWebSocketMessage(event.data);
  138. }
  139.  
  140. // Check if the message is a system message
  141. if (parsedMessage.stumble === "system") {
  142. const systemMessage = parsedMessage.message;
  143.  
  144. // Check if the system message contains the client version
  145. if (systemMessage.startsWith('"Client Version:')) {
  146. // Save the user list to a file
  147. console.log("sysmsgdetected");
  148. }
  149. }
  150. });
  151.  
  152. return ws;
  153. };
  154.  
  155. // Function to create user list div
  156. function createUserListDiv() {
  157. const userListDiv = document.createElement("div");
  158. userListDiv.id = "userList";
  159. userListDiv.style.position = "absolute"; // Change to absolute positioning
  160. userListDiv.style.top = "100px"; // Adjust top position as needed
  161. userListDiv.style.left = "10px"; // Adjust left position as needed
  162. userListDiv.style.height = "calc(100% - 100px)"; // Adjust height to fill remaining space
  163. userListDiv.style.overflowY = "auto";
  164. userListDiv.style.color = "#ffffff";
  165. userListDiv.style.padding = "10px";
  166. userListDiv.style.zIndex = "2"; // Set a higher z-index value
  167. userListDiv.style.display = "none"; // Hide the custom user list by default
  168. return userListDiv;
  169. }
  170.  
  171. // Function to update handleUserMap with user data from the loaded file
  172. function updateHandleUserMap(fileContent) {
  173. // Split the file content into lines
  174. const lines = fileContent.split('\n');
  175.  
  176. // Iterate over each line to parse user data
  177. lines.forEach(line => {
  178. const userData = line.trim().split(' '); // Splitting by space to separate username and handle
  179. if (userData.length === 2) {
  180. const username = userData[0].trim();
  181. const handle = userData[1].trim();
  182.  
  183. // Check if the handle already exists in the handleUserMap
  184. if (!handleUserMap.hasOwnProperty(handle)) {
  185. // Update user map with the new username and handle
  186. handleUserMap[handle] = username;
  187. }
  188. }
  189. });
  190. }
  191.  
  192.  
  193. // Function to update the user list display
  194. function updateUserListDisplay() {
  195. // Get the user list element
  196. const userList = document.getElementById("userList");
  197. if (userList) {
  198. // Update the display with modified handleUserMap
  199. userList.innerHTML = ""; // Clear the user list
  200. for (const handle in handleUserMap) {
  201. const username = handleUserMap[handle];
  202. // Append the user to the user list with 'B' if present
  203. const listItem = document.createElement("li");
  204. listItem.textContent = `${username} (${handle})`;
  205. userList.appendChild(listItem);
  206. }
  207. }
  208. }
  209.  
  210. // Function to get the user's own handle number (implement your own logic)
  211. function getOwnHandle() {
  212. // Implement your logic to retrieve the user's own handle number
  213. // For demonstration purposes, return a hardcoded value
  214. return "123456"; // Replace with your actual handle number
  215. }
  216.  
  217. // WebSocket listener for handling messages
  218. function handleWebSocketMessage(message) {
  219. const parsedMessage = JSON.parse(message);
  220. const ownHandle = getOwnHandle(); // Function to get the user's own handle number
  221.  
  222. if (parsedMessage.stumble === "msg" && ownHandle === parsedMessage.handle) {
  223. const text = parsedMessage.text;
  224. if (text.startsWith("#ban")) {
  225. // Extract the handle from the message
  226. const handleToBan = text.replace("#ban", "").trim();
  227. if (handleToBan !== "") {
  228. // Check if the handle exists in the handleUserMap
  229. if (handleUserMap.hasOwnProperty(handleToBan)) {
  230. // Add 'B' next to the handle number
  231. handleUserMap[handleToBan] += " B";
  232. // Update the user list display
  233. updateUserListDisplay();
  234. // Update the users.txt file
  235. saveUserListToFile();
  236. } else {
  237. alert("Handle not found!");
  238. }
  239. } else {
  240. alert("Invalid handle!");
  241. }
  242. }
  243. }
  244. }
  245.  
  246. // Call functions to set up overlay, WebSocket listener, and initial user list display
  247.  
  248. setupWebSocketListener();
  249. updateUserListDisplay();
  250.  
  251.  
  252. // Function to display WebSocket messages and update user list
  253. function displayWebSocketMessage(message) {
  254. const parsedMessage = JSON.parse(message);
  255. if (parsedMessage.stumble === "join") {
  256. // Handle join messages: Extract handle and username from the message
  257. const { handle, username } = parsedMessage;
  258. // Map handle to username in handleUserMap
  259. handleUserMap[handle] = username;
  260. // Add the user to the custom user list with the appropriate icon (join user)
  261. addUserToUserList({ handle, username }, "join");
  262. } else if (parsedMessage.stumble === "msg") {
  263. // Handle message messages: Extract handle and text from the message
  264. const { handle, text } = parsedMessage;
  265. // Retrieve username from handleUserMap or use handle if not found
  266. const username = handleUserMap[handle] || handle;
  267. // Display the message in the WebSocket messages div
  268. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  269. if (webSocketMessagesDiv) {
  270. // Append the message with a newline character
  271. webSocketMessagesDiv.textContent += `${username}: ${text}\n`;
  272. // Scroll to the bottom of the messages div
  273. webSocketMessagesDiv.scrollTop = webSocketMessagesDiv.scrollHeight;
  274. }
  275.  
  276. // Additional logic for handling commands
  277. if (text === "#join") {
  278. console.log("join");
  279. // Add your logic here
  280. } else if (text === "#icon") {
  281. console.log("icon");
  282. // Add your logic here
  283. } else if (text === "#tokes") {
  284. console.log("tokes");
  285. // Call your tokes function here
  286. TokesSendEnter();
  287. } else if (text.startsWith("#ai ")) {
  288. console.log("ai");
  289. // Extract the word after "#ai"
  290. const word = text.substring(4);
  291. console.log("Word after '#ai':", word);
  292. // Call your AI function here with the extracted word
  293. DoAi(word); // Adjust parameters as needed
  294. }
  295. } else if (parsedMessage.stumble === "joined") {
  296. // Handle joined messages: Add users to handleUserMap and custom user list
  297. const userList = parsedMessage.userlist;
  298. if (userList && userList.length > 0) {
  299. userList.forEach(user => {
  300. // Extract username from either "username" or "nick"
  301. const username = user.username || user.nick;
  302. // Map handle to username in handleUserMap
  303. handleUserMap[user.handle] = username;
  304. // Add the user to the custom user list with the appropriate icon
  305. addUserToUserList({ handle: user.handle, username }, "joined");
  306. });
  307. }
  308. } else if (parsedMessage.stumble === "quit") {
  309. // Handle quit messages: Remove users from handleUserMap and custom user list
  310. const handle = parsedMessage.handle;
  311. const username = handleUserMap[handle];
  312. if (username) {
  313. // Remove the handle from handleUserMap
  314. delete handleUserMap[handle];
  315. // Remove the user from the custom user list
  316. removeUserFromUserList(handle);
  317. console.log('line 323 user departed')
  318. }
  319. }
  320. }
  321.  
  322.  
  323.  
  324. // Function to add user to user list with appropriate icon based on user type
  325. function addUserToUserList(user, userType) {
  326. const userList = document.getElementById("userList");
  327. if (!userList) return;
  328.  
  329. const userItem = document.createElement("div");
  330. userItem.textContent = `${user.username}`;
  331.  
  332. // Define the default dot color and icon
  333. let dotColor; // Default dot color
  334. let icon; // Default icon
  335.  
  336. // Set dot color and icon based on user type
  337. if (userType === "self") {
  338. console.log('[function addUserToList] found "self" for purple dot. Applying!')
  339. dotColor = "purple";
  340. icon = "🟣";
  341.  
  342. } else if (userType === "join") {
  343. console.log('[function addUserToList] found "join" for green dot. Applying!')
  344. dotColor = "green";
  345. icon = "🟢";
  346.  
  347. } else if (userType === "joined") {
  348. console.log('[function addUserToList] found "joined" for blue dot. Applying!')
  349. dotColor = "blue";
  350. icon = "🔵";
  351.  
  352. } else {
  353. // For other user types, default to red dot
  354. console.log('[function addUserToList] found "userList" for green dot. Applying!')
  355. dotColor = "green";
  356. icon = "🟢";
  357. //dotColor = "red"; // FOR QUIT MSG
  358. //icon = "🔴";
  359. }
  360.  
  361. // Add colored dot based on user status
  362. const dot = document.createElement("span");
  363. dot.textContent = icon;
  364. dot.style.color = dotColor;
  365. userItem.appendChild(dot);
  366.  
  367. // Add custom icon for the user
  368. if (user.icon) {
  369. const customIcon = document.createElement("span");
  370. customIcon.textContent = user.icon;
  371. customIcon.style.marginLeft = "5px"; // Adjust margin as needed
  372. userItem.appendChild(customIcon);
  373. }
  374.  
  375. userList.appendChild(userItem); // Append user item to the user list
  376.  
  377. // If user is not active and not yourself, show popup for 5 seconds
  378. //if (!user.active && user.handle !== yourHandle) {
  379. //const popup = document.createElement("div");
  380. //popup.textContent = "WELCOME TO STUMBLECHAT YEOPARDY!";
  381. //popup.style.fontSize = "48px";
  382. //popup.style.position = "fixed";
  383. //popup.style.top = "50%";
  384. //popup.style.left = "50%";
  385. //popup.style.transform = "translate(-50%, -50%)";
  386. //popup.style.backgroundColor = "rgba(0, 0, 0, 0.5)";
  387. //popup.style.color = "white";
  388. //popup.style.padding = "10px";
  389. //popup.style.borderRadius = "5px";
  390. //document.body.appendChild(popup);
  391.  
  392. }
  393. // Function to update handleUserMap and add users to custom user list
  394. function updateUserListAndMapOnJoin(user) {
  395. // Update handleUserMap with the new user
  396. handleUserMap[user.handle] = user.username || user.nick; // Derive username from handle or nick
  397. // Add the new user to the custom user list
  398. addUserToUserList(user);
  399. }
  400.  
  401. // Call the function to update the user list
  402. function updateUserListOnMessage(userList) {
  403. return function(message) {
  404. const parsedMessage = JSON.parse(message);
  405. if (parsedMessage.stumble === "join" || parsedMessage.stumble === "msg" || parsedMessage.stumble === "joined") {
  406. // Add user to user list
  407. addUserToUserList(parsedMessage);
  408. }
  409. };
  410. }
  411.  
  412. // Function to create WebSocket messages div
  413. function createWebSocketMessagesDiv() {
  414. const div = document.createElement("div");
  415. div.id = "webSocketMessages";
  416. div.style.position = "relative";
  417. div.style.height = "25%";
  418. div.style.paddingLeft = "2px";
  419. div.style.visibility = "visible"; // Ensure the div is visible
  420. div.style.willChange = "transform";
  421. div.style.boxSizing = "border-box";
  422. div.style.overflowX = "hidden";
  423. div.style.overflowY = "auto";
  424. div.style.color = "#ffffff"; // Set font color to white
  425. div.style.padding = "10px"; // Example padding
  426. div.style.zIndex = "2"; // Set a higher z-index value for the WebSocket messages div
  427.  
  428. // Additional styles for specific scenarios
  429. div.style.display = "flex";
  430. div.style.flexDirection = "column";
  431. div.style.justifyContent = "flex-end";
  432. div.style.fontSize = "18px";
  433.  
  434. div.style.whiteSpace = "pre-wrap"; // Allow text to wrap within the container
  435. div.style.wordWrap = "break-word"; // Allow long words to break and wrap
  436.  
  437. // Locate the chat-position div
  438. const chatPositionDiv = document.getElementById("chat-position");
  439. if (chatPositionDiv) {
  440. // Append custom div to the chat-position div
  441. chatPositionDiv.appendChild(div);
  442. } else {
  443. // If chat-position div not found, append to document body as fallback
  444. document.body.appendChild(div);
  445. }
  446. }
  447.  
  448. // Call the function to create the WebSocket messages div
  449. createWebSocketMessagesDiv();
  450.  
  451. function toggleWebSocketMessagesDiv() {
  452. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  453. const chatContentDiv = document.getElementById("chat-content");
  454. if (webSocketMessagesDiv && chatContentDiv) {
  455. // Check the current display style of the WebSocket messages div
  456. const webSocketMessagesDisplayStyle = webSocketMessagesDiv.style.display;
  457.  
  458. // Toggle the display of both divs based on the current display style of the WebSocket messages div
  459. if (webSocketMessagesDisplayStyle === "none") {
  460. // If WebSocket messages div is hidden, show it and hide chat content div
  461. webSocketMessagesDiv.style.display = "block";
  462. chatContentDiv.style.display = "none";
  463. } else {
  464. // If WebSocket messages div is visible, hide it and show chat content div
  465. webSocketMessagesDiv.style.display = "none";
  466. chatContentDiv.style.display = "block";
  467. }
  468. }
  469. }
  470. // This line keeps the default style and hides the socket log by default remove for default wss log
  471. toggleWebSocketMessagesDiv()
  472.  
  473. // Add a button to toggle visibility of WebSocket messages div
  474. const toggleMessagesButton = document.createElement("button");
  475. toggleMessagesButton.textContent = "M";
  476. toggleMessagesButton.style.position = "fixed";
  477. toggleMessagesButton.style.right = "0px";
  478. toggleMessagesButton.style.bottom = "0px";
  479. toggleMessagesButton.addEventListener("click", toggleWebSocketMessagesDiv);
  480. document.body.appendChild(toggleMessagesButton);
  481.  
  482. // Call the function to create the user list div
  483. const userListDiv = createUserListDiv();
  484. const chatContainer = document.getElementById("chat-container");
  485. if (chatContainer) {
  486. chatContainer.appendChild(userListDiv); // Append to chat container instead
  487. } else {
  488. document.body.appendChild(userListDiv);
  489. }
  490.  
  491. // Function to remove user from custom user list
  492. function removeUserFromUserList(handle) {
  493. const userList = document.getElementById("userList");
  494. if (userList) {
  495. const userElements = userList.querySelectorAll("div");
  496. userElements.forEach(userElement => {
  497. if (userElement.textContent.includes(`(${handle})`)) {
  498. userElement.remove();
  499. console.log('line 490 user remove/depart')
  500. }
  501. });
  502. }
  503. }
  504.  
  505. // Function to toggle visibility of custom user list
  506. function toggleCustomUserList() {
  507. const userListDiv = document.getElementById("userList");
  508. if (userListDiv) {
  509. userListDiv.style.display = userListDiv.style.display === "none" ? "block" : "none";
  510. }
  511. }
  512.  
  513. // Add a button to toggle visibility of custom user list
  514. const toggleButton = document.createElement("button");
  515. toggleButton.textContent = "U";
  516. toggleButton.style.position = "fixed";
  517. toggleButton.style.top = "0px";
  518. toggleButton.style.left = "0px";
  519. toggleButton.addEventListener("click", toggleCustomUserList);
  520. document.body.appendChild(toggleButton);
  521.  
  522. // Function to clear messages
  523. function clr() {
  524. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  525. if (webSocketMessagesDiv) {
  526. webSocketMessagesDiv.innerHTML = "";
  527. }
  528. }
  529.  
  530. // Function to apply font size to WebSocket messages
  531. function applyFontSize(fontSize) {
  532. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  533. if (webSocketMessagesDiv) {
  534. webSocketMessagesDiv.style.fontSize = `${fontSize}px`;
  535. }
  536. }
  537.  
  538. /* Additional compacting styles */
  539. /*@-moz-document url-prefix("https://stumblechat.com/room/") {*/
  540. // Compact message styles
  541. const compactStyles = `
  542. .message .nickname ~ .content {
  543. display: inline-block;
  544. top: -7px;
  545. position: relative;
  546. margin-left: 2px;
  547. margin-right: 1em;
  548. }
  549. .content + .content {
  550. display: inline-block!important;
  551. margin-right: 1em;
  552. }
  553. .message .nickname ~ .content span {
  554. line-height: 1.5em;
  555. }
  556. `;
  557.  
  558. // Apply compact styles to the document
  559. const style = document.createElement('style');
  560. style.textContent = compactStyles;
  561. document.head.appendChild(style);
  562. /*}*/
  563.  
  564. // Function to send the "Tokes in 20 seconds" message and simulate Enter key press
  565. function TokesSendEnter() {
  566. // Insert predefined text
  567. const textArea = document.getElementById("textarea");
  568. textArea.value += 'Tokes in 20 seconds\n';
  569.  
  570. // Simulate Enter key press
  571. const event = new KeyboardEvent('keypress', {
  572. key: 'Enter',
  573. code: 'Enter',
  574. keyCode: 13,
  575. which: 13,
  576. bubbles: true
  577. });
  578. textArea.dispatchEvent(event);
  579.  
  580. // Set a timeout to display "tokes started" message after 20 seconds
  581. setTimeout(function() {
  582. textArea.value += 'tokes started\n'; // Send message indicating tokes has started
  583.  
  584. // Simulate Enter key press again
  585. textArea.dispatchEvent(event);
  586. }, 20000); // 20 seconds delay
  587. }
  588.  
  589.  
  590. // Function to create input element
  591. function createInputBox() {
  592. const input = document.createElement('input');
  593. input.type = 'text';
  594. input.placeholder = 'Type your message...';
  595. input.style.position = 'fixed'; // Set position to fixed
  596. input.style.bottom = '50px'; // Adjust bottom position as needed
  597. input.style.left = '10px'; // Adjust left position as needed
  598. input.style.zIndex = '9999'; // Set a high z-index value to ensure it's above other elements
  599.  
  600. // Add event listener to detect when user presses Enter key
  601. input.addEventListener('keypress', function(event) {
  602. if (event.key === 'Enter') {
  603. const msg = input.value.trim();
  604. // Check if the message starts with '/kick'
  605. if (msg.startsWith('/kick')) {
  606. // Extract the username from the input text
  607. const username = msg.substring(6).trim(); // Assuming '/kick ' is 6 characters long
  608. // Check if the username exists in the handleUserMap
  609. const handle = Object.keys(handleUserMap).find(key => handleUserMap[key] === username);
  610. if (handle) {
  611. // Send kick message to WebSocket server
  612. webSocket.send(JSON.stringify({
  613. "stumble": "kick",
  614. "handle": handle
  615. }));
  616. } else {
  617. console.error('Username not found');
  618. }
  619. // Clear the input field after sending the message
  620. input.value = "";
  621. }
  622. // Check if the message starts with '/ban'
  623. else if (msg.startsWith('/ban')) {
  624. // Extract the username from the input text
  625. const username = msg.substring(5).trim(); // Assuming '/ban ' is 5 characters long
  626. // Check if the username exists in the handleUserMap
  627. const handle = Object.keys(handleUserMap).find(key => handleUserMap[key] === username);
  628. if (handle) {
  629. // Send ban message to WebSocket server
  630. webSocket.send(JSON.stringify({
  631. "stumble": "ban",
  632. "handle": handle
  633. }));
  634. } else {
  635. console.error('Username not found');
  636. }
  637. // Clear the input field after sending the message
  638. input.value = "";
  639. }
  640. // If the message is not a kick or ban command, send it as a regular message
  641. else {
  642. // Send normal message via sendMessage function
  643. sendMessage();
  644. }
  645. }
  646. });
  647.  
  648. return input;
  649. }
  650.  
  651. // Call the function to create input box
  652. const input = createInputBox();
  653.  
  654. // Append input to the document body or wherever you want it to appear
  655. document.body.appendChild(input);
  656.  
  657.  
  658.  
  659. // Function to handle sending message
  660. function sendMessage() {
  661. // Get the value of the input
  662. const msg = input.value.trim();
  663.  
  664. // Check if the WebSocket is not null and the message is not empty
  665. if (webSocket !== undefined && msg !== "") { // Check if webSocket is defined
  666. // Send the message via WebSocket
  667. webSocket.send(JSON.stringify({
  668. "stumble": "msg",
  669. "text": msg
  670. }));
  671.  
  672. // Clear the input field after sending the message
  673. input.value = "";
  674. }
  675. }
  676.  
  677. // Add event listener to button
  678. button.addEventListener('click', sendMessage);
  679.  
  680. // Create div to contain input and button
  681. const inputMenuBox = document.createElement('div');
  682. inputMenuBox.id = 'input-menu-box';
  683. inputMenuBox.appendChild(input);
  684. inputMenuBox.appendChild(button);
  685.  
  686. // Append input menu box to the document body or wherever you want it to appear
  687. document.body.appendChild(inputMenuBox);
  688.  
  689. // Function to send JSON data to WebSocket server
  690. function sendJSONData(data) {
  691. // Check if WebSocket connection is open
  692. if (webSocket.readyState === WebSocket.OPEN) {
  693. // Send JSON data to WebSocket server
  694. webSocket.send(JSON.stringify(data));
  695. console.log('JSON data sent:', data);
  696. } else {
  697. console.error('WebSocket connection is not open');
  698. }
  699. }
  700.  
  701. // Example JSON data
  702. const jsonData = {
  703. stumble: "msg", // Modify stumble according to your message type
  704. text: "Hello, WebSocket server!" // Modify text according to your message content
  705. };
  706.  
  707. // Call sendJSONData function to send JSON data to WebSocket server
  708. sendJSONData(jsonData);
  709.  
  710. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement