Advertisement
MeKLiN2

Untitled

Jan 28th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Intercept StumbleChat WebSocket Messages
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-01-28
  5. // @description Intercept WebSocket messages on StumbleChat and display them
  6. // @author You
  7. // @match https://stumblechat.com/room/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=stumblechat.com
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Create a function to display WebSocket messages
  16. function displayWebSocketMessage(message) {
  17. const webSocketMessagesDiv = document.getElementById("webSocketMessages");
  18. if (webSocketMessagesDiv) {
  19. webSocketMessagesDiv.innerHTML += message + "<br>";
  20. webSocketMessagesDiv.scrollTop = webSocketMessagesDiv.scrollHeight;
  21. }
  22. }
  23.  
  24. // Override WebSocket constructor to intercept WebSocket creation
  25. const originalWebSocket = window.WebSocket;
  26. window.WebSocket = function(url, protocols) {
  27. console.log('WebSocket URL:', url);
  28.  
  29. // Call original WebSocket constructor
  30. const ws = new originalWebSocket(url, protocols);
  31.  
  32. // Event listener for receiving messages
  33. ws.addEventListener('message', event => {
  34. displayWebSocketMessage(event.data);
  35. });
  36.  
  37. return ws;
  38. };
  39.  
  40. // Method to create the div for displaying WebSocket messages
  41. function createWebSocketMessagesDiv() {
  42. const div = document.createElement("div");
  43. div.id = "webSocketMessages";
  44. div.style.border = "1px solid #ccc";
  45. div.style.overflow = "auto";
  46. div.style.maxHeight = "200px";
  47. document.body.appendChild(div);
  48. }
  49.  
  50. // Call the function to create the WebSocket messages div
  51. createWebSocketMessagesDiv();
  52. })();
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement