Advertisement
MeKLiN2

anti-afk stumblechat

Jan 1st, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.13 KB | None | 0 0
  1. // ==UserScript==
  2. // @name SlashKickChelly..
  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 === ".afk meklin") {
  288. console.log("tokes");
  289. // Call your tokes function here
  290. MeklinSendEnter();
  291. } else if (text === ".afk kyskye") {
  292. console.log("tokes");
  293. // Call your tokes function here
  294. KyskyeSendEnter();
  295. } else if (text.startsWith("#ai ")) {
  296. console.log("ai");
  297. // Extract the word after "#ai"
  298. const word = text.substring(4);
  299. console.log("Word after '#ai':", word);
  300. // Call your AI function here with the extracted word
  301. DoAi(word); // Adjust parameters as needed
  302. }
  303. } else if (parsedMessage.stumble === "joined") {
  304. // Handle joined messages: Add users to handleUserMap and custom user list
  305. const userList = parsedMessage.userlist;
  306. if (userList && userList.length > 0) {
  307. userList.forEach(user => {
  308. // Extract username from either "username" or "nick"
  309. const username = user.username || user.nick;
  310. // Map handle to username in handleUserMap
  311. handleUserMap[user.handle] = username;
  312. // Add the user to the custom user list with the appropriate icon
  313. addUserToUserList({ handle: user.handle, username }, "joined");
  314. });
  315. }
  316. } else if (parsedMessage.stumble === "quit") {
  317. // Handle quit messages: Remove users from handleUserMap and custom user list
  318. const handle = parsedMessage.handle;
  319. const username = handleUserMap[handle];
  320. if (username) {
  321. // Remove the handle from handleUserMap
  322. delete handleUserMap[handle];
  323. // Remove the user from the custom user list
  324. removeUserFromUserList(handle);
  325. console.log('line 323 user departed')
  326. }
  327. }
  328. }
  329.  
  330.  
  331.  
  332. // Function to add user to user list with appropriate icon based on user type
  333. function addUserToUserList(user, userType) {
  334. const userList = document.getElementById("userList");
  335. if (!userList) return;
  336.  
  337. const userItem = document.createElement("div");
  338. userItem.textContent = `${user.username}`;
  339.  
  340. // Define the default dot color and icon
  341. let dotColor; // Default dot color
  342. let icon; // Default icon
  343.  
  344. // Set dot color and icon based on user type
  345. if (userType === "self") {
  346. console.log('[function addUserToList] found "self" for purple dot. Applying!')
  347. dotColor = "purple";
  348. icon = "🟣";
  349.  
  350. } else if (userType === "join") {
  351. console.log('[function addUserToList] found "join" for green dot. Applying!')
  352. dotColor = "green";
  353. icon = "🟢";
  354.  
  355. } else if (userType === "joined") {
  356. console.log('[function addUserToList] found "joined" for blue dot. Applying!')
  357. dotColor = "blue";
  358. icon = "🔵";
  359.  
  360. } else {
  361. // For other user types, default to red dot
  362. console.log('[function addUserToList] found "userList" for green dot. Applying!')
  363. dotColor = "green";
  364. icon = "🟢";
  365. //dotColor = "red"; // FOR QUIT MSG
  366. //icon = "🔴";
  367. }
  368.  
  369. // Add colored dot based on user status
  370. const dot = document.createElement("span");
  371. dot.textContent = icon;
  372. dot.style.color = dotColor;
  373. userItem.appendChild(dot);
  374.  
  375. // Add custom icon for the user
  376. if (user.icon) {
  377. const customIcon = document.createElement("span");
  378. customIcon.textContent = user.icon;
  379. customIcon.style.marginLeft = "5px"; // Adjust margin as needed
  380. userItem.appendChild(customIcon);
  381. }
  382.  
  383. userList.appendChild(userItem); // Append user item to the user list
  384.  
  385. // If user is not active and not yourself, show popup for 5 seconds
  386. //if (!user.active && user.handle !== yourHandle) {
  387. //const popup = document.createElement("div");
  388. //popup.textContent = "WELCOME TO STUMBLECHAT YEOPARDY!";
  389. //popup.style.fontSize = "48px";
  390. //popup.style.position = "fixed";
  391. //popup.style.top = "50%";
  392. //popup.style.left = "50%";
  393. //popup.style.transform = "translate(-50%, -50%)";
  394. //popup.style.backgroundColor = "rgba(0, 0, 0, 0.5)";
  395. //popup.style.color = "white";
  396. //popup.style.padding = "10px";
  397. //popup.style.borderRadius = "5px";
  398. //document.body.appendChild(popup);
  399.  
  400. }
  401. // Function to update handleUserMap and add users to custom user list
  402. function updateUserListAndMapOnJoin(user) {
  403. // Update handleUserMap with the new user
  404. handleUserMap[user.handle] = user.username || user.nick; // Derive username from handle or nick
  405. // Add the new user to the custom user list
  406. addUserToUserList(user);
  407. }
  408.  
  409. // Call the function to update the user list
  410. function updateUserListOnMessage(userList) {
  411. return function(message) {
  412. const parsedMessage = JSON.parse(message);
  413. if (parsedMessage.stumble === "join" || parsedMessage.stumble === "msg" || parsedMessage.stumble === "joined") {
  414. // Add user to user list
  415. addUserToUserList(parsedMessage);
  416. }
  417. };
  418. }
  419.  
  420. // Function to create WebSocket messages div
  421. function createWebSocketMessagesDiv() {
  422. const div = document.createElement("div");
  423. div.id = "webSocketMessages";
  424. div.style.position = "relative";
  425. div.style.height = "25%";
  426. div.style.paddingLeft = "2px";
  427. div.style.visibility = "visible"; // Ensure the div is visible
  428. div.style.willChange = "transform";
  429. div.style.boxSizing = "border-box";
  430. div.style.overflowX = "hidden";
  431. div.style.overflowY = "auto";
  432. div.style.color = "#ffffff"; // Set font color to white
  433. div.style.padding = "10px"; // Example padding
  434. div.style.zIndex = "2"; // Set a higher z-index value for the WebSocket messages div
  435.  
  436. // Additional styles for specific scenarios
  437. div.style.display = "flex";
  438. div.style.flexDirection = "column";
  439. div.style.justifyContent = "flex-end";
  440. div.style.fontSize = "18px";
  441.  
  442. div.style.whiteSpace = "pre-wrap"; // Allow text to wrap within the container
  443. div.style.wordWrap = "break-word"; // Allow long words to break and wrap
  444.  
  445. // Locate the chat-position div
  446. const chatPositionDiv = document.getElementById("chat-position");
  447. if (chatPositionDiv) {
  448. // Append custom div to the chat-position div
  449. chatPositionDiv.appendChild(div);
  450. } else {
  451. // If chat-position div not found, append to document body as fallback
  452. document.body.appendChild(div);
  453. }
  454. }
  455.  
  456. // Call the function to create the WebSocket messages div
  457. createWebSocketMessagesDiv();
  458.  
  459. function toggleWebSocketMessagesDiv() {
  460. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  461. const chatContentDiv = document.getElementById("chat-content");
  462. if (webSocketMessagesDiv && chatContentDiv) {
  463. // Check the current display style of the WebSocket messages div
  464. const webSocketMessagesDisplayStyle = webSocketMessagesDiv.style.display;
  465.  
  466. // Toggle the display of both divs based on the current display style of the WebSocket messages div
  467. if (webSocketMessagesDisplayStyle === "none") {
  468. // If WebSocket messages div is hidden, show it and hide chat content div
  469. webSocketMessagesDiv.style.display = "block";
  470. chatContentDiv.style.display = "none";
  471. } else {
  472. // If WebSocket messages div is visible, hide it and show chat content div
  473. webSocketMessagesDiv.style.display = "none";
  474. chatContentDiv.style.display = "block";
  475. }
  476. }
  477. }
  478. // This line keeps the default style and hides the socket log by default remove for default wss log
  479. toggleWebSocketMessagesDiv()
  480.  
  481. // Add a button to toggle visibility of WebSocket messages div
  482. const toggleMessagesButton = document.createElement("button");
  483. toggleMessagesButton.textContent = "M";
  484. toggleMessagesButton.style.position = "fixed";
  485. toggleMessagesButton.style.right = "0px";
  486. toggleMessagesButton.style.bottom = "0px";
  487. toggleMessagesButton.addEventListener("click", toggleWebSocketMessagesDiv);
  488. document.body.appendChild(toggleMessagesButton);
  489.  
  490. // Call the function to create the user list div
  491. const userListDiv = createUserListDiv();
  492. const chatContainer = document.getElementById("chat-container");
  493. if (chatContainer) {
  494. chatContainer.appendChild(userListDiv); // Append to chat container instead
  495. } else {
  496. document.body.appendChild(userListDiv);
  497. }
  498.  
  499. // Function to remove user from custom user list
  500. function removeUserFromUserList(handle) {
  501. const userList = document.getElementById("userList");
  502. if (userList) {
  503. const userElements = userList.querySelectorAll("div");
  504. userElements.forEach(userElement => {
  505. if (userElement.textContent.includes(`(${handle})`)) {
  506. userElement.remove();
  507. console.log('line 490 user remove/depart')
  508. }
  509. });
  510. }
  511. }
  512.  
  513. // Function to toggle visibility of custom user list
  514. function toggleCustomUserList() {
  515. const userListDiv = document.getElementById("userList");
  516. if (userListDiv) {
  517. userListDiv.style.display = userListDiv.style.display === "none" ? "block" : "none";
  518. }
  519. }
  520.  
  521. // Add a button to toggle visibility of custom user list
  522. const toggleButton = document.createElement("button");
  523. toggleButton.textContent = "U";
  524. toggleButton.style.position = "fixed";
  525. toggleButton.style.top = "0px";
  526. toggleButton.style.left = "0px";
  527. toggleButton.addEventListener("click", toggleCustomUserList);
  528. document.body.appendChild(toggleButton);
  529.  
  530. // Function to clear messages
  531. function clr() {
  532. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  533. if (webSocketMessagesDiv) {
  534. webSocketMessagesDiv.innerHTML = "";
  535. }
  536. }
  537.  
  538. // Function to apply font size to WebSocket messages
  539. function applyFontSize(fontSize) {
  540. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  541. if (webSocketMessagesDiv) {
  542. webSocketMessagesDiv.style.fontSize = `${fontSize}px`;
  543. }
  544. }
  545.  
  546. /* Additional compacting styles */
  547. /*@-moz-document url-prefix("https://stumblechat.com/room/") {*/
  548. // Compact message styles
  549. const compactStyles = `
  550. .message .nickname ~ .content {
  551. display: inline-block;
  552. top: -7px;
  553. position: relative;
  554. margin-left: 2px;
  555. margin-right: 1em;
  556. }
  557. .content + .content {
  558. display: inline-block!important;
  559. margin-right: 1em;
  560. }
  561. .message .nickname ~ .content span {
  562. line-height: 1.5em;
  563. }
  564. `;
  565.  
  566. // Apply compact styles to the document
  567. const style = document.createElement('style');
  568. style.textContent = compactStyles;
  569. document.head.appendChild(style);
  570. /*}*/
  571.  
  572. // Function to send the "Tokes in 20 seconds" message and simulate Enter key press
  573. function TokesSendEnter() {
  574. // Insert predefined text
  575. const textArea = document.getElementById("textarea");
  576. textArea.value += 'Tokes in 20 seconds\n';
  577.  
  578. // Simulate Enter key press
  579. const event = new KeyboardEvent('keypress', {
  580. key: 'Enter',
  581. code: 'Enter',
  582. keyCode: 13,
  583. which: 13,
  584. bubbles: true
  585. });
  586. textArea.dispatchEvent(event);
  587.  
  588. // Set a timeout to display "tokes started" message after 20 seconds
  589. setTimeout(function() {
  590. textArea.value += 'tokes started\n'; // Send message indicating tokes has started
  591.  
  592. // Simulate Enter key press again
  593. textArea.dispatchEvent(event);
  594. }, 20000); // 20 seconds delay
  595. }
  596.  
  597. // Function to send the "Tokes in 20 seconds" message and simulate Enter key press
  598. function KyskyeSendEnter() {
  599. // Insert predefined text
  600. const textArea = document.getElementById("textarea");
  601. textArea.value += 'Tokes in 20 seconds\n';
  602.  
  603. // Simulate Enter key press
  604. const event = new KeyboardEvent('keypress', {
  605. key: 'Enter',
  606. code: 'Enter',
  607. keyCode: 13,
  608. which: 13,
  609. bubbles: true
  610. });
  611. textArea.dispatchEvent(event);
  612.  
  613. // Set a timeout to display "tokes started" message after 20 seconds
  614. setTimeout(function() {
  615. textArea.value += 'tokes started\n'; // Send message indicating tokes has started
  616.  
  617. // Simulate Enter key press again
  618. textArea.dispatchEvent(event);
  619. }, 20000); // 20 seconds delay
  620. }
  621.  
  622. // Function to send the "Tokes in 20 seconds" message and simulate Enter key press
  623. function MeklinSendEnter() {
  624. // Insert predefined text
  625. const textArea = document.getElementById("textarea");
  626. textArea.value += 'Tokes in 20 seconds\n';
  627.  
  628. // Simulate Enter key press
  629. const event = new KeyboardEvent('keypress', {
  630. key: 'Enter',
  631. code: 'Enter',
  632. keyCode: 13,
  633. which: 13,
  634. bubbles: true
  635. });
  636. textArea.dispatchEvent(event);
  637.  
  638. // Set a timeout to display "tokes started" message after 20 seconds
  639. setTimeout(function() {
  640. textArea.value += 'tokes started\n'; // Send message indicating tokes has started
  641.  
  642. // Simulate Enter key press again
  643. textArea.dispatchEvent(event);
  644. }, 20000); // 20 seconds delay
  645. }
  646.  
  647.  
  648. let input; // Declare input as a global variable
  649.  
  650. // Function to create input element
  651. function createInputBox() {
  652. input = document.createElement('input'); // Assign to global variable
  653. input.type = 'text';
  654. input.placeholder = 'Type your message...';
  655. input.style.position = 'fixed';
  656. input.style.bottom = '50px';
  657. input.style.left = '10px';
  658. input.style.zIndex = '9999';
  659.  
  660. const button = document.createElement('button');
  661. button.textContent = 'Send';
  662. button.style.position = 'fixed';
  663. button.style.bottom = '10px';
  664. button.style.left = '10px';
  665. button.style.zIndex = '9999';
  666.  
  667. // Attach a single event listener for all behaviors
  668. button.addEventListener('click', function () {
  669. const msg = input.value.trim();
  670. if (msg.startsWith('/kick')) {
  671. const username = msg.substring(6).trim();
  672. const handle = Object.keys(handleUserMap).find(key => handleUserMap[key] === username);
  673. if (handle) {
  674. webSocket.send(JSON.stringify({
  675. "stumble": "kick",
  676. "handle": handle
  677. }));
  678. } else {
  679. console.error('Username not found');
  680. }
  681. } else if (msg.startsWith('/ban')) {
  682. const username = msg.substring(5).trim();
  683. const handle = Object.keys(handleUserMap).find(key => handleUserMap[key] === username);
  684. if (handle) {
  685. webSocket.send(JSON.stringify({
  686. "stumble": "ban",
  687. "handle": handle
  688. }));
  689. } else {
  690. console.error('Username not found');
  691. }
  692. } else if (webSocket && webSocket.readyState === WebSocket.OPEN && msg !== "") {
  693. webSocket.send(JSON.stringify({
  694. "stumble": "msg",
  695. "text": msg
  696. }));
  697. console.log('Sending regular message:', msg);
  698. } else {
  699. console.error('WebSocket is not defined or message is empty');
  700. }
  701. input.value = ""; // Clear the input field after handling
  702. });
  703.  
  704. document.body.appendChild(input);
  705. document.body.appendChild(button);
  706. }
  707.  
  708. // Function to send JSON data to WebSocket server
  709. function sendJSONData(data) {
  710. if (webSocket && webSocket.readyState === WebSocket.OPEN) {
  711. webSocket.send(JSON.stringify(data));
  712. console.log('JSON data sent:', data);
  713. } else {
  714. console.error('WebSocket connection is not open');
  715. }
  716. }
  717.  
  718. // Example JSON data
  719. const jsonData = {
  720. stumble: "msg",
  721. text: "Hello, WebSocket server!"
  722. };
  723.  
  724. // Initialize input box
  725. createInputBox();
  726.  
  727. // Call sendJSONData function to send JSON data to WebSocket server
  728. sendJSONData(jsonData);
  729.  
  730. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement