Advertisement
MeKLiN2

Untitled

Nov 10th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. // ==UserScript==
  2. // @name user table with resizable and collapsible items
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Intercept WebSocket messages on StumbleChat and display them in a resizable and collapsible table overlay
  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. // Function to create the resizable, collapsible WebSocket overlay
  17. function createWebSocketOverlay() {
  18. const overlay = document.createElement("div");
  19. overlay.id = "webSocketOverlay";
  20. overlay.style.position = "fixed";
  21. overlay.style.top = "10%";
  22. overlay.style.left = "10%";
  23. overlay.style.width = "600px"; // Set the default size to 600px width
  24. overlay.style.height = "60%";
  25. overlay.style.backgroundColor = "rgba(0, 0, 0, 0.7)";
  26. overlay.style.zIndex = "9999";
  27. overlay.style.display = "none"; // Initially hidden
  28. overlay.style.alignItems = "center";
  29. overlay.style.justifyContent = "center";
  30. overlay.style.padding = "10px";
  31. overlay.style.resize = "both";
  32. overlay.style.overflow = "auto";
  33. overlay.style.boxSizing = "border-box";
  34.  
  35. // Create a close button for the overlay
  36. const closeButton = document.createElement("button");
  37. closeButton.textContent = "Close";
  38. closeButton.style.position = "absolute";
  39. closeButton.style.top = "10px";
  40. closeButton.style.right = "10px";
  41. closeButton.style.padding = "5px 10px";
  42. closeButton.style.backgroundColor = "#ff4b5c";
  43. closeButton.style.color = "white";
  44. closeButton.style.border = "none";
  45. closeButton.style.cursor = "pointer";
  46. closeButton.addEventListener('click', () => {
  47. overlay.style.display = "none";
  48. reopenButton.style.display = "inline-block"; // Show the reopen button when overlay is closed
  49. });
  50. overlay.appendChild(closeButton);
  51.  
  52. // Create the main table to hold all message items
  53. const table = document.createElement("table");
  54. table.style.width = "100%";
  55. table.style.backgroundColor = "white";
  56. table.style.borderCollapse = "collapse";
  57. overlay.appendChild(table);
  58.  
  59. // Append overlay to the document body
  60. document.body.appendChild(overlay);
  61.  
  62. return { overlay, table };
  63. }
  64.  
  65. // Method to create a collapsible section for each message type
  66. function createCollapsibleSection(title, content, symbol) {
  67. const section = document.createElement("tr");
  68. const header = document.createElement("td");
  69. header.colSpan = 2;
  70. header.style.padding = "10px";
  71. header.style.cursor = "pointer";
  72. header.style.backgroundColor = "#f2f2f2";
  73. header.innerHTML = `<strong>${symbol}</strong> ${title ? title : ""}`; // Avoid showing "Unknown" if no title
  74.  
  75. // Toggle the visibility of the content when clicked
  76. header.addEventListener("click", () => {
  77. const contentRow = section.querySelector(".contentRow");
  78. if (contentRow.style.display === "none") {
  79. contentRow.style.display = "table-row";
  80. } else {
  81. contentRow.style.display = "none";
  82. }
  83. });
  84.  
  85. section.appendChild(header);
  86.  
  87. // Create a row for content (initially hidden)
  88. const contentRow = document.createElement("tr");
  89. contentRow.classList.add("contentRow");
  90. contentRow.style.display = "none";
  91. const contentCell = document.createElement("td");
  92. contentCell.colSpan = 2;
  93. contentCell.style.padding = "10px";
  94. contentCell.style.whiteSpace = "pre-wrap";
  95. contentCell.textContent = content;
  96.  
  97. contentRow.appendChild(contentCell);
  98. section.appendChild(contentRow);
  99.  
  100. return section;
  101. }
  102.  
  103. // Method to display WebSocket messages
  104. function displayWebSocketMessage(message, table) {
  105. const data = JSON.parse(message); // Assuming message is a JSON string
  106.  
  107. // Add the broadcast section
  108. if (data.type === "broadcast") {
  109. const broadcastRow = document.createElement("tr");
  110. const broadcastCell = document.createElement("td");
  111. broadcastCell.colSpan = 2;
  112. broadcastCell.style.padding = "10px";
  113. broadcastCell.style.backgroundColor = "#ffeb3b";
  114. broadcastCell.textContent = JSON.stringify(data, null, 2); // Format the broadcast JSON
  115. broadcastRow.appendChild(broadcastCell);
  116. table.appendChild(broadcastRow);
  117. } else {
  118. // Create a collapsible section for non-broadcast messages
  119. const section = createCollapsibleSection(data.type || "", JSON.stringify(data, null, 2), "🔽");
  120. table.appendChild(section);
  121. }
  122. }
  123.  
  124. // Override WebSocket constructor to intercept WebSocket creation
  125. const originalWebSocket = window.WebSocket;
  126. window.WebSocket = function(url, protocols) {
  127. console.log('WebSocket URL:', url);
  128.  
  129. // Call original WebSocket constructor
  130. const ws = new originalWebSocket(url, protocols);
  131.  
  132. // Create the overlay and table
  133. const { overlay, table } = createWebSocketOverlay();
  134.  
  135. // Create the reopen button (initially hidden)
  136. const reopenButton = document.createElement("button");
  137. reopenButton.textContent = "Open WebSocket Log";
  138. reopenButton.style.position = "fixed";
  139. reopenButton.style.bottom = "10px";
  140. reopenButton.style.left = "10px";
  141. reopenButton.style.padding = "10px 15px";
  142. reopenButton.style.backgroundColor = "#4CAF50";
  143. reopenButton.style.color = "white";
  144. reopenButton.style.border = "none";
  145. reopenButton.style.cursor = "pointer";
  146. reopenButton.style.display = "none"; // Initially hidden
  147. reopenButton.style.zIndex = "10000"; // Ensure it stays on top
  148. reopenButton.addEventListener('click', () => {
  149. overlay.style.display = "flex";
  150. reopenButton.style.display = "none"; // Hide reopen button when overlay is open
  151. });
  152. document.body.appendChild(reopenButton);
  153.  
  154. // Event listener for receiving messages
  155. ws.addEventListener('message', event => {
  156. displayWebSocketMessage(event.data, table);
  157. overlay.style.display = "flex"; // Show the overlay when a message is received
  158. reopenButton.style.display = "none"; // Hide reopen button when overlay is open
  159. });
  160.  
  161. return ws;
  162. };
  163.  
  164. })();
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement