Advertisement
MeKLiN2

Intercept WebSocket messages on StumbleChat and display them

Nov 8th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Intercept
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Intercept WebSocket messages on StumbleChat and display them
  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.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Create a function to display WebSocket messages
  17. function displayWebSocketMessage(message) {
  18. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  19. if (webSocketMessagesDiv) {
  20. webSocketMessagesDiv.innerHTML += message + "<br>";
  21. webSocketMessagesDiv.scrollTop = webSocketMessagesDiv.scrollHeight;
  22. }
  23. }
  24.  
  25. // Override WebSocket constructor to intercept WebSocket creation
  26. const originalWebSocket = window.WebSocket;
  27. window.WebSocket = function(url, protocols) {
  28. console.log('WebSocket URL:', url);
  29.  
  30. // Call original WebSocket constructor
  31. const ws = new originalWebSocket(url, protocols);
  32.  
  33. // Event listener for receiving messages
  34. ws.addEventListener('message', event => {
  35. displayWebSocketMessage(event.data);
  36. });
  37.  
  38. return ws;
  39. };
  40.  
  41. // Method to create the div for displaying WebSocket messages
  42. function createWebSocketMessagesDiv() {
  43. const div = document.createElement("div");
  44. div.id = "webSocketMessages";
  45. div.style.position = "relative";
  46. div.style.height = "25%";
  47. div.style.paddingLeft = "2px";
  48. div.style.willChange = "transform";
  49. div.style.boxSizing = "border-box";
  50. div.style.overflowX = "hidden";
  51. div.style.overflowY = "auto";
  52. div.style.color = "#ffffff"; // Set font color to white
  53. div.style.padding = "10px"; // Example padding
  54. div.style.zIndex = "2"; // Set a higher z-index value
  55.  
  56. // Additional styles for specific scenarios
  57. div.style.display = "flex";
  58. div.style.flexDirection = "column";
  59. div.style.justifyContent = "flex-end";
  60. div.style.fontSize = "12px";
  61.  
  62. div.style.whiteSpace = "normal"; // Allow text to wrap within the container
  63. div.style.wordWrap = "break-word"; // Allow long words to break and wrap
  64.  
  65. // Locate and append custom div the chat-position div
  66. const chatPositionDiv = document.getElementById("chat-position");
  67. if (chatPositionDiv) {
  68. chatPositionDiv.appendChild(div);
  69. } else {
  70. // If chat-position div not found, append to document body
  71. document.body.appendChild(div);
  72. }
  73. }
  74. // Call the function to create the WebSocket messages div
  75. createWebSocketMessagesDiv();
  76. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement